# Ansible module "template" support for Jinja2 new class "ChainableUndefined"

**URL:** <https://forum.ansible.com/t/ansible-module-template-support-for-jinja2-new-class-chainableundefined/33378>\
**Category:** Ansible Project\
**Created:** [July 12, 2020, 9:43am UTC](https://forum.ansible.com/t/ansible-module-template-support-for-jinja2-new-class-chainableundefined/33378 "2020-07-12T09:43:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baeschtu](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Baeschtu](https://forum.ansible.com/u/Baeschtu)\
**Post date:** [July 12, 2020, 9:43am UTC](https://forum.ansible.com/t/ansible-module-template-support-for-jinja2-new-class-chainableundefined/33378/1 "2020-07-12T09:43:28Z")

</div>

According to the release notes of Jinja2 Version 2.11.0 (released on 2020-01-27) there is a new class named “ChainableUndefined” to handle undefined variables on multiple levels in Jinja2 templates. Currently, I use a ansible task using the module “template” to render jinja2 templates. However, I don’t know how to pass this new “ChainableUndefined” class to the parameter “undefined” in the Jinja2 environment via the ansible module.

Does anyone know how to set the parameter “undefined” to ChainableUndefined through the ansible “template” module?  
Does the module “template” only support the “StrictUndefined” class, currently?

---

<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:** [July 13, 2020, 1:56pm UTC](https://forum.ansible.com/t/ansible-module-template-support-for-jinja2-new-class-chainableundefined/33378/2 "2020-07-13T13:56:55Z")

</div>

Ansible actually has a feature that predates the jinja2 change that works exactly the same. We already have an AnsibleUndefined that performs the exact same functionality: [https://github.com/ansible/ansible/blob/devel/lib/ansible/template/ **init**.py#L274](https://github.com/ansible/ansible/blob/devel/lib/ansible/template/ __init__.py#L274)

This functionality has been in place since Ansible 2.8.

Due to the above, you need to do nothing, and you already get this functionality in Ansible

---

<div class="post-metadata">

**Author:** ![Baeschtu](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Baeschtu](https://forum.ansible.com/u/Baeschtu)\
**Post date:** [July 14, 2020, 8:16pm UTC](https://forum.ansible.com/t/ansible-module-template-support-for-jinja2-new-class-chainableundefined/33378/3 "2020-07-14T20:16:16Z")

</div>

Thanks for this reply. To simplify tests for existing variables I now use builtin jinja2 filter “_default()_” such as “_{% for person in persons | default(‘’) %}_” to do not first have to test if the variable “_persons_” is defined. If I remove the part " _| default(‘’)_" from the for-loop and the variable is not defined ansible will raise an error.

---

<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:** [July 14, 2020, 8:18pm UTC](https://forum.ansible.com/t/ansible-module-template-support-for-jinja2-new-class-chainableundefined/33378/4 "2020-07-14T20:18:47Z")

</div>

This is the expected behavior, and ChainableUndefined doesn’t change that. `AnsibleUndefined` inherits from `StrictUndefined`, and in the end, you cannot simply loop over undefined values. Both ChainableUndefined and AnsibleUndefined allow something like this to work:

foo.bar.baz|default(‘qux’)

Where `foo` is undefined. Prior to this you needed to insert `|default` at every level, instead of just at the end.
