Ansible Ad-Hoc Commands for When You Don't Need a Playbook

Not every fleet-wide task needs a playbook. The ansible ad-hoc command covers most one-off checks.

Ping the whole inventory

ansible all -i inventory.ini -m ping

Check disk space everywhere

ansible webservers -m shell -a "df -h /" -i inventory.ini

Restart a service on a group

ansible db -m systemd -a "name=postgresql state=restarted" -b -i inventory.ini

Push a file to every host

ansible all -m copy -a "src=./resolv.conf dest=/etc/resolv.conf backup=yes" -b

Gather facts for one host and grep it

ansible web01 -m setup -a "filter=ansible_distribution*"

Run with a limited batch size (rolling, not all-at-once)

ansible webservers -m shell -a "systemctl reload nginx" -f 5 -b

Check what would change without applying it

ansible all -m package -a "name=curl state=latest" --check -b

-f controls fork/parallelism, -b becomes root, --check is your dry run. Reach for a real playbook once the same ad-hoc command shows up in your shell history three times.