Skip to content

Commit 04b6542

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 2bf2c57 commit 04b6542

19 files changed

+70
-59
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
# Task file: clear_facts, run linux-system-roles.ssh,.
3+
# Include this with include_tasks or import_tasks
4+
# Input:
5+
# - __sr_tasks_from: tasks_from to run - same as tasks_from in include_role
6+
# - __sr_public: export private vars from role - same as public in include_role
7+
# - __sr_failed_when: set to false to ignore role errors - same as failed_when in include_role
8+
- name: Clear facts
9+
meta: clear_facts
10+
11+
# note that you can use failed_when with import_role but not with include_role
12+
# so this simulates the __sr_failed_when false case
13+
# Q: Why do we need a separate task to run the role normally? Why not just
14+
# run the role in the block and rethrow the error in the rescue block?
15+
# A: Because you cannot rethrow the error in exactly the same way as the role does.
16+
# It might be possible to exactly reconstruct ansible_failed_result but it's not worth the effort.
17+
- name: Run the role with __sr_failed_when false
18+
when:
19+
- __sr_failed_when is defined
20+
- not __sr_failed_when
21+
block:
22+
- name: Run the role
23+
include_role:
24+
name: linux-system-roles.ssh
25+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
26+
public: "{{ __sr_public | default(false) }}"
27+
rescue:
28+
- name: Ignore the failure when __sr_failed_when is false
29+
debug:
30+
msg: Ignoring failure when __sr_failed_when is false
31+
32+
- name: Run the role normally
33+
include_role:
34+
name: linux-system-roles.ssh
35+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
36+
public: "{{ __sr_public | default(false) }}"
37+
when: __sr_failed_when | d(true)

tests/tests_additional_packages.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
- name: Verify default packages as well as additional are installed
33
hosts: all
4-
gather_facts: true
54
vars:
65
additional_package: >-
76
{% if ansible_facts['distribution'] in [ 'RedHat', 'CentOS' ] and
@@ -16,8 +15,7 @@
1615
{% endif %}
1716
tasks:
1817
- name: Run role
19-
include_role:
20-
name: linux-system-roles.ssh
18+
include_tasks: tasks/run_role_with_clear_facts.yml
2119
vars:
2220
ssh_additional_packages:
2321
- "{{ additional_package | trim }}"

tests/tests_all_options.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
- name: Test we can handle all configuration options documented in manual page
33
hosts: all
4-
gather_facts: true
54
vars:
65
ssh_c: {}
76
pkg_mgr: "{{ (ansible_facts['distribution_version'] | int > 7) |
@@ -102,8 +101,7 @@
102101
"{{ ssh_options.stdout_lines }}"
103102

104103
- name: Run role
105-
include_role:
106-
name: linux-system-roles.ssh
104+
include_tasks: tasks/run_role_with_clear_facts.yml
107105
vars:
108106
__ssh_supports_validate: false
109107
ssh_config_file: /etc/test_ssh_config

tests/tests_backup.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
with_items: "{{ backup_files.files }}"
2323

2424
- name: Configure ssh without creating backup
25-
ansible.builtin.include_role:
26-
name: linux-system-roles.ssh
25+
include_tasks: tasks/run_role_with_clear_facts.yml
2726
vars:
2827
ssh_backup: false
2928

@@ -34,8 +33,7 @@
3433
register: no_backup
3534

3635
- name: Configure ssh again with different configuration and with backup
37-
ansible.builtin.include_role:
38-
name: linux-system-roles.ssh
36+
include_tasks: tasks/run_role_with_clear_facts.yml
3937
vars:
4038
ssh_ForwardX11Trusted: 'yes' # noqa var-naming
4139
register: second_run

tests/tests_custom_config.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
hosts: all
44
tasks:
55
- name: Run role
6-
include_role:
7-
name: linux-system-roles.ssh
6+
include_tasks: tasks/run_role_with_clear_facts.yml
87
vars:
98
ssh:
109
Compression: true

tests/tests_custom_drop_in.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
- name: Test we can write any other configuration file as a drop-in
33
hosts: all
4-
gather_facts: true
54
vars:
65
__ssh_test_backup_files:
76
- /tmp/ssh_config.d_00-ansible.conf
@@ -21,8 +20,7 @@
2120
include_tasks: tasks/backup.yml
2221

2322
- name: Run role
24-
include_role:
25-
name: linux-system-roles.ssh
23+
include_tasks: tasks/run_role_with_clear_facts.yml
2624
vars:
2725
ssh:
2826
Compression: true

tests/tests_custom_with_defaults.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
hosts: all
44
tasks:
55
- name: Run role
6-
include_role:
7-
name: linux-system-roles.ssh
6+
include_tasks: tasks/run_role_with_clear_facts.yml
87
vars:
98
ssh:
109
Compression: true

tests/tests_default.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
- name: Ensure that the role runs with default parameters
1313
hosts: all
14-
gather_facts: false
15-
roles:
16-
- linux-system-roles.ssh
14+
tasks:
15+
- name: Run role
16+
include_tasks: tasks/run_role_with_clear_facts.yml
17+
vars:
18+
__sr_public: true
1719

1820
- name: Restore configuration files
1921
hosts: all

tests/tests_global_config.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
- name: Test we can write global config with default configuration
3-
gather_facts: true
43
hosts: all
54
vars:
65
__ssh_test_backup_files:
@@ -12,8 +11,7 @@
1211
include_tasks: tasks/backup.yml
1312

1413
- name: Run role
15-
include_role:
16-
name: linux-system-roles.ssh
14+
include_tasks: tasks/run_role_with_clear_facts.yml
1715
vars:
1816
# noqa var-naming
1917
ssh:

tests/tests_global_config_mode.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
- name: Test we can write global config with given permissions
33
hosts: all
4-
gather_facts: true
54
vars:
65
__ssh_test_backup_files:
76
- /etc/ssh/ssh_config.d/00-ansible.conf
@@ -16,8 +15,7 @@
1615
include_tasks: tasks/backup.yml
1716

1817
- name: Run role
19-
include_role:
20-
name: linux-system-roles.ssh
18+
include_tasks: tasks/run_role_with_clear_facts.yml
2119
vars:
2220
ssh:
2321
Compression: true

0 commit comments

Comments
 (0)