# Need help with escaping a jinja-sequence in bash script

**URL:** <https://forum.ansible.com/t/need-help-with-escaping-a-jinja-sequence-in-bash-script/21476>\
**Category:** Ansible Project\
**Created:** [May 18, 2016, 1:43pm UTC](https://forum.ansible.com/t/need-help-with-escaping-a-jinja-sequence-in-bash-script/21476 "2016-05-18T13:43:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Szustkowski](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@Simon\_Szustkowski](https://forum.ansible.com/u/Simon_Szustkowski)\
**Post date:** [May 18, 2016, 1:43pm UTC](https://forum.ansible.com/t/need-help-with-escaping-a-jinja-sequence-in-bash-script/21476/1 "2016-05-18T13:43:13Z")

</div>

Hi,

i want to create a template which should result in the following output:

1. custommark4 masq 1 PREROUTING inface “${openvpn\_if}”

Here, the value “openvpn” should be read from a variable. I have some problems with creating proper jinja2-Syntax because of the curly brackets which are already there.  
My first solution was just plain

`"${{{ item.name }}_if}"`

which resulted in a parsing error: AnsibleError: ERROR! template error while templating string: expected token ‘:’, got ‘}’"

So i consulted the jinja documentation which specified that i could just escape characters with ’  
The second attempt was:  
`"$'{'{{ item.name }}_if}"`

This was accepted by jinja, but resulted in “$'{'openvpn\_if}” (notice the ’ around the first curly bracket). This is no valid bash syntax.

So, how could i escape these brackets correctly?

…and for those wondering why i create bash scripts with ansible: I am managing a server side firewall with the great FireHOL-utility which is an abstraction layer for the iptables-command. And it’s config files are using plain bash syntax.

---

<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:** [May 18, 2016, 5:43pm UTC](https://forum.ansible.com/t/need-help-with-escaping-a-jinja-sequence-in-bash-script/21476/2 "2016-05-18T17:43:01Z")

</div>

I found this:  
[http://jinja.pocoo.org/docs/dev/templates/#escaping](http://jinja.pocoo.org/docs/dev/templates/#escaping)

Which says:  
\<quote\>  
The easiest way to output a literal variable delimiter ({{) is by  
using a variable expression:

{{ '{{' }}  
\</quote\>

So I would try this (untested):

"${{'{'}}{{ item.name }}\_if}"

Johannes

---

<div class="post-metadata">

**Author:** ![Simon\_Szustkowski](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@Simon\_Szustkowski](https://forum.ansible.com/u/Simon_Szustkowski)\
**Post date:** [May 18, 2016, 5:56pm UTC](https://forum.ansible.com/t/need-help-with-escaping-a-jinja-sequence-in-bash-script/21476/3 "2016-05-18T17:56:46Z")

</div>

> “${{‘{’}}{{ [item.name](http://item.name) }}\_if}”

Ah, stupid me. I should have read the documentation correctly. Will definitely try this, thanks.

---

<div class="post-metadata">

**Author:** ![Simon\_Szustkowski](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@Simon\_Szustkowski](https://forum.ansible.com/u/Simon_Szustkowski)\
**Post date:** [May 19, 2016, 11:23am UTC](https://forum.ansible.com/t/need-help-with-escaping-a-jinja-sequence-in-bash-script/21476/4 "2016-05-19T11:23:40Z")

</div>

Yes, this works.
