A cheat sheet for Ansible Ad-Hoc commands
List of arguments for ad-hoc commands:
ansible -i [inventory] [server group] -m [module] -u [user]
-m:Specify a module Eg:
-m command -a ping
If module is not specified with -m ansible defaults to the command module for ad-hoc commands
-a:Give arguments for the specified module or
command module by default.
Command module doesn't support
| & redirection, use
-m shell module
ansible all -m shell -a "command | grep text"
-f 1:Specify number of
forksansible uses. 1 will send command to one server at a time in order hosts are arranged in inventory.
-b:Becomes a different user
--becomeis used to supplement
sudowhen not operating with Linux systems.
-KWill ask the password for root user when used with
--limit "ip1,ip2"Limits the host, command will not execute on the ip marked with limit. Also, supports regex for ip.
Polling & running in Background:
-p 0Specify a polling time in seconds. Ansible will run the command in the background and check in the specified time if that command has executed.
0 = polling disabledBelow job runs for 30 minutes with polls every minute
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
-BSpecify time limit for the job to run in background. Below job runs for 3600 seconds without polling
ansible all -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff"
A result file is printed in output which gives a job ID. Check the result of the job using
ansible all -m async_status -a "jid=jobid"Job ID is unique to each server.
Thanks for reading!