# Ansible template not substituting if any variable not defined

**URL:** <https://forum.ansible.com/t/ansible-template-not-substituting-if-any-variable-not-defined/24007>\
**Category:** Ansible Project\
**Created:** [March 18, 2017, 5:50pm UTC](https://forum.ansible.com/t/ansible-template-not-substituting-if-any-variable-not-defined/24007 "2017-03-18T17:50:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Gateley](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John\_Gateley](https://forum.ansible.com/u/John_Gateley)\
**Post date:** [March 18, 2017, 5:50pm UTC](https://forum.ansible.com/t/ansible-template-not-substituting-if-any-variable-not-defined/24007/1 "2017-03-18T17:50:02Z")

</div>

HI y’all,

I switched to a new computer, so this is a clean installation of ansible etc.  
One of my playbooks is not working properly. I tracked it down to using a template and one of the variables isn’t defined.  
Here’s the scenario:

role/tasks/main.yml:

---

<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:** [March 20, 2017, 4:50pm UTC](https://forum.ansible.com/t/ansible-template-not-substituting-if-any-variable-not-defined/24007/2 "2017-03-20T16:50:19Z")

</div>

you can set:

error\_on\_undefined\_vars = False

in ansible.cfg, but this will also skip any errors in templating in plays.

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 20, 2017, 5:27pm UTC](https://forum.ansible.com/t/ansible-template-not-substituting-if-any-variable-not-defined/24007/3 "2017-03-20T17:27:22Z")

</div>

Use defaults, to handle undefined variables without ignoring errors:

ServerAlias: {{ foo | default(‘bar’) }}

---

<div class="post-metadata">

**Author:** ![John\_Gateley](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@John\_Gateley](https://forum.ansible.com/u/John_Gateley)\
**Post date:** [March 21, 2017, 3:07am UTC](https://forum.ansible.com/t/ansible-template-not-substituting-if-any-variable-not-defined/24007/4 "2017-03-21T03:07:19Z")

</div>

Thank you Brian and Arthur.

I already had the error\_on\_undefined\_vars set to False.

Based on Brian’s comment I am going to switch to not having undefined variables - tlooks like my original approach is a code-smell.

However, I still believe there is a bug here, probably introduced in a recent version of Ansible… My current version is:

ansible-playbook 2.2.1.0

My previous version is unknown, but probably a year and a half old.

The bug is that if a variable is undefined for a template, no substitutions at all are occurring, leaving the bracket notation in place. In the old version, substitutions for defined variables would occur, and for undefined variables they would be replaced with the empty string.

Thanks again,

John
