Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I escape double curly braces in jinja2?

Tags:

ansible

jinja2

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?

like image 879
gr0gu3 Avatar asked Apr 01 '19 21:04

gr0gu3


People also ask

How do you escape Jinja template?

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.

How do you get out of curly braces in Yaml?

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.

What does double curly braces mean?

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.


1 Answers

{% raw %}{{ databasehost }}{% endraw %} should work.

You can also use {{ '{{ databasehost }}' }} as an alternative.

like image 163
blhsing Avatar answered Sep 20 '22 15:09

blhsing