When using Ansible with a dynamic inventory, you can end up typing a lot more:
EC2_INI_PATH=prod.ini ansible -i plugins/inventory/ec2.py -u ec2-user -s "*" -m ping
Unfortunately Ansible doesn't let you pass in a full configuration file at runtime, but I recently stumbled on a workaround for this: a shell script that sets relevant environment variables and then executes your command:
#!/bin/sh export EC2_INI_PATH="prod.ini" export ANSIBLE_HOSTS="plugins/inventory/ec2.py" export ANSIBLE_REMOTE_USER="ec2-user" #export ANSIBLE_SUDO="yes" exec "[email protected]"
Save this as an executable script and it can then be used like this:
./my_wrapper ansible "*" --list-hosts ./my_wrapper ansible "*" -a "whoami"
Rather neatly, the exported variables don't stick around after the script - check with
env | grep ANSIBLE. You can even create multiple wrappers for different environments:
./staging "*" -s -a "service httpd graceful" ./prod "*" -a "uptime"
The relevant constants for overriding config and parameters can vary a bit from what you'd expect, but you can find them in lib/ansible/constants.py.
Subscribe to Ross's codelog
Get the latest posts delivered right to your inbox