ansible (ad-hoc): Usage, Options & Common CI Errors
Run a single module against your hosts without writing a playbook.
The ansible command runs one module against a pattern of hosts - ideal for quick checks (ping, uptime), one-off changes, and smoke tests in CI before a full playbook run.
What it does
ansible <pattern> -m <module> -a <args> runs a single task (one module invocation) against every host matching the pattern in the inventory. With no -m it defaults to the command module. It is the imperative counterpart to ansible-playbook’s declarative runs.
Common usage
# Connectivity check across all hosts
ansible all -i inventory.ini -m ping
# Run a shell command on a group
ansible web -i inventory.ini -m command -a "uptime"
# Make a change with privilege escalation
ansible db -i inventory.ini -m apt -a "name=nginx state=present" --becomeCommon error in CI: no hosts matched / unreachable
A run prints "[WARNING]: No inventory was parsed, only implicit localhost is available" or "skipping: no hosts matched" when -i is missing or the pattern names a group that does not exist; with bad SSH it reports "UNREACHABLE!". Fix: pass an explicit -i inventory, verify the group name with ansible-inventory --list, and for unreachable hosts set ANSIBLE_HOST_KEY_CHECKING=false on ephemeral runners plus the right --user/--private-key.
Key options
| Option | Purpose |
|---|---|
| -m MODULE | Module to run (default: command) |
| -a ARGS | Module arguments |
| -i INVENTORY | Inventory source |
| --become | Privilege escalation |
| -u USER / --private-key | SSH user / key |