# default for undefined dict

**URL:** <https://forum.ansible.com/t/default-for-undefined-dict/23682>\
**Category:** Ansible Project\
**Created:** [February 8, 2017, 10:29am UTC](https://forum.ansible.com/t/default-for-undefined-dict/23682 "2017-02-08T10:29:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrien\_nayrat](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@adrien\_nayrat](https://forum.ansible.com/u/adrien_nayrat)\
**Post date:** [February 8, 2017, 10:29am UTC](https://forum.ansible.com/t/default-for-undefined-dict/23682/1 "2017-02-08T10:29:57Z")

</div>

Hi,

Here is my variable :

instances:

- name: ref  
mgservices:  
name: test1

My default variable:

default\_mgservice :  
name: blah

I want to use “default\_mgservice.name” when mgservices is not defined :  
instances:

- name: ref

Here is my template

{% for instance in instances %}  
pgservice : {{ instance.mgservices.name | default(default\_mgservice.name) }}  
{% endfor %}

But it failed :

“AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘mgservices’”}

I don’t understand why default do not work on dict? Any idea?

Thanks

---

<div class="post-metadata">

**Author:** ![adrien\_nayrat](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@adrien\_nayrat](https://forum.ansible.com/u/adrien_nayrat)\
**Post date:** [February 8, 2017, 10:34am UTC](https://forum.ansible.com/t/default-for-undefined-dict/23682/2 "2017-02-08T10:34:18Z")

</div>

FYI :  
ansible 2.2.0.0

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 8, 2017, 10:41am UTC](https://forum.ansible.com/t/default-for-undefined-dict/23682/3 "2017-02-08T10:41:45Z")

</div>

Just a guess:  
Your problem is not the default, but rather how you try to address  
your dict.

Try this:  
pgservice: {{ instance["msgservices"]["name"] }}

Johannes

---

<div class="post-metadata">

**Author:** ![adrien\_nayrat](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@adrien\_nayrat](https://forum.ansible.com/u/adrien_nayrat)\
**Post date:** [February 8, 2017, 11:37am UTC](https://forum.ansible.com/t/default-for-undefined-dict/23682/4 "2017-02-08T11:37:24Z")

</div>

> Just a guess:  
> Your problem is not the default, but rather how you try to address  
> your dict.
> 
> Try this:  
> pgservice: {{ instance[“msgservices”][“name”] }}
> 
> Johannes

Thanks but I have the same error. I want to use default\_mgservice.name when mgservices is not defined.

I find this workarround :  
{% if instance.mgservices is defined %}  
pgservice : {{ instance.mgservices.name | default(default\_mgservice.name) }}  
{% else %}  
pgservice : {{ default\_mgservice.name }}  
{% endif %}

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [February 8, 2017, 11:42am UTC](https://forum.ansible.com/t/default-for-undefined-dict/23682/5 "2017-02-08T11:42:15Z")

</div>

Hi,

Try with this:  
{% for instance in instances %}  
pgservice : {{ (instance['mgservices'] | default(default\_mgservice))['name'] }}  
{% endfor %}

Cheers,  
Marko  
CONFIDENTIALITY NOTICE: This message is the property of International Game Technology PLC and/or its subsidiaries and may contain proprietary, confidential or trade secret information. This message is intended solely for the use of the addressee. If you are not the intended recipient and have received this message in error, please delete this message from your system. Any unauthorized reading, distribution, copying, or other use of this message or its attachments is strictly prohibited.
