We think we experience an anomalie with undefined variables and usage of subelements:
`
name: Enable/disable service
firewalld:
zone: “{{ item.0.ZONE }}”
service: “{{ item.1.SERVICE }}”
permanent: yes
immediate: yes
state: “{{ item.1.STATE }}”
loop: “{{ FIREWALLD_ZONE_SERVICE|subelements(‘SERVICES’) }}”
when: FIREWALLD_ZONE_SERVICE is defined
`
FIREWALLD_ZONE_SERVICE is NOT defined. Our expectation is that ansible will skip this task like it does at other tasks without sublements. With subelements in the loop ansible tries to resolve undefined FIREWALLD_ZONE_SERVICE and fails with an error:
`
fatal: […]: FAILED! => {“msg”: “obj must be a list of dicts or a nested dict”}
`
If FIREWALLD_ZONE_SERVICE is defined everything works like expected.
- *subelements* filter is executed before *when*, because
- *when* is evaluated on each iteration, because
- *when* argument can change on each iteration (e.g. may include *item*).
- *when* can not be evaluated before *subelements*, because *item* does not
exist yet.
The loop will fail
loop: "{{ FIREWALLD_ZONE_SERVICE|subelements('SERVICES') }}"
when: FIREWALLD_ZONE_SERVICE is defined
FAILED! => {"msg": "'FIREWALLD_ZONE_SERVICE' is undefined"}
The loop below is skipped when FIREWALLD_ZONE_SERVICE is not defined
(this is not consistent, it should also fail. Source would reveal why)
loop: "{{ FIREWALLD_ZONE_SERVICE }}"
when: FIREWALLD_ZONE_SERVICE is defined
,but it will also fail if the evaluation of the *loop* argument
(FIREWALLD_ZONE_SERVICE) before *when* is forced e.g.
loop: "{{ FIREWALLD_ZONE_SERVICE }}"
when: item|length > 10 and
FIREWALLD_ZONE_SERVICE is defined
FAILED! => {"msg": "'FIREWALLD_ZONE_SERVICE' is undefined"}