Last Updated: June 07, 2019
·
686
· Lars Van Casteren

Execute a command that needs escaping through Ansible on a group

I've got several docker containers that are running nginx instances with self-signed SSL certificates that are not exposed to the outside world. This snippet gets the container ip on the docker host and uses openssl to read the expiration date on the certificates. It's wrapped in Ansible so it can be executed on large number of hosts in a group but that means you need to escape the single quotes in the remote command.

ansible -i hosts/ <GROUP_NAME> \
-m shell -a \
'echo | openssl s_client -connect `sudo docker ps | grep nginx | awk {'\''print $14'\''} | awk -F: {'\''print $1'\''}`:443 2>/dev/null | openssl x509 -noout -dates'

The output is:

server.wherever.com | SUCCESS | rc=0 >>. 
notBefore=May 25 00:22:09 2018 GMT  
notAfter=Aug 23 00:22:09 2018 GMT