# Checking whether nested variable is defined in the when statement

**URL:** <https://forum.ansible.com/t/checking-whether-nested-variable-is-defined-in-the-when-statement/31826>\
**Category:** Ansible Project\
**Created:** [December 16, 2019, 8:57am UTC](https://forum.ansible.com/t/checking-whether-nested-variable-is-defined-in-the-when-statement/31826 "2019-12-16T08:57:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emmanuel\_Livingstone](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Emmanuel\_Livingstone](https://forum.ansible.com/u/Emmanuel_Livingstone)\
**Post date:** [December 16, 2019, 8:57am UTC](https://forum.ansible.com/t/checking-whether-nested-variable-is-defined-in-the-when-statement/31826/1 "2019-12-16T08:57:57Z")

</div>

Nested data structures can be accessed either by indexing using the key name or dot syntax with the key name - [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#accessing-complex-variable-data](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-complex-variable-data)

But I’m not able to access nested data structure from variables in the when statement. Here is a minimal sample to demonstrate the issue

> # cat play.yml
> 
> - hosts: localhost
> 
> vars:
> 
> parent:
> 
> child1: true
> 
> tasks:
> 
> - name: “Check if variable is defined”
> 
> fail:
> 
> msg: “mandatory variable {{ item }} not passed as extra args when invoking playbook”
> 
> when: item not in vars
> 
> loop:
> 
> - parent
> 
> - parent.child1
> 
> - parent.child2

Here is the sample output

ansible-playbook play.yml (livingstone-dev/monitoring)

> [WARNING]: No inventory was parsed, only implicit localhost is available
> 
> [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’
> 
> PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [localhost]
> 
> TASK [Check if variable is defined] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> skipping: [localhost] =\> (item=parent)
> 
> failed: [localhost] (item=parent.child1) =\> {“ansible\_loop\_var”: “item”, “changed”: false, “item”: “parent.child1”, “msg”: “mandatory variable parent.child1 not passed as extra args when invoking playbook”}
> 
> failed: [localhost] (item=parent.child2) =\> {“ansible\_loop\_var”: “item”, “changed”: false, “item”: “parent.child2”, “msg”: “mandatory variable parent.child2 not passed as extra args when invoking playbook”}
> 
> PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

Any idea how I can have nested variables in ansible when statements.

---

<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:** [December 16, 2019, 12:10pm UTC](https://forum.ansible.com/t/checking-whether-nested-variable-is-defined-in-the-when-statement/31826/2 "2019-12-16T12:10:24Z")

</div>

Hi Emmanuel,

> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;parent:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;child1: true  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name: "Check if variable is defined"  
> &nbsp;&nbsp;&nbsp;&nbsp;fail:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "mandatory variable {{ item }} not passed as extra args"  
> &nbsp;&nbsp;&nbsp;&nbsp;when: item not in vars  
> &nbsp;&nbsp;&nbsp;&nbsp;loop:  
> &nbsp;&nbsp;&nbsp;&nbsp;- parent  
> &nbsp;&nbsp;&nbsp;&nbsp;- parent.child1  
> &nbsp;&nbsp;&nbsp;&nbsp;- parent.child2
> 
> skipping: [localhost] =\> (item=parent)  
> failed: [localhost] (item=parent.child1) =\> {"ansible\_loop\_var": "item",  
> "changed": false, "item": "parent.child1", "msg": "mandatory variable  
> parent.child1 not passed as extra args when invoking playbook"}

'json\_query' returns 'False' when the parameter is not defined. 'set\_fact'  
before the evaluation to register the variable with 'hostvars'. For example

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent: "{{ parent }}"

&nbsp;&nbsp;&nbsp;&nbsp;- fail:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "mandatory variable {{ item }} not passed as extra args"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: not hostvars[inventory\_hostname]|json\_query(item)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- parent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- parent.child1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- parent.child2

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Emmanuel\_Livingstone](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Emmanuel\_Livingstone](https://forum.ansible.com/u/Emmanuel_Livingstone)\
**Post date:** [December 17, 2019, 3:49am UTC](https://forum.ansible.com/t/checking-whether-nested-variable-is-defined-in-the-when-statement/31826/3 "2019-12-17T03:49:04Z")

</div>

Thanks Vladimir. I’m sure your solution works. I’ve figured out a simpler solution. Just posting it here for reference - [https://stackoverflow.com/a/59367345/434678](https://stackoverflow.com/a/59367345/434678)
