Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running sub shell command in Ansible ad-hoc

Tags:

bash

ansible

I would like to run a sub shell command in an ad-hoc Ansible command.

Here is what I want to do :

sudo ansible myservers -m shell -a "touch /var/tmp/$(uname -n)"

It creates the remote file but with the name of the local host, it doesn't execute the uname command on remote servers.

like image 681
tonio94 Avatar asked Jan 23 '18 14:01

tonio94


1 Answers

I found the solution :

sudo ansible myservers -m shell -a '/bin/bash -c "toto=`uname -n` ; touch /var/tmp/\$toto.json;"'

Seems that I have to start a shell to execute sub shell commands, but it works.

like image 78
tonio94 Avatar answered Oct 06 '22 01:10

tonio94



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!