Skip to content

test: add tasks and action plugin to run role with clear facts [citest_skip]#321

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

test: add tasks and action plugin to run role with clear facts [citest_skip]#321
richm merged 1 commit intolinux-system-roles:mainfrom
richm:clear_facts

Conversation

@richm
Copy link
Copy Markdown
Collaborator

@richm richm commented Mar 16, 2026

The file tests/tasks/run_role_with_clear_facts.yml allows us to invoke the
role with no ansible_facts in order to test that the role is able to gather
its own facts if the user is using ANSIBLE_GATHERING=explicit. However,
the test may need facts, so this task will save the current facts, then clear
them, then run the role, then merge the saved facts with any facts set by
the role. The action plugin is needed to merge ansible_facts because that
is a "special" variable and cannot be modified with set_fact, update_fact,
etc.

@richm richm requested review from rjeffman and spetrosi as code owners March 16, 2026 20:33
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Mar 16, 2026

Reviewer's Guide

Introduce a reusable test task wrapper that clears and later restores Ansible facts around role execution, plus an action plugin to merge facts, and update certificate role tests to use this wrapper instead of including the role directly so the role’s own fact-gathering logic is exercised.

File-Level Changes

Change Details Files
Add a reusable task file to run the certificate role with facts cleared, then restore/merge facts for tests.
  • Create a task sequence that saves current ansible_facts, clears them with meta: clear_facts, runs linux-system-roles.certificate via include_role, and then merges/restores facts.
  • Introduce __sr_tasks_from, __sr_public, and __sr_failed_when parameters to mimic include_role behavior (tasks_from, public, and failed_when) when invoking the role.
  • Implement conditional error-handling logic to simulate failed_when: false by wrapping include_role in a block/rescue and ignoring failures when requested.
tests/tasks/run_role_with_clear_facts.yml
Introduce an action plugin to merge saved and current Ansible facts on the controller for use in tests.
  • Implement merge_ansible_facts action plugin that validates required arguments current_ansible_facts and saved_ansible_facts and ensures they are dicts.
  • Templating of arguments via self._templar.template to support Jinja inputs before merging.
  • Merge facts by copying saved facts and overlaying current facts, exposing the result via result['ansible_facts'] without marking the task changed.
tests/action_plugins/merge_ansible_facts.py
Update certificate role tests to invoke the role through the new run_role_with_clear_facts task wrapper instead of including/importing the role directly or using the roles: keyword.
  • Replace include_role/import_role invocations of linux-system-roles.certificate with include_tasks: tasks/run_role_with_clear_facts.yml, passing certificate_requests and related test variables through vars.
  • For cases that previously used tasks_from: set_vars.yml and public: true, pass __sr_tasks_from and __sr_public flags into the wrapper so it can delegate correctly to the role.
  • Convert plays that used roles: - linux-system-roles.certificate into explicit tasks that include the new wrapper, and drop explicit gather_facts settings where the wrapper and role’s own fact gathering make them unnecessary.
  • Adjust tests that relied on include_role/import_role semantics for error handling to instead use the wrapper’s __sr_failed_when parameter to simulate failed_when: false when needed.
tests/tests_key_size_reissue.yml
tests/tests_principal.yml
tests/tests_fs_attrs.yml
tests/tests_subject_complex.yml
tests/tests_basic_self_signed.yml
tests/tests_dns_ip_email.yml
tests/tests_key_usage_and_extended_key_usage.yml
tests/tests_no_auto_renew.yml
tests/tests_not_wait_for_cert.yml
tests/tests_provider.yml
tests/tests_run_hooks.yml
tests/tests_subject.yml
tests/tests_basic_ipa.yml
tests/tests_key_size.yml
tests/tests_many_self_signed.yml
tests/setup-snapshot.yml
tests/tests_default.yml
tests/tests_test_mode.yml
tests/tests_wrong_provider.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

Copy link
Copy Markdown

@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/tasks/run_role_with_clear_facts.yml, saved_facts: "{{ ansible_facts }}" will fail if the play is ever run with gather_facts: false; consider using a default (e.g. {{ ansible_facts | default({}) }}) and mirroring that assumption in the merge_ansible_facts plugin input validation.
  • The helper task file and plugin introduce a generic saved_facts variable name; to avoid accidental collisions with existing test variables, consider renaming it with a more specific prefix (e.g. __sr_saved_facts).
  • The comment in run_role_with_clear_facts.yml refers to tests/library being on the module path, but the implementation uses an action plugin under tests/action_plugins; it would be clearer to update the comment to match the actual plugin location/mechanism.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In tests/tasks/run_role_with_clear_facts.yml, `saved_facts: "{{ ansible_facts }}"` will fail if the play is ever run with `gather_facts: false`; consider using a default (e.g. `{{ ansible_facts | default({}) }}`) and mirroring that assumption in the `merge_ansible_facts` plugin input validation.
- The helper task file and plugin introduce a generic `saved_facts` variable name; to avoid accidental collisions with existing test variables, consider renaming it with a more specific prefix (e.g. `__sr_saved_facts`).
- The comment in run_role_with_clear_facts.yml refers to `tests/library` being on the module path, but the implementation uses an action plugin under `tests/action_plugins`; it would be clearer to update the comment to match the actual plugin location/mechanism.

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.

…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 self-assigned this Mar 18, 2026
@richm richm changed the title test: ensure role gathers the facts it uses by having test clear_facts before include_role test: add tasks and action plugin to run role with clear facts [citest_skip] Mar 18, 2026
@richm
Copy link
Copy Markdown
Collaborator Author

richm commented Mar 18, 2026

[citest]

@richm richm merged commit d2289b5 into linux-system-roles:main Mar 19, 2026
35 of 37 checks passed
@richm richm deleted the clear_facts branch March 19, 2026 13:24
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