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>
This commit is contained in:
committed by
Richard Megginson
parent
84a356a028
commit
091570301d
@@ -0,0 +1,37 @@
|
||||
---
|
||||
# Task file: clear_facts, run linux-system-roles.aide.
|
||||
# Include this with include_tasks or import_tasks
|
||||
# Input:
|
||||
# - __sr_tasks_from: tasks_from to run - same as tasks_from in include_role
|
||||
# - __sr_public: export private vars from role - same as public in include_role
|
||||
# - __sr_failed_when: set to false to ignore role errors - same as failed_when in include_role
|
||||
- name: Clear facts
|
||||
meta: clear_facts
|
||||
|
||||
# note that you can use failed_when with import_role but not with include_role
|
||||
# so this simulates the __sr_failed_when false case
|
||||
# Q: Why do we need a separate task to run the role normally? Why not just
|
||||
# run the role in the block and rethrow the error in the rescue block?
|
||||
# A: Because you cannot rethrow the error in exactly the same way as the role does.
|
||||
# It might be possible to exactly reconstruct ansible_failed_result but it's not worth the effort.
|
||||
- name: Run the role with __sr_failed_when false
|
||||
when:
|
||||
- __sr_failed_when is defined
|
||||
- not __sr_failed_when
|
||||
block:
|
||||
- name: Run the role
|
||||
include_role:
|
||||
name: linux-system-roles.aide
|
||||
tasks_from: "{{ __sr_tasks_from | default('main') }}"
|
||||
public: "{{ __sr_public | default(false) }}"
|
||||
rescue:
|
||||
- name: Ignore the failure when __sr_failed_when is false
|
||||
debug:
|
||||
msg: Ignoring failure when __sr_failed_when is false
|
||||
|
||||
- name: Run the role normally
|
||||
include_role:
|
||||
name: linux-system-roles.aide
|
||||
tasks_from: "{{ __sr_tasks_from | default('main') }}"
|
||||
public: "{{ __sr_public | default(false) }}"
|
||||
when: __sr_failed_when | d(true)
|
||||
@@ -38,8 +38,7 @@
|
||||
- name: Run tests
|
||||
block:
|
||||
- name: Run the role and set up cron
|
||||
ansible.builtin.include_role:
|
||||
name: linux-system-roles.aide
|
||||
include_tasks: tasks/run_role_with_clear_facts.yml
|
||||
vars:
|
||||
aide_init: true
|
||||
aide_cron_check: true
|
||||
@@ -55,8 +54,7 @@
|
||||
failed_when: result is changed
|
||||
|
||||
- name: Run the role and and do not touch cron
|
||||
ansible.builtin.include_role:
|
||||
name: linux-system-roles.aide
|
||||
include_tasks: tasks/run_role_with_clear_facts.yml
|
||||
vars:
|
||||
aide_cron_interval: "0 1 * * *"
|
||||
|
||||
@@ -70,8 +68,7 @@
|
||||
failed_when: result is changed
|
||||
|
||||
- name: Run the role and disable cron
|
||||
ansible.builtin.include_role:
|
||||
name: linux-system-roles.aide
|
||||
include_tasks: tasks/run_role_with_clear_facts.yml
|
||||
vars:
|
||||
aide_cron_check: false
|
||||
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
---
|
||||
- name: Ensure that the role runs with default parameters
|
||||
hosts: all
|
||||
roles:
|
||||
- role: linux-system-roles.aide
|
||||
tasks:
|
||||
- name: Run the role
|
||||
include_tasks: tasks/run_role_with_clear_facts.yml
|
||||
vars:
|
||||
aide_config_template: files/aide-custom.conf.j2
|
||||
aide_install: true
|
||||
aide_init: true
|
||||
tasks:
|
||||
- name: Check header for ansible_managed, fingerprint
|
||||
include_tasks: tasks/check_header.yml
|
||||
vars:
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
---
|
||||
- name: Ensure that the role runs with default parameters
|
||||
hosts: all
|
||||
gather_facts: false # test that role works in this case
|
||||
roles:
|
||||
- linux-system-roles.aide
|
||||
tasks:
|
||||
- name: Run the role
|
||||
include_tasks: tasks/run_role_with_clear_facts.yml
|
||||
- name: Check if the file exists
|
||||
ansible.builtin.stat:
|
||||
path: /etc/aide.conf
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
---
|
||||
- name: Ensure that the role runs with default parameters
|
||||
hosts: all
|
||||
roles:
|
||||
- role: linux-system-roles.aide
|
||||
tasks:
|
||||
- name: Run the role
|
||||
include_tasks: tasks/run_role_with_clear_facts.yml
|
||||
vars:
|
||||
aide_init: true
|
||||
tasks:
|
||||
- name: Check if the file exists
|
||||
ansible.builtin.stat:
|
||||
path: /etc/aide.conf
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
---
|
||||
- name: Test role include variable override
|
||||
hosts: all
|
||||
gather_facts: true
|
||||
tasks:
|
||||
- name: Create var file in caller that can override the one in called role
|
||||
delegate_to: localhost
|
||||
|
||||
Reference in New Issue
Block a user