Skip to content
Latchkey

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

Terminal
# 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" --become

Common 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

OptionPurpose
-m MODULEModule to run (default: command)
-a ARGSModule arguments
-i INVENTORYInventory source
--becomePrivilege escalation
-u USER / --private-keySSH user / key

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →