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?

  • Docker swarm-manager displays old container information
  • Running docker container in Jenkins container, How can I set the volume from host?
  • Docker on CentOS report “unable to mount sys as readonly” when run
  • Autoscale volume and pods simultaneously (Kubernetes)
  • Link php docker to host mysql
  • How do I lookup Docker service member addresses using DNS?
  • - name: Test 
      shell: "docker inspect --format '{{ .NetworkSettings.IPAddress }}' instance1"

  • Best way for debugging docker build error
  • How to add new custom command to docker API?
  • How to get docker exec stdout to be as verbose as running command in container?
  • Access Windows 2016 Server Container (Docker container) via GUI?
  • Can docker application use dependent images?
  • Create image-stream on image from private registry on OpenShift
  • 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.