Ansible escaping double quotes and single quotes

Georg Gutsche picture Georg Gutsche · Oct 7, 2015 · Viewed 18.7k times · Source

I would like insert a nrpe command in the nrpe.cfg with this ansible command

check_tomcat_threads.pl -H localhost -p 30011 -C '"http-bio-30011"' -w 200 -c 50

But the the problem are the '" and "'

To set this line in the nrpe.cfg use the command

 - { regexp: '^command\[SERVICE_tomcat_pi_Threads\]', line: "command[SERVICE_tomcat_Threads_pi]=/appiu/monitoring/check_tomcat_threads.pl -H localhost -p 30011 -C '\"http-bio-30011\"' -w 200 -c 50" }

but the result in the nrpe.cfg is

...-C http-bio-30011..

If I use ''\"http-bio-30011\"'' in the ansible script

the result in the nrpe.cfg is

...-C "http-bio-30011"... 

How I can escaping the single quotes and the double quotes to get this -C '"http-bio-30011"'?

Greetings Georg

Answer

Yaroslav Admin picture Yaroslav Admin · Oct 8, 2015

It's a bug in lineinfile module.

The correct syntax from YAML point of view is following (but it will work, only when bug is fixed). You should escape only two characters " and \ in double-quoted literals:

line: "command[SERVICE_tomcat_Threads_pi]=/appiu/monitoring/check_tomcat_threads.pl -H localhost -p 30011 -C '\"http-bio-30011\"' -w 200 -c 50"

To temporarily workaround it, you can use:

line: 'command[SERVICE_tomcat_Threads_pi]=/appiu/monitoring/check_tomcat_threads.pl -H localhost -p 30011 -C \''"http-bio-30011"\'' -w 200 -c 50'