Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping double curly braces in Ansible

How to escape double curly braces in Ansible 1.9.2?

For instance, how can I escape double curly braces in the following shell command?

- name: Test    shell: "docker inspect --format '{{ .NetworkSettings.IPAddress }}' instance1" 
like image 696
Davide Guerri Avatar asked Aug 28 '15 20:08

Davide Guerri


People also ask

How do you escape the special characters in Ansible?

Yes, you need to escape '$' sign with '\', and it's executed in the server, it just won't show in the resulting output. Because in some cases like 'awk' with 'print' not working with Ansible ad-hoc command and need to utilize playbooks. It will spit out the result you want.

How do you get out of curly braces in Jinja2?

{% raw %}{{ databasehost }}{% endraw %} should work. You can also use {{ '{{ databasehost }}' }} as an alternative.

What is double curly brackets in Yaml?

It says that double curly braces {{ variable }} are used to evaluate expressions. Whilst distinguishing a single curly braces (after a colon), that is used to declare a dictionary.

What does double curly braces mean in Python?

The curly braces are part of Django Template Language. The part encapsulated between double curly braces {{ }} is nothing but a variable. That's how DTL, Jinja2 and other template languages work. They have their own set of rules which translates the template in to python and later to HTML code.


2 Answers

Whenever you have problems with conflicting characters in Ansible, a rule of thumb is to output them as a string in a Jinja expression.

So instead of {{ you would use {{ '{{' }}:

- debug: msg="docker inspect --format '{{ '{{' }} .NetworkSettings.IPAddress {{ '}}' }}' instance1" 

Topic "Escaping" in the Jinja2 docs.

like image 113
udondan Avatar answered Oct 11 '22 11:10

udondan


This:

- name: Test    shell: "docker inspect --format {% raw %}'{{ .NetworkSettings.IPAddress }}' {% endraw %} instance1" 

Should work

Another way to do is using backslashes like \{\{ .NetworkSettings.IPAddress \}\}

Hope it helps

like image 39
Filipe Avatar answered Oct 11 '22 11:10

Filipe