# Is there a way to used dot-nested vars with 'default' filter in jinja2 templates?

**URL:** <https://forum.ansible.com/t/is-there-a-way-to-used-dot-nested-vars-with-default-filter-in-jinja2-templates/15085>\
**Category:** Ansible Project\
**Created:** [April 24, 2014, 2:06pm UTC](https://forum.ansible.com/t/is-there-a-way-to-used-dot-nested-vars-with-default-filter-in-jinja2-templates/15085 "2014-04-24T14:06:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kesten\_Broughton2](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Kesten\_Broughton2](https://forum.ansible.com/u/Kesten_Broughton2)\
**Post date:** [April 24, 2014, 2:06pm UTC](https://forum.ansible.com/t/is-there-a-way-to-used-dot-nested-vars-with-default-filter-in-jinja2-templates/15085/1 "2014-04-24T14:06:13Z")

</div>

I have the need to template a yaml file using vars in another yaml file as  
input. There are often defaults.

I'd like to do this

in vars.yml  
parentvar:  
&nbsp;&nbsp;childvar:  
&nbsp;&nbsp;&nbsp;&nbsp;subvar1: val1  
# subvar2 is not even listed to keep user vars.yml file concise

template.yml  
{{ parentvar.childvar.subvar1 }}  
{{ parentvar.childvar.subvar2 | default("default for subvar2") }}

This fails with "dict object has no attribute 'subvar2'

It would work fine if i had  
vars.yml  
parentvar\_childvar\_subvar1: val1

and template.yml  
{{ parentvar\_childvar\_subvar1 }}  
{{ parentvar\_childvar\_subvar2 | default("default for subvar2") }}

I presume this is a problem at the jinja2 level?

I would like to avoid the '\_' flattened solution as it becomes pretty  
unreadable for large config files.  
Any suggestions on how to work around this?

thanks

Kesten Broughton  
512 701 4209

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [April 24, 2014, 2:25pm UTC](https://forum.ansible.com/t/is-there-a-way-to-used-dot-nested-vars-with-default-filter-in-jinja2-templates/15085/2 "2014-04-24T14:25:11Z")

</div>

Not positive if this will work, but try:

Excerpts from Kesten Broughton's message of 2014-04-24 10:06:13 -0400:

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 24, 2014, 2:33pm UTC](https://forum.ansible.com/t/is-there-a-way-to-used-dot-nested-vars-with-default-filter-in-jinja2-templates/15085/3 "2014-04-24T14:33:54Z")

</div>

It works best to use standard hash key access such as:

foo[‘bar’][‘baz’]|default(‘qux’)

Jinja2 seems to be able to handle it better.
