kubernetesansible

How to properly escape symbols in ansible command?


I want to execute the command on each node:

docker ps --format '{{.Names}}' -a | egrep -v '^k8s.*$'

I tried millions of variants to execute command in ansible, including:

- hosts: kubernetes
  tasks:
    - name: check docker
      command:
        cmd: docker ps --format '{{.Names}}' -a | egrep -v '^k8s.*$'
      register: doc

    - debug: var=doc.stdout_lines

I tried to escape characters. But nothing works. So, how to make ansible execute the my docker command on each host?

PS I want to list containers that ain't controlled by k8s


Solution

  • You have several problems with your task:

    1. you are using the command module with shell syntax (pipelining multiple commands)

      The error in this case:

      unknown shorthand flag: 'v' in -v See 'docker ps --help'.

      As you can see, the pipe is ignored and the -v parameter is passed to the Docker command.

    2. double curly braces in a string. Double curly braces are interpreted as Jinja2 syntax in Ansible and attempts to templatize.

      The error in this case:

      template error while templating string: unexpected '.'.

    Solution for problem 1

    See below for examples.

    Solution for problem 2

    Final resulting task