Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible command quotes

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.

like image 318
Romain Avatar asked Jul 12 '26 01:07

Romain


2 Answers

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
like image 86
maxschlepzig Avatar answered Jul 15 '26 15:07

maxschlepzig


You can escape them with \”

example: "hello=\"hi\""

like image 37
imjoseangel Avatar answered Jul 15 '26 16:07

imjoseangel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!