# jinja2 template default not working when variable is not defined

**URL:** <https://forum.ansible.com/t/jinja2-template-default-not-working-when-variable-is-not-defined/20116>\
**Category:** Ansible Project\
**Created:** [January 4, 2016, 1:45pm UTC](https://forum.ansible.com/t/jinja2-template-default-not-working-when-variable-is-not-defined/20116 "2016-01-04T13:45:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jepper](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jepper](https://forum.ansible.com/u/jepper)\
**Post date:** [January 4, 2016, 1:45pm UTC](https://forum.ansible.com/t/jinja2-template-default-not-working-when-variable-is-not-defined/20116/1 "2016-01-04T13:45:39Z")

</div>

Hello all, please can someone help me explain why this jinja2 template fail if I have not defined wildfly\_memory\_settings ?

JAVA\_OPTS=“-Xms{{ wildfly\_memory\_settings.Xms | default(‘64m’) }} -Xmx{{ wildfly\_memory\_settings.Xmx | default(‘512m’) }} -XX:MaxPermSize=256m -verbose:gc -Djava.net.preferIPv4Stack=true”

The error: fatal: [targetbox] =\> {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘wildfly\_memory\_settings’ is undefined”, ‘failed’: True}

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [January 4, 2016, 1:52pm UTC](https://forum.ansible.com/t/jinja2-template-default-not-working-when-variable-is-not-defined/20116/2 "2016-01-04T13:52:16Z")

</div>

Hi,

it is because to test wildfly\_memory\_settings.Xms you need to access wildfly\_memory\_settings first and it is not defined. Unfortunatellz zou cannot use defautl this way.

---

<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:** [January 4, 2016, 2:00pm UTC](https://forum.ansible.com/t/jinja2-template-default-not-working-when-variable-is-not-defined/20116/3 "2016-01-04T14:00:01Z")

</div>

default only works on the 'last' var, so if you have to default the  
main var also if that can be undefined.

{{ (wildfly\_memory\_settings|default({})).Xms | default('64m') }}

---

<div class="post-metadata">

**Author:** ![rektide](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@rektide](https://forum.ansible.com/u/rektide)\
**Post date:** [January 4, 2016, 10:00pm UTC](https://forum.ansible.com/t/jinja2-template-default-not-working-when-variable-is-not-defined/20116/4 "2016-01-04T22:00:17Z")

</div>

This might work?

-Xms{{ (wildfly\_memory\_settings|default({'Xms': '64m'})).Xms }}

And if not, this ought:

-Xms{{ wildfly\_memory\_settings.Xms if wildfly\_memory\_settings is defined  
else '64m' }}
