Reputation: 329
I am trying to run a simple ad-hoc ansible command on various hosts to find out if a directory exists.
The following command works correctly by returning exists
although it does not print the environment variable beforehand:
ansible all -i hosts.list -m shell -a "if test -d /the/dir/; then echo '$HOSTNAME exists'; fi"
Can anyone please tell me why only exists
is returned instead of HOSTNAME exists
?
Upvotes: 0
Views: 835
Reputation: 311416
Because it's included in double quotes ("
), your $HOSTNAME
is being interpreted by your local shell. You probably want to write instead:
ansible all -i hosts.list -m shell -a \
'if test -d /the/dir/; then echo "$HOSTNAME exists"; fi'
In most cases you will want to use single quotes ('
) for your argument to -a
when you're using the shell
module to prevent your local shell from expanding variables, etc, that are intended to be expanded on the remote host.
Upvotes: 3