Take for example a sample playbook.yml file. If you need to specify an inventory file and some variables, you can end up with:
ansible-playbook playbook.yml -i hosts -e var1=val1 -e var2=val2
And if you use vault it can be even longer:
ansible-playbook playbook.yml -i hosts -e var1=val1 -e var2=val2 –vault-pswd-file ~/vault-pass
These commands are pretty long, and kind of make you want to run them as seldom as you can. To avoid this unhelpful sentiment, I wrote ansible-exec – a wrapper around ansible-playbook.
With ansible-exec you can do these:
# Define variables like normal cli arguments ./playbook.yml -i hosts --var1=value --var2=value2 # Run localhost playbooks with an empty inventory ./localhost_playbook.yml --var1=value # Pass other ansible-playbook options after a double-dash ./playbook.yml -i hosts -- --tags myapp
Just like normal scripts. Less to remember, less to type and one more reason to prefer an Ansible playbook to a bash script.
To install ansible-exec:
git clone https://github.com/bigpandaio/ansible-exec cd ansible-exec make install
To use, put this shebang line as the first line in your playbook:
#!/usr/local/bin/ansible-exec
And chmod a+x.
The script itself sits in /usr/local/bin and has a small config file in /etc/ansible/ansible-exec.conf. The config file is there to change some default behaviors:
- By default, if no inventory file is specified via -i , ansible-exec will generate a dummy inventory. You can override this with the DEFAULT_INVENTORY variable.
- You can add other ansible-playbook options that should always be passed in the ANSIBLE_PLAYBOOK_ARGS array.
Enjoy!