Need help with escaping a jinja-sequence in bash script

2,405 views
Skip to first unread message

Simon Szustkowski

unread,
May 18, 2016, 9:43:13 AM5/18/16
to Ansible Project
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. 

Johannes Kastl

unread,
May 18, 2016, 1:43:05 PM5/18/16
to ansible...@googlegroups.com
On 18.05.16 15:43 Simon Szustkowski wrote:

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

I found this:
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

signature.asc

Simon Szustkowski

unread,
May 18, 2016, 1:56:47 PM5/18/16
to Ansible Project


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


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

Simon Szustkowski

unread,
May 19, 2016, 7:23:40 AM5/19/16
to Ansible Project
Yes, this works. 
Reply all
Reply to author
Forward
0 new messages