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?

  • How to Dockerize a PHP app
  • How to Give Docker Containers more resources
  • No response from Firebase in Docker
  • docker bad mount driver
  • What is image sprawl in docker and how to avoid it?
  • Error checking context is accessible: 'can't stat '.gvfs''. Please check permissions and try again
  • - name: Test 
      shell: "docker inspect --format '{{ .NetworkSettings.IPAddress }}' instance1"

  • Why am i getting an error while trying to install ruby on Dockerfile?
  • Are there known security risks in running docker with user namespace disabled?
  • Accessing logs folder of running docker container
  • How to build a sidecar container for passing files from a machine outside of the Kubernetes cluster?
  • Service name for Docker Compose remote interpreter in PyCharm 5.1 Beta 2
  • How to set Zookeeper dataDir in Docker (fig.yml)
  • 5 Solutions collect form web for “Escaping double curly braces in Ansible”

    Here is a mostly clean and Ansible native workaround not depending on docker --inspect with curly braces. We assume we have just referenced one container with the Ansible docker module before:

    - name: query IP of client container
      shell: "docker exec {{ docker_containers[0].Id }} hostname -I"
      register: _container_query
    - name: get IP of query result
        _container_ip: "{{ _container_query.stdout | regex_replace('\\s','') }}"

    You now have the IP of the Docker container in the Variable _container_ip. I also published this workaround on my article The Marriage of Ansible with Docker.

    [Update 2015-11-03] Removed whitespaces of the stdout of the container query.

    [Update 2015-11-04] BTW, there were two pull requests in the official Ansible repository, that would made this workaround needless by recovering the facts returned by the Docker module. So you could acces the IP of a docker container via docker_containers[0].NetworkSettings.IPAddress. So please vote for those pull requests:

    • fixed broken facts #1457
    • docker module: fix regressions introduced by f38186c and 80aca4b #2093

    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.


    - 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

    I managed to work around my issue using a small script:

    #!/usr/bin/env bash
    docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$1"

    And the following Ansible play

    - copy:
        src: files/
        dest: /usr/local/bin/
        owner: root
        group: root
        mode: 0770
    - shell: "/usr/local/bin/ {{ SWIFT_ACCOUNT_HOSTNAME }}"
      register: swift_account_info

    Nevertheless, it’s very surprising that Ansible doesn’t allow escaping double curly braces!

    Tried on with ansible

    {%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 ‘{{‘

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

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

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

    Backslash ‘\’ seems do not work

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.