1

I am using Ansible to query a bunch of Linux servers, to see what version of Docker is installed.

My simple playbook looks like this:

---
- name: Query Docker
  any_errors_fatal: false
  hosts: all
  gather_facts: false

  tasks:
    - name: Query Docker version - Debian/Ubuntu
      ansible.builtin.command: docker version --format '{{.Server.Version}}'
      become: no
      register: result

    - name: Output Debian/Ubuntu results
      ansible.builtin.debug:
        var: result
        verbosity: 1

Running that docker command on the command line gives me the value I need, for example, something like "20.10.14" or "20.10.7".

When I try passing that command using ansible.builtin.command module, the templating breaks, with the error message:

template error while templating string: unexpected '.'. String: docker version --format '{{.Server.Version}}'

Note: I realize I can also get this information by using the community.docker.docker_host_info module, and that's actually what I am doing to accomplish my task, but I would still like to know how to pass a Go template string, with double curly-brackets, correctly quoted to an Ansible command.

EDIT: This was closed as a duplicate of another question, but I am going to leave it here in case it helps anyone else find the answer to a similar issue.

The fix was to put brackets around the text, like this:

---
- name: Query Docker
  any_errors_fatal: false
  hosts: all
  gather_facts: false

  tasks:
    - name: Query Docker version - Debian/Ubuntu
      ansible.builtin.command: docker version --format {{'{{.Server.Version}}'}}
      become: no
      register: result

    - name: Output Debian/Ubuntu results
      ansible.builtin.debug:
        var: result
        verbosity: 1
m0j0
  • 3,484
  • 5
  • 28
  • 33
  • you have a typo with dot before Server.Version?, this is what the error says... – Frenchy Apr 27 '22 at 17:55
  • No, it is not a typo. Running that command at a shell prompt gives the Docker version, such as 20.10.14. – m0j0 May 12 '22 at 16:10

0 Answers0