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"
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.
{% raw %}{{ databasehost }}{% endraw %} should work. You can also use {{ '{{ databasehost }}' }} as an alternative.
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.
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.
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.
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
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