Ansible command quotes

14,988

Solution 1

You can escape them with \”

example: "hello=\"hi\""

Solution 2

You can simply use the YAML literal block string syntax. In that way you don't need to escape any quotes. Instead, you can pass your shell command as is.

Example:

- name: test task
  shell:
      cmd: |
          cut -f 1 -d: /etc/passwd | xargs -n 1 -I {} bash -c ' echo -e "\n{}" ; chage -l {}'
  tags: test
Share:
14,988
Romain
Author by

Romain

For fun: Python automation

Updated on June 04, 2022

Comments

  • Romain
    Romain almost 2 years

    I would like to have the following command integrated in an Ansible playbook task:

    cut -f 1 -d: /etc/passwd | xargs -n 1 -I {} bash -c ' echo -e "\n{}" ; chage -l {}'.

    Any quote inside breaks the whole command. How I can avoid it to make it run the whole string?

    Many thanks in advance.