# changing Jinja2's delimiters from Ansible, or within Jinja2?

**URL:** <https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077>\
**Category:** Ansible Project\
**Created:** [November 11, 2012, 11:59am UTC](https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077 "2012-11-11T11:59:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@Nick](https://forum.ansible.com/u/Nick)\
**Post date:** [November 11, 2012, 11:59am UTC](https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077/1 "2012-11-11T11:59:03Z")

</div>

Hi,

I’m attempting to use Ansible to install Exim configs using the template module, which is problematic since Exim uses both dollars and curly braces as part of its config syntax.

The only workaround I’ve found is to wrap things in the {% raw %} … {% endraw %} tags, which is clumsy. From reading around it sounds like it is possible to change the delimiter but only at the Python API level.

Assuming I need to template the configs, is there some way of stopping Jinja2 from trying to expand {{ … }} and ${ … } in selected templates?

Thanks,

N

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 11, 2012, 2:42pm UTC](https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077/2 "2012-11-11T14:42:36Z")

</div>

I would not be opposed to a patch that sets something like this at the  
very top line of the template

# delimiter={@ basic\_replace=False

??

---

<div class="post-metadata">

**Author:** ![Nick](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@Nick](https://forum.ansible.com/u/Nick)\
**Post date:** [November 12, 2012, 1:06pm UTC](https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077/3 "2012-11-12T13:06:06Z")

</div>

Sounds good, and I'd like to help out but unfortunately I'm not well tooled to create such a patch, not having much Python hacking experience so far. Pointers where to look would help to encourage me to try fixing this.

Cheers,

N

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [November 13, 2012, 4:32pm UTC](https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077/4 "2012-11-13T16:32:08Z")

</div>

In the templates module, when initializing the jinja2 environment you can pass optional start/stop delimiters:

environment = jinja2.Environment(loader=loader, trim\_blocks=True,block\_start\_string=‘@@’,block\_end\_string=‘@@’,variable\_start\_string=‘@=’, variable\_end\_string=‘=@’)

would change {% statement %} to @@ statement @@  
and {{ var }} into @= var =@.

Make it a config option and only pass if set, otherwise jinja will use it’s own defaults.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 13, 2012, 4:38pm UTC](https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077/5 "2012-11-13T16:38:54Z")

</div>

the trick about making it a config option would make it that way for  
ALL templates, so I was proposing we have some way to include that  
info commented out at the top of the template, almost like a shebang  
interpeter line

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [November 13, 2012, 4:42pm UTC](https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077/6 "2012-11-13T16:42:35Z")

</div>

yes, making it a template module parameter does seem like a smaller change.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 13, 2012, 4:43pm UTC](https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077/7 "2012-11-13T16:43:31Z")

</div>

yeah, but not as a module parameter you'd have to put in the playbook,  
but something you'd just put at the top of the template.

that way the choice can travel with the template itself

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [November 13, 2012, 4:55pm UTC](https://forum.ansible.com/t/changing-jinja2s-delimiters-from-ansible-or-within-jinja2/12077/8 "2012-11-13T16:55:01Z")

</div>

I was trying to avoid either opening the file before calling jinja2 or forcing jinja2 to reevaluate it’s own environment and reprocess the current template, but it does give you the most ‘flexible’ interface for the person writing the template.

It should not be too hard to write, just need some time.
