# defaulting on undefined nested dictionaries

**URL:** <https://forum.ansible.com/t/defaulting-on-undefined-nested-dictionaries/19003>\
**Category:** Ansible Project\
**Created:** [August 12, 2015, 2:11pm UTC](https://forum.ansible.com/t/defaulting-on-undefined-nested-dictionaries/19003 "2015-08-12T14:11:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_McMahon](https://avatars.discourse-cdn.com/v4/letter/p/2acd7d/32.png) [@Patrick\_McMahon](https://forum.ansible.com/u/Patrick_McMahon)\
**Post date:** [August 12, 2015, 2:11pm UTC](https://forum.ansible.com/t/defaulting-on-undefined-nested-dictionaries/19003/1 "2015-08-12T14:11:40Z")

</div>

Hi all,

I have a dictionary with a nested dictionary defined in defaults/main.yml of my role:

`file_system_system_vg: name: "A Name" lv: {}`

where lv is the nested dictionary.

I can use the nested dictionary in a task like this:  
``

`

- name: Create Logicals Volumes on primary VG  
lvol: vg={{ file\_system\_system\_vg.name }} lv={{ item.value.name }} size={{ item.value.size }}  
with\_dict: **file\_system\_system\_vg.lv**  
`

This all works fine when `file_system_system_vg` is defined, but i would like to remove it from defaults/main.yml and only run the task when `file_system_system_vg. **lv** ` is defined elsewhere, so I have tried this:

`

- name: Create Logicals Volumes on primary VG  
lvol: vg={{ file\_system\_system\_vg.name }} lv={{ item.value.name }} size={{ item.value.size }}  
with\_dict: **file\_system\_system\_vg.lv | default ({})**  
`

but it throws an error:

`TASK: [file_system | Create Logicals Volumes on primary VG] ******************* fatal: [dsdptgfc06v.devqa.exch.int] => with_dict expects a dict`

Please note that `file_system_system_vg `is not defined here and neither is```file_system_system_vg.lv.`Does this make sense?  
How can I make ansible skip this task when both`file_system_system_vg`and```file_system_system_vg.lv`are undefined?  
N.B when I run the task I need to use the dictionaries within `file_system_system_vg.lv`

Thanks in advance  
Paddy

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 12, 2015, 2:40pm UTC](https://forum.ansible.com/t/defaulting-on-undefined-nested-dictionaries/19003/2 "2015-08-12T14:40:26Z")

</div>

have you tried:

with\_dict: "{{ file\_system\_system\_vg is  
defined\>ternary(file\_system\_system\_vg.lv|default({}), {})}}"

?

---

<div class="post-metadata">

**Author:** ![Patrick\_McMahon](https://avatars.discourse-cdn.com/v4/letter/p/2acd7d/32.png) [@Patrick\_McMahon](https://forum.ansible.com/u/Patrick_McMahon)\
**Post date:** [August 12, 2015, 3:06pm UTC](https://forum.ansible.com/t/defaulting-on-undefined-nested-dictionaries/19003/3 "2015-08-12T15:06:18Z")

</div>

Hi Brian,

so in my defaults/main.yml if I define only this:

`file_system_system_vg:`

It works! so we are nearly there. If I remove `file_system_system_vg `from` `defaults/main.yml it throws an error again:

`

TASK: [file\_system | Create Logicals Volumes on primary VG] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [dsdptgfc06v.devqa.exch.int] =\> with\_dict expects a dict

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
`

So is it possible to remove `file_system_system_vg `completely from` `defaults/main.yml?

Thanks  
Patrick

---

<div class="post-metadata">

**Author:** ![Patrick\_McMahon](https://avatars.discourse-cdn.com/v4/letter/p/2acd7d/32.png) [@Patrick\_McMahon](https://forum.ansible.com/u/Patrick_McMahon)\
**Post date:** [August 12, 2015, 3:54pm UTC](https://forum.ansible.com/t/defaulting-on-undefined-nested-dictionaries/19003/4 "2015-08-12T15:54:12Z")

</div>

I made a simple playbook to show the issue. It seems ansible evalutes all variables regardless of the conditional result before ternary.  
If you run the playbook twice, once with "`simpleDict" `defined, and again with it commented out.``

`

---

<div class="post-metadata">

**Author:** ![Christian\_Thiemann](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@Christian\_Thiemann](https://forum.ansible.com/u/Christian_Thiemann)\
**Post date:** [August 12, 2015, 8:13pm UTC](https://forum.ansible.com/t/defaulting-on-undefined-nested-dictionaries/19003/5 "2015-08-12T20:13:25Z")

</div>

You can specify default values for both the dictionary in which you are trying to look something up, and the key you are looking up:

` `with\_dict: **(file\_system\_system\_vg|default({})).lv|default({})**` `

The parentheses are important!

---

<div class="post-metadata">

**Author:** ![Patrick\_McMahon](https://avatars.discourse-cdn.com/v4/letter/p/2acd7d/32.png) [@Patrick\_McMahon](https://forum.ansible.com/u/Patrick_McMahon)\
**Post date:** [August 12, 2015, 9:43pm UTC](https://forum.ansible.com/t/defaulting-on-undefined-nested-dictionaries/19003/6 "2015-08-12T21:43:08Z")

</div>

It works, exactly what I was after! Thanks Christian

Patrick
