<p dir="auto">I believe there's an ansible parsing error in this shell task that then leads to a template error.</p>
<p dir="auto">Here's a sample playbook, test.yml, that I ran against a host with docker installed. <code class="notranslate">ansible-playbook test.yml -e hosts=dockerhost -v</code></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
---
# test.yml
- name: Simple test  
  hosts: &quot;{{ hosts }}&quot;
  gather_facts: no
  sudo: true
  tasks:
    - name: remove previous container if it exists
      command: docker rm -f busybox-container
      ignore_errors: yes
    - name: run simple docker container
      command: docker run -di --name=busybox-container busybox:latest
    - name: get image name
      shell: docker inspect --format=&quot;{{'{{.Config.Image}}'}}&quot; busybox-container
      #shell:  docker ps -a | grep busybox | awk '{print $2}'
      register: image_name
    - name: print debug
      debug: msg={{ image_name.stdout }}"><pre class="notranslate"><code class="notranslate">
---
# test.yml
- name: Simple test  
  hosts: "{{ hosts }}"
  gather_facts: no
  sudo: true
  tasks:
    - name: remove previous container if it exists
      command: docker rm -f busybox-container
      ignore_errors: yes
    - name: run simple docker container
      command: docker run -di --name=busybox-container busybox:latest
    - name: get image name
      shell: docker inspect --format="{{'{{.Config.Image}}'}}" busybox-container
      #shell:  docker ps -a | grep busybox | awk '{print $2}'
      register: image_name
    - name: print debug
      debug: msg={{ image_name.stdout }}
</code></pre></div>
<p dir="auto">Compare the output of the shell task "get image name" using <code class="notranslate">docker inspect</code> (which uses a go template similar to jinja2 syntax and therefore must be escaped) to the shell task that parses the same value through a series of shell commands. The stdout value is the same for each shell, but the debug task fails for <code class="notranslate">docker inpect</code> but not the other.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/253579/6517942/5edd8b70-c36e-11e4-89e6-0e06a90d3458.png"><img src="https://cloud.githubusercontent.com/assets/253579/6517942/5edd8b70-c36e-11e4-89e6-0e06a90d3458.png" alt="tmp" style="max-width: 100%;"></a></p>
<h5 dir="auto">Issue Type:</h5>
<p dir="auto">Bug Report</p>
<h5 dir="auto">Ansible Version:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 1.8.2
  configured module search path = /usr/share/ansible"><pre class="notranslate"><code class="notranslate">ansible 1.8.2
  configured module search path = /usr/share/ansible
</code></pre></div>
<h5 dir="auto">Environment:</h5>
<p dir="auto">Debian unstable</p>
<h5 dir="auto">Summary:</h5>
<p dir="auto">Trying to use the registed output of <code class="notranslate">docker inspect --format=... ...</code> results in a fatal error.</p>
<p dir="auto">Interestingly enough this works with Ansible 1.7.2 but not with 1.8.2.  Also works when just using the output of <code class="notranslate">docker inspect ...</code> without the <code class="notranslate">--format</code> option.</p>
<h5 dir="auto">Steps To Reproduce:</h5>
<p dir="auto">Use the following <code class="notranslate">playbook.yml</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- hosts: localhost
  connection: local
  tasks:
    ## I know this task returns the information we inpect below
    ## in ansible_facts.docker_containers[0].  But to uncover
    ## the bug `docker inspect --format=...` will be used.
    - name: start test container
      docker:
        image: busybox
        name: sleep
        command: sleep 10
        volumes:
          - /srv
        state: running
    - name: read image hash of container
      command: docker inspect sleep
      register: inspect
    - name: read image hash of container
      command: docker inspect --format='{{ &quot;{{&quot; }}index . &quot;Image&quot;{{ &quot;}}&quot; }}' sleep
      register: image
    - name: read name of container
      command: docker inspect --format='{{ &quot;{{&quot; }}.Name{{ &quot;}}&quot; }}' sleep
      register: name
    - name: read directory of container volume
      command: docker inspect --format='{{ &quot;{{&quot; }}index .Volumes &quot;/srv&quot;{{ &quot;}}&quot; }}' sleep
      register: volume
    ## We can't us with_items on the variable names since the
    ## encountered FATALs will end the loop prematurely.
    - debug:
        var: &quot;{{ i }}.stdout&quot;
    - name: clean up test container
      docker:
        image: busybox
        name: sleep
        state: absent"><pre class="notranslate"><code class="notranslate">- hosts: localhost
  connection: local
  tasks:
    ## I know this task returns the information we inpect below
    ## in ansible_facts.docker_containers[0].  But to uncover
    ## the bug `docker inspect --format=...` will be used.
    - name: start test container
      docker:
        image: busybox
        name: sleep
        command: sleep 10
        volumes:
          - /srv
        state: running
    - name: read image hash of container
      command: docker inspect sleep
      register: inspect
    - name: read image hash of container
      command: docker inspect --format='{{ "{{" }}index . "Image"{{ "}}" }}' sleep
      register: image
    - name: read name of container
      command: docker inspect --format='{{ "{{" }}.Name{{ "}}" }}' sleep
      register: name
    - name: read directory of container volume
      command: docker inspect --format='{{ "{{" }}index .Volumes "/srv"{{ "}}" }}' sleep
      register: volume
    ## We can't us with_items on the variable names since the
    ## encountered FATALs will end the loop prematurely.
    - debug:
        var: "{{ i }}.stdout"
    - name: clean up test container
      docker:
        image: busybox
        name: sleep
        state: absent
</code></pre></div>
<p dir="auto">Execute it on a host which has Docker installed and running:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="for i in inspect image name volume; do
    ansible-playbook -e i=$i playbook.yml
done"><pre class="notranslate"><code class="notranslate">for i in inspect image name volume; do
    ansible-playbook -e i=$i playbook.yml
done
</code></pre></div>
<h5 dir="auto">Expected Results:</h5>
<ul dir="auto">
<li><code class="notranslate">inspect.stdout</code> should contain the complete JSON output</li>
<li><code class="notranslate">image.stdout</code> should be the hash of the official <code class="notranslate">busybox</code> Docker image</li>
<li><code class="notranslate">name.stdout</code> should be <code class="notranslate">/sleep</code></li>
<li><code class="notranslate">volume.stdout</code> should be something like <code class="notranslate">/var/lib/docker/vfs/dir/92fd93ceedababafb4d61615749e093dd170cae4f2f1ec092fc11159b6b0b7c5</code></li>
</ul>
<h5 dir="auto">Actual Results:</h5>
<ul dir="auto">
<li><code class="notranslate">inspect.stdout</code> correctly contains the complete JSON output with</li>
<li>the debug task for <code class="notranslate">image.stdout</code> fails with: <code class="notranslate">fatal: [localhost] =&gt; template error while templating string: expected name or number</code></li>
<li>the debug task for <code class="notranslate">name.stdout</code> fails with: <code class="notranslate">fatal: [localhost] =&gt; template error while templating string: unexpected '.'</code></li>
<li>the debug task for <code class="notranslate">volume.stdout</code> fails with: <code class="notranslate">fatal: [localhost] =&gt; template error while templating string: expected token 'end of print statement', got 'string'</code></li>
</ul>