Single quotes in Ansible shell module

4,542 views
Skip to first unread message

Chris D

unread,
Apr 20, 2016, 3:21:22 PM4/20/16
to Ansible Project
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 :(

Aleksey Tsalolikhin

unread,
Apr 20, 2016, 6:07:55 PM4/20/16
to ansible...@googlegroups.com

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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/97962ea8-9fd7-49c0-b3f8-c230c4e1670a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Apr 23, 2016, 5:18:58 PM4/23/16
to ansible...@googlegroups.com
On 19. april 2016 23:09, Chris D wrote:
> 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

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"


--
Kai Stian Olstad

Johannes Kastl

unread,
Apr 24, 2016, 9:36:53 AM4/24/16
to ansible...@googlegroups.com
On 23.04.16 23:18 Kai Stian Olstad wrote:

> 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"

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

signature.asc

Arvind Ks

unread,
Mar 23, 2017, 6:27:20 PM3/23/17
to Ansible Project
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"}'

Kai Stian Olstad

unread,
Mar 24, 2017, 6:53:29 AM3/24/17
to ansible...@googlegroups.com
On 23.03.2017 23:22, Arvind Ks wrote:
> 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"}'

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.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages