← Back to Home
📋

Ansible Inventory & Ad-Hoc Commands

Tell Ansible which servers to manage, then run instant commands across all of them.

12 min read📚DevOps Fundamentals

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:


The Inventory File

An inventory lists your managed nodes, optionally grouped. Here’s a simple inventory.ini:

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

💡 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:

bash
ansible all -i inventory.ini -m ping
bash — 80×24
student@devops:~$ansible web -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:

text
ansible <group> -i inventory.ini -m <module> -a "<arguments>"

Some useful examples:

bash
# 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

  1. Create inventory.ini with a [web] group containing one or two hosts you can SSH to (or use localhost with ansible_connection=local).
  2. Run the ping module against the group and confirm SUCCESS.
  3. Run an ad-hoc command module to get uptime from the hosts.
  4. Try an ad-hoc command that needs --become.

🧠 Knowledge Check

Knowledge Check

What is the purpose of an Ansible inventory file?

Knowledge Check

When is an ad-hoc command the better choice over a playbook?


💼 Interview Preparation

Interview Q&A

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.

Up Next

Ansible Playbooks

You've mastered this lesson. Continue your journey to becoming a DevOps Engineer.

Start Next Lesson