Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to use inventory variables in ad-hoc commands

Tags:

ansible

I want to run an ad-hoc command over a host file, in the host file I have defined a variable for each host, How can I use that variable while executing an ad-hoc command.

For example:

ansible -i /home/bob/hosts_file -m shell -a "$VAR/project run"

I have defined the $VAR for each host in "hosts_file", the $VAR is different for every host in the inventory file. How can I use that variable in my ad-hoc command replacing for each host when executing.

like image 781
Snort5121 Avatar asked Jul 12 '17 23:07

Snort5121


Video Answer


1 Answers

Host variables are available to ansible, even while running an ad-hoc command. You insert them as you would through a playbook, using a jinja template.

ansible all -i /home/bob/hosts_file -m shell -a "{{var}}/project run"
like image 163
Kevin M Granger Avatar answered Oct 06 '22 17:10

Kevin M Granger