Skip to content

test: ensure role gathers the facts it uses by having test clear_facts before include_role#231

Merged
richm merged 1 commit intolinux-system-roles:mainfrom
richm:clear_facts
Mar 20, 2026
Merged

test: ensure role gathers the facts it uses by having test clear_facts before include_role#231
richm merged 1 commit intolinux-system-roles:mainfrom
richm:clear_facts

Conversation

@richm
Copy link
Contributor

@richm richm commented Mar 19, 2026

The role gathers the facts it uses. For example, if the user uses
ANSIBLE_GATHERING=explicit, the role uses the setup module with the
facts and subsets it requires.

This change allows us to test this. Before every role invocation, the test
will use meta: clear_facts so that the role starts with no facts.

Create a task file tests/tasks/run_role_with_clear_facts.yml to do the tasks
to clear the facts and run the role. Note that this means we don't need to
use gather_facts for the tests.

Some vars defined using ansible_facts have been changed to be defined with
set_fact instead. This is because of the fact that vars are lazily
evaluated - the var might be referenced when the facts have been cleared, and
will issue an error like ansible_facts["distribution"] is undefined. This is
typically done for blocks that have a when condition that uses ansible_facts
and the block has a role invocation using run_role_with_clear_facts.yml
These have been rewritten to define the when condition using set_fact. This
is because the when condition is evaluated every time a task is invoked in the
block, and if the facts are cleared, this will raise an undefined variable error.

Signed-off-by: Rich Megginson rmeggins@redhat.com

Summary by Sourcery

Ensure ssh role tests run with a clean fact set so the role explicitly gathers the facts it needs.

New Features:

  • Introduce a reusable test task file that clears facts and then runs the ssh role with configurable options.

Enhancements:

  • Centralize role invocation logic in tests via a helper task file that can simulate include_role failed_when behavior.

Tests:

  • Update all ssh role tests to invoke the role through the new clear-facts helper instead of direct include_role or play-level gather_facts.

…s before include_role

The role gathers the facts it uses.  For example, if the user uses
`ANSIBLE_GATHERING=explicit`, the role uses the `setup` module with the
facts and subsets it requires.

This change allows us to test this.  Before every role invocation, the test
will use `meta: clear_facts` so that the role starts with no facts.

Create a task file tests/tasks/run_role_with_clear_facts.yml to do the tasks
to clear the facts and run the role.  Note that this means we don't need to
use `gather_facts` for the tests.

Some vars defined using `ansible_facts` have been changed to be defined with
`set_fact` instead.  This is because of the fact that `vars` are lazily
evaluated - the var might be referenced when the facts have been cleared, and
will issue an error like `ansible_facts["distribution"] is undefined`.  This is
typically done for blocks that have a `when` condition that uses `ansible_facts`
and the block has a role invocation using run_role_with_clear_facts.yml
These have been rewritten to define the `when` condition using `set_fact`.  This
is because the `when` condition is evaluated every time a task is invoked in the
block, and if the facts are cleared, this will raise an undefined variable error.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
@richm richm requested a review from Jakuje as a code owner March 19, 2026 23:24
@sourcery-ai
Copy link

sourcery-ai bot commented Mar 19, 2026

Reviewer's Guide

Refactors ssh role tests to run the role via a new helper task file that clears Ansible facts before each role invocation, ensuring the role gathers its own required facts, and adjusts test playbooks to use this helper instead of direct include_role/gather_facts.

File-Level Changes

Change Details Files
Introduce a reusable helper task file to clear facts and run the ssh role with optional behavior flags.
  • Create tests/tasks/run_role_with_clear_facts.yml that first issues meta: clear_facts, then includes the linux-system-roles.ssh role.
  • Support optional __sr_tasks_from, __sr_public, and __sr_failed_when variables to mimic include_role parameters.
  • Implement a block/rescue wrapper to simulate failed_when: false behavior while still running the role normally when __sr_failed_when is true or unset.
tests/tasks/run_role_with_clear_facts.yml
Update ssh role test playbooks to use the new helper instead of direct include_role and to stop relying on play-level gather_facts.
  • Replace direct include_role: linux-system-roles.ssh calls with include_tasks: tasks/run_role_with_clear_facts.yml while passing through existing ssh-related vars.
  • Remove gather_facts directives from tests that now rely on the role’s own fact gathering behavior.
  • Ensure any tests that previously ran the role via roles: now run it via a task that includes the new helper file.
tests/tests_global_drop_in_role.yml
tests/tests_default.yml
tests/tests_backup.yml
tests/tests_additional_packages.yml
tests/tests_all_options.yml
tests/tests_custom_drop_in.yml
tests/tests_global_config.yml
tests/tests_global_config_mode.yml
tests/tests_global_drop_in.yml
tests/tests_indentation.yml
tests/tests_match.yml
tests/tests_no_skip_defaults.yml
tests/tests_precedence.yml
tests/tests_regenerate_defaults.yml
tests/tests_user_config.yml
tests/tests_custom_config.yml
tests/tests_custom_with_defaults.yml
tests/tests_include_vars_from_parent.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@richm
Copy link
Contributor Author

richm commented Mar 19, 2026

[citest]

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In tests like tests_additional_packages.yml you’ve removed gather_facts but still compute variables using ansible_facts at play level, which will now be undefined; consider moving those expressions into a set_fact task before calling run_role_with_clear_facts.yml or retaining gather_facts where needed.
  • run_role_with_clear_facts.yml unconditionally runs meta: clear_facts for every inclusion; if any plays rely on facts before or after the role run, you may want to make clearing facts optional via a parameter to avoid surprising side effects in those scenarios.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In tests like `tests_additional_packages.yml` you’ve removed `gather_facts` but still compute variables using `ansible_facts` at play level, which will now be undefined; consider moving those expressions into a `set_fact` task before calling `run_role_with_clear_facts.yml` or retaining `gather_facts` where needed.
- `run_role_with_clear_facts.yml` unconditionally runs `meta: clear_facts` for every inclusion; if any plays rely on facts before or after the role run, you may want to make clearing facts optional via a parameter to avoid surprising side effects in those scenarios.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@richm richm merged commit 54ad872 into linux-system-roles:main Mar 20, 2026
38 of 39 checks passed
@richm richm deleted the clear_facts branch March 20, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant