0 / 13 lessons — 0%
Lesson 03 / 13
Ad-hoc commands
Before writing a whole playbook, Ansible can run a single module against your inventory directly from the command line — perfect for quick checks or one-off fixes across a fleet.
# is everyone reachable? ansible all -i inventory.ini -m ping # run an arbitrary shell command ansible webservers -i inventory.ini -m shell -a "df -h" # install a package (the "apt" module, not raw shell) ansible webservers -i inventory.ini -m apt -a "name=nginx state=present" --become # copy a file out to every matching host ansible webservers -i inventory.ini -m copy -a "src=./index.html dest=/var/www/html/index.html"
| Flag | Meaning |
|---|---|
-i | which inventory file to use |
-m | which module to run (ping, shell, apt, copy, ...) |
-a | arguments passed to that module |
--become | escalate privileges (sudo) on the target host |
Rule of thumb: ad-hoc commands are for things you'll type once and never need to repeat exactly — checking disk space, restarting a service right now. The moment you'd want to run the same thing again next week, write a playbook instead.
Try it yourselfRun
ansible all -i inventory.ini -m shell -a "uptime" against your test hosts. One command, every server's uptime back in seconds — no loop, no SSH-ing in one at a time.