Files
ansible-role-aide/tests/tests_include_vars_from_parent.yml
Rich MegginsonandRichard Megginson 091570301d 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>
2026-03-19 09:18:34 -06:00

55 lines
2.2 KiB
YAML

---
- name: Test role include variable override
hosts: all
tasks:
- name: Create var file in caller that can override the one in called role
delegate_to: localhost
copy:
# usually the fake file will cause the called role to crash of
# overriding happens, but if not, set a variable that will
# allow to detect the bug
content: "__caller_override: true"
# XXX ugly, self-modifying code - changes the "caller" role on
# the controller
dest: "{{ playbook_dir }}/roles/caller/vars/{{ item }}.yml"
mode: preserve
loop: "{{ varfiles | unique }}"
# In case the playbook is executed against multiple hosts, use
# only the first one. Otherwise the hosts would stomp on each
# other since they are changing files on the controller.
when: inventory_hostname == ansible_play_hosts_all[0]
vars:
# change to hostvars['localhost']['ansible_facts'] to use the
# information for localhost
facts: "{{ ansible_facts }}"
versions:
- "{{ facts['distribution_version'] }}"
- "{{ facts['distribution_major_version'] }}"
separators: ["-", "_"]
# create all variants like CentOS, CentOS_8.1, CentOS-8.1,
# CentOS-8, CentOS-8.1
# more formally:
# {{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_version'] }}
# {{ ansible_facts['distribution'] }}-{{ ansible_facts['distribution_major_version'] }}
# {{ ansible_facts['distribution'] }}
# {{ ansible_facts['os_family'] }}
# and the same for _ as separator.
varfiles: "{{ [facts['distribution']] | product(separators) |
map('join') | product(versions) | map('join') | list +
[facts['distribution'], facts['os_family']] }}"
register: __varfiles_created
- name: Import role
import_role:
name: caller
vars:
roletoinclude: linux-system-roles.aide
- name: Cleanup
file:
path: "{{ item.dest }}"
state: absent
loop: "{{ __varfiles_created.results }}"
delegate_to: localhost
when: inventory_hostname == ansible_play_hosts_all[0]