I need to escape double curly braces in a code I'm working on using Ansible. The thing is I have all those parameters that needs to be transformed in variables. Basically I'm working on a template creator.
I've tried using {% raw %}{{ name-of-variable }}{% endraw %} but it did not worked. When I tried /{/{ name-of-variable }} I almost got it, but I am trying to get rid of the backslashes too.
Here's a bit of the code:
local_action:
module: replace
path: "/tmp/{{ ambiance }}/{{ seed }}DEFAULT.j2"
regexp: "{{ item.regexp1 }}"
replace: "{{ item.replace }}"
with_items:
- { regexp1: '^DBHOST.*$', replace: 'DBHOST = {% raw %}{{ databasehost }}{% endraw %}' }
- { regexp1: '^GLOBALHOST.*$', replace: 'GLOBALHOST = {% raw %}{{ global_hostname }}{% endraw %}' }
I expect the result as it follows:
DBHOST = {{ satabasehost }}
GLOBALHOST = {{ global_hostname }}
Any suggestions/ideas?
To escape jinja2 syntax in a jinja2 template with Python Flask, we can put render the template code without interpretation by putting the code in the {% raw %} block.
David Ward. Ok the solution is to double quote the whole line. This means using a backslash to escape any double quotes in the line.
Creating and executing a simple template Handlebars expressions are put into double curly braces {{expr}} for HTML-escaped content; otherwise, use triple curly brackets {{{expr}}} to avoid HTML-escaping.
{% raw %}{{ databasehost }}{% endraw %}
should work.
You can also use {{ '{{ databasehost }}' }}
as an alternative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With