# Single quotes in Ansible shell module

**URL:** <https://forum.ansible.com/t/single-quotes-in-ansible-shell-module/21209>\
**Category:** Ansible Project\
**Created:** [April 19, 2016, 9:09pm UTC](https://forum.ansible.com/t/single-quotes-in-ansible-shell-module/21209 "2016-04-19T21:09:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_D](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@Chris\_D](https://forum.ansible.com/u/Chris_D)\
**Post date:** [April 19, 2016, 9:09pm UTC](https://forum.ansible.com/t/single-quotes-in-ansible-shell-module/21209/1 "2016-04-19T21:09:37Z")

</div>

I’m trying to run a command using the Ansible shell module, but having some trouble. Basically, with this command, I want to list all postgres databases on a server, then run the psql -c ‘\dx’ command against them to find out what extensions are installed in each database in the cluster.

Unfortunately, I haven’t been able to find the right combination of quotes/single quotes to get the command to run successfully.

Example: host1 has 3 postgres databases on it: db1, db2, and db3

ansible ‘host1’ -m shell -a 'for database in $(psql -l | grep ‘^ db’ | awk ‘{print $1}’); do psql -c ‘\dx’; done

I’ve tried enclosing the whole ‘for… done’ command in double-quotes, etc, but no luck ☹

---

<div class="post-metadata">

**Author:** ![Aleksey\_Tsalolikhin](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Aleksey\_Tsalolikhin](https://forum.ansible.com/u/Aleksey_Tsalolikhin)\
**Post date:** [April 20, 2016, 10:07pm UTC](https://forum.ansible.com/t/single-quotes-in-ansible-shell-module/21209/2 "2016-04-20T22:07:38Z")

</div>

I’ve run into this too.

My workaround is to use the script module instead of the shell module. Then you don’t have to fight with Ansible about quotes.

Added benefit: you can put your script under Version Control.

ansible host1 -m script -a example.sh

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 23, 2016, 9:18pm UTC](https://forum.ansible.com/t/single-quotes-in-ansible-shell-module/21209/3 "2016-04-23T21:18:48Z")

</div>

This is not tested, but should work:

ansible 'host1' -m shell -a "for database in \$(psql -l | grep '^ db' | awk '{print \$1}'); do psql -c \"\dx \$database\"; done"

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [April 24, 2016, 1:36pm UTC](https://forum.ansible.com/t/single-quotes-in-ansible-shell-module/21209/4 "2016-04-24T13:36:36Z")

</div>

When I read the OP I was not sure, if the problem are the quotes or  
the backslash inside before the dx, that has to be escaped...

Johannes

---

<div class="post-metadata">

**Author:** ![Arvind\_Ks](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Arvind\_Ks](https://forum.ansible.com/u/Arvind_Ks)\
**Post date:** [March 23, 2017, 10:22pm UTC](https://forum.ansible.com/t/single-quotes-in-ansible-shell-module/21209/5 "2017-03-23T22:22:51Z")

</div>

I need to run this command. How do i escape the commands.

find / -xdev ( -perm -4000 -o -perm -2000 ) -type f | awk ‘{print “-a always,exit -F path=” $1 " -F perm=x -F auid\>=500 -F auid!=4294967295 -k privileged"}’

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 24, 2017, 10:53am UTC](https://forum.ansible.com/t/single-quotes-in-ansible-shell-module/21209/6 "2017-03-24T10:53:15Z")

</div>

It usually involves trial and error until it works.  
Escaping things Ansible would expand and alternating single and double quotes.

I recommend the easier way, make a script of the command and use the Ansible Script module to run it, then you don't have to escape anything.
