Ansible Inventory & Ad-Hoc Commands
Tell Ansible which servers to manage, then run instant commands across all of them.
Before Ansible can configure servers, it needs to know they exist. That’s the inventory. Once it does, ad-hoc commands let you run a single action across every server instantly — perfect for quick checks before you write full playbooks.
🎯 Learning Objectives
By the end of this lesson you will:
- Write an inventory file and organize hosts into groups
- Test connectivity with the
pingmodule - Run ad-hoc commands across servers
- Understand when to use ad-hoc vs a playbook
The Inventory File
An inventory lists your managed nodes, optionally grouped. Here’s a simple inventory.ini:
[web]
web-01 ansible_host=203.0.113.10
web-02 ansible_host=203.0.113.11
[db]
db-01 ansible_host=203.0.113.20
[all:vars]
ansible_user=ubuntu[web]and[db]are groups — you can target a whole group at once.[all:vars]sets variables (like the SSH user) for every host.
💡 Groups are how you target servers
Grouping lets you say “run this on all web servers” or “only the database”. Real inventories group by role (web/db/cache), environment (prod/staging), or region.
Test Connectivity: the ping Module
The first thing to run against any new inventory — it confirms Ansible can reach and authenticate to each host:
ansible all -i inventory.ini -m ping⚠ Ansible ping is not ICMP
The ping module doesn’t send network pings — it connects over SSH and checks Python is usable on the target. A SUCCESS ... "pong" means the whole connection + auth path works.
Ad-Hoc Commands
An ad-hoc command runs a single module against hosts without writing a playbook. The shape is:
ansible <group> -i inventory.ini -m <module> -a "<arguments>"Some useful examples:
# Check uptime on all web servers
ansible web -i inventory.ini -m command -a "uptime"
# Check disk space everywhere
ansible all -i inventory.ini -m command -a "df -h /"
# Install nginx (needs sudo -> --become)
ansible web -i inventory.ini -m apt -a "name=nginx state=present" --become💡 --become = sudo
--become tells Ansible to escalate privileges (run as root via sudo) for tasks that need it, like installing packages or editing system files.
Ad-Hoc vs Playbook
| Use ad-hoc when… | Use a playbook when… |
|---|---|
| One quick action (restart, check) | Multiple steps that belong together |
| Exploring / troubleshooting | You want it repeatable and version-controlled |
| No need to keep it | It’s part of your standard setup |
Ad-hoc is the “quick command” mode; playbooks (next lesson) are the durable, reusable mode.
🧪 Hands-on Lab
Build an Inventory and Ping It
- Create
inventory.iniwith a[web]group containing one or two hosts you can SSH to (or uselocalhostwithansible_connection=local). - Run the
pingmodule against the group and confirmSUCCESS. - Run an ad-hoc
commandmodule to getuptimefrom the hosts. - Try an ad-hoc command that needs
--become.
🧠 Knowledge Check
What is the purpose of an Ansible inventory file?
When is an ad-hoc command the better choice over a playbook?
💼 Interview Preparation
How do you organize an Ansible inventory for a real environment?
Summary
You can now define an inventory, verify connectivity with ping, and run ad-hoc commands across groups of servers. Next, we capture multi-step configuration as reusable playbooks — the heart of Ansible.