jinja2 template default not working when variable is not defined

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}

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.

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') }}

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' }}