# Undefined variables and filter subelements

**URL:** <https://forum.ansible.com/t/undefined-variables-and-filter-subelements/31616>\
**Category:** Ansible Project\
**Created:** [November 7, 2019, 12:43pm UTC](https://forum.ansible.com/t/undefined-variables-and-filter-subelements/31616 "2019-11-07T12:43:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobias\_Kirchhofer](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@Tobias\_Kirchhofer](https://forum.ansible.com/u/Tobias_Kirchhofer)\
**Post date:** [November 7, 2019, 12:43pm UTC](https://forum.ansible.com/t/undefined-variables-and-filter-subelements/31616/1 "2019-11-07T12:43:02Z")

</div>

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.

Anyone?

---

<div class="post-metadata">

**Author:** ![Tobias\_Kirchhofer](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@Tobias\_Kirchhofer](https://forum.ansible.com/u/Tobias_Kirchhofer)\
**Post date:** [November 8, 2019, 11:54am UTC](https://forum.ansible.com/t/undefined-variables-and-filter-subelements/31616/2 "2019-11-08T11:54:47Z")

</div>

Maybe i was not precise enough. Situation is this:

If the var is defined it looks like this:

`  
FIREWALLD\_ZONE\_SERVICE:

- ZONE: public  
SERVICES:
- SERVICE: http  
STATE: enabled
- SERVICE: https  
STATE: enabled

`

We loop through SERVICES with  
`  
FIREWALLD\_ZONE\_SERVICE|subelements(‘SERVICES’)

`

This is fine as long the var is defined. It not we expect that

`  
when: FIREWALLD\_ZONE\_SERVICE is defined

`

prevents the evaluation of the war in the loop.

In another context it works fine:

`loop: "{{ FIREWALLD_ZONE|product(FIREWALLD_ZONE.ZONES)|list }}"`

`  
when: FIREWALLD\_ZONE is defined

`

If FIREWALLD\_ZONE is not defined the var in the loop is not evaluated.

So the difference is:

If we have ‘subelements’ in the loop ‘when: var is defined’ is not working.  
If we have eq. ‘product’ in the loop ‘when: var is defined’ works.

More clear now? 🙂

Could someone spend some thoughts? Maybe it is a misunderstanding on our site…

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 8, 2019, 12:31pm UTC](https://forum.ansible.com/t/undefined-variables-and-filter-subelements/31616/3 "2019-11-08T12:31:46Z")

</div>

Use \*default\* and \*skip\_missing\* subelements. For example

&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ FIREWALLD\_ZONE\_SERVICE|default()|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subelements('SERVICES', skip\_missing=True) }}"

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Tobias\_Kirchhofer](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@Tobias\_Kirchhofer](https://forum.ansible.com/u/Tobias_Kirchhofer)\
**Post date:** [November 8, 2019, 2:13pm UTC](https://forum.ansible.com/t/undefined-variables-and-filter-subelements/31616/4 "2019-11-08T14:13:10Z")

</div>

> Use _default_ and _skip\_missing_ subelements. For example
> 
> loop: “{{ FIREWALLD\_ZONE\_SERVICE|default()|  
> subelements(‘SERVICES’, skip\_missing=True) }}”

Thank you! This is working. Is only needed with ‘subelements’. But why? Anyway - thanks! 🙂

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 8, 2019, 3:55pm UTC](https://forum.ansible.com/t/undefined-variables-and-filter-subelements/31616/5 "2019-11-08T15:55:59Z")

</div>

I guess

&nbsp;&nbsp;- \*subelements\* filter is executed before \*when\*, because  
&nbsp;&nbsp;- \*when\* is evaluated on each iteration, because  
&nbsp;&nbsp;- \*when\* argument can change on each iteration (e.g. may include \*item\*).  
&nbsp;&nbsp;- \*when\* can not be evaluated before \*subelements\*, because \*item\* does not  
&nbsp;&nbsp;&nbsp;&nbsp;exist yet.

The loop will fail

&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ FIREWALLD\_ZONE\_SERVICE|subelements('SERVICES') }}"  
&nbsp;&nbsp;when: FIREWALLD\_ZONE\_SERVICE is defined

&nbsp;&nbsp;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)

&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ FIREWALLD\_ZONE\_SERVICE }}"  
&nbsp;&nbsp;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.

&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ FIREWALLD\_ZONE\_SERVICE }}"  
&nbsp;&nbsp;when: item|length \> 10 and  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FIREWALLD\_ZONE\_SERVICE is defined  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;FAILED! =\> {"msg": "'FIREWALLD\_ZONE\_SERVICE' is undefined"}

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 8, 2019, 4:07pm UTC](https://forum.ansible.com/t/undefined-variables-and-filter-subelements/31616/6 "2019-11-08T16:07:01Z")

</div>

As a result, \*default\* is better construct

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ FIREWALLD\_ZONE\_SERVICE|default() }}"

than \*when\*

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ FIREWALLD\_ZONE\_SERVICE }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: FIREWALLD\_ZONE\_SERVICE is defined

---

<div class="post-metadata">

**Author:** ![Tobias\_Kirchhofer](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@Tobias\_Kirchhofer](https://forum.ansible.com/u/Tobias_Kirchhofer)\
**Post date:** [November 9, 2019, 8:04am UTC](https://forum.ansible.com/t/undefined-variables-and-filter-subelements/31616/7 "2019-11-09T08:04:09Z")

</div>

Thank you for the [b](https://groups.google.com/forum/#!msg/ansible-project/Y1mGC3vrsng/8DZRiJU6AwAJ)ackground explanation! Interesting.
