Skip to content

Commit acab375

Browse files
committed
test: ensure role gathers the facts it uses by having test clear_facts 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>
1 parent 2034137 commit acab375

3 files changed

Lines changed: 6 additions & 7 deletions

File tree

tests/tests_default.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
hosts: all
55
tasks:
66
- name: Include trustee_server role
7-
ansible.builtin.include_role:
8-
name: linux-system-roles.trustee_server
9-
public: true
7+
ansible.builtin.include_tasks: tasks/run_role_with_clear_facts.yml
8+
vars:
9+
__sr_public: true
1010

1111
- name: Gather package facts
1212
ansible.builtin.package_facts:

tests/tests_include_vars_from_parent.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
- name: Test role include variable override
33
hosts: all
4-
gather_facts: true
54
tasks:
65
- name: Create var file in caller that can override the one in called role
76
delegate_to: localhost

tests/tests_secret_registration_server.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
trustee_server_secret_registration_enabled: true
88
tasks:
99
- name: Include trustee_server role
10-
ansible.builtin.include_role:
11-
name: linux-system-roles.trustee_server
12-
public: true
10+
ansible.builtin.include_tasks: tasks/run_role_with_clear_facts.yml
11+
vars:
12+
__sr_public: true
1313

1414
- name: Gather package facts
1515
ansible.builtin.package_facts:

0 commit comments

Comments
 (0)