Escaping double curly braces in Ansible

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


New in Ansible 2.0 is the ability to declare a value as unsafe with the !unsafe tag.

In your example you could do:

- name: Test 
  shell: !unsafe "docker inspect --format '{{ .NetworkSettings.IPAddress }}' instance1"

See the docs for details.


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.


Tried on with ansible 2.1.1.0

{%raw%}...{%endraw%} block seems the clear way

- name: list container images and name date on the server
  shell: docker ps --format {%raw%}"{{.Image}} {{.Names}}"{%endraw%}

Only need to escape leading '{{'

tasks:
- name: list container images and names
  shell: docker ps --format "{{'{{'}}.Image}} {{'{{'}}.Names}}"

No harm to escap the tailing '}}', except more difficult to read.

tasks:
- name: list container images and names
  shell: docker ps --format "{{'{{'}}.Image{{'}}'}} {{'{{'}}.Names{{'}}'}}"

Backslash '\' seems do not work