# template substitution

**URL:** <https://forum.ansible.com/t/template-substitution/21623>\
**Category:** Ansible Project\
**Created:** [June 1, 2016, 5:23am UTC](https://forum.ansible.com/t/template-substitution/21623 "2016-06-01T05:23:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Gray](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@Peter\_Gray](https://forum.ansible.com/u/Peter_Gray)\
**Post date:** [June 1, 2016, 5:23am UTC](https://forum.ansible.com/t/template-substitution/21623/1 "2016-06-01T05:23:10Z")

</div>

If I have in a template file

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [June 1, 2016, 1:01pm UTC](https://forum.ansible.com/t/template-substitution/21623/2 "2016-06-01T13:01:23Z")

</div>

I normally write it like this:

{% if i\_want\_yes %}  
# yes  
{% else %}  
# no  
{% endif %}

Would that work for you?

Johannes

---

<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:** [June 1, 2016, 1:12pm UTC](https://forum.ansible.com/t/template-substitution/21623/3 "2016-06-01T13:12:52Z")

</div>

ansible uses the trim\_blocks setting when invoking jinja, by default its ‘yes’.

ansible allows for overrides by creating a special header:

```auto
#jinja2: trim_blocks: False
#
# {% if i_want_yes %} yes {% else %} no {% endif %}
#

```

---

<div class="post-metadata">

**Author:** ![Peter\_Gray](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@Peter\_Gray](https://forum.ansible.com/u/Peter_Gray)\
**Post date:** [June 1, 2016, 10:46pm UTC](https://forum.ansible.com/t/template-substitution/21623/4 "2016-06-01T22:46:40Z")

</div>

Many thanks for this information. For future reference where is this documented?

The interaction of ansible with jinga2 seems a bit opaque.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [June 2, 2016, 4:01am UTC](https://forum.ansible.com/t/template-substitution/21623/5 "2016-06-02T04:01:28Z")

</div>

> Many thanks for this information. For future reference where is this  
> documented?

[https://groups.google.com/forum/#!topic/ansible-project/qHyIjyal\_Ic](https://groups.google.com/forum/#!topic/ansible-project/qHyIjyal_Ic) is a  
thread where it came up before, which says

&nbsp;&nbsp;you can use a per template header to override this, you can see an  
&nbsp;&nbsp;example in the template docs.

&nbsp;&nbsp;[https://docs.ansible.com/template\_module.html](https://docs.ansible.com/template_module.html)

which is now at [http://docs.ansible.com/ansible/template\_module.html](http://docs.ansible.com/ansible/template_module.html), and  
which only seems to mention it in a note saying

&nbsp;&nbsp;Since Ansible version 0.9, templates are loaded with trim\_blocks=True.

and not any actual examples, or any explanation of what trim\_blocks does,  
so it could be hard to spot this if you didn't already know what you were  
looking for.

> The interaction of ansible with jinga2 seems a bit opaque.

That page also doesn't mention  
[http://docs.ansible.com/ansible/playbooks\_filters.html](http://docs.ansible.com/ansible/playbooks_filters.html), which I've found  
to be super useful in turning up examples of Ansible-specific Jinja filters.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
