I want to have an Ansible-Jinja2 expression with which I can detect whether a specific variable with a dynamic name is set in host/group vars or a role that is included. (Concretely, something like repo_backports_$distroname: true to enable the backports repo centrally. Anything that needs backports, playbook task or role, just sets it; even one suffices for presence; none setting it will cause removal.)
I’m unable to find a way to query such a variable dynamically and while debugging found that even statically does not work:
Assume two hosts, frw001 and frw254. They belong to the same group. frw001 has migrated_to_kea_dhcpd set to true in hostvars, frw254 not.
I have:
$ git grep testvar
roles/common-pcengine/defaults/main.yml:testvar: false
roles/dhcpd4/defaults/main.yml:testvar: true
I’ve experimented already with a playbook that only includes the frw254 host:
- hosts:
- frw254
roles:
- role: common-pcengine
- role: dhcpd
when: not (migrated_to_kea_dhcpd | default(false))
- role: dhcpd4
when: (migrated_to_kea_dhcpd | default(false))
tasks:
- name: statically
debug:
msg: v={{ testvar | default(false) }},m={{ migrated_to_kea_dhcpd | default(false) }}
tags: x
(Yes, this is not an MWE.)
Yet, when I start it, testvar shows up as true:
$ ansible-playbook playbooks/groups/pcengine.yml -l
frw254 -t x
[…]
TASK [statically] ***********************************************************************************************
ok: [frw254] => {
"msg": "v=True,m=False"
}
I’ve put debug: into every role file and can confirm that frw254 is in role dhcpd, not dhcpd4, so roles/dhcpd4/defaults/main.yml should not be read/used.
But, for some reason, it is?!
Is there a way I can trace when and from where a variable is set?