Lesson 2 of 11 ↓
- 1.Ansible Basic Setup Guide
- 2.Ansible Inventory Guide (hosts)
- 3.Ansible Configuration Guide (ansible.cfg)
- 4.Ansible Ad-hoc Commands Guide
- 5.Ansible Playbook Guide (playbook.yml)
- 6.Ansible Variables & Precedence Guide
- 7.Ansible Loops Guide
- 8.Ansible Conditionals Guide (when)
- 9.Ansible Vault Guide
- 10.Ansible Handlers Guide
- 11.Ansible Custom Roles Guide
2 min read
Ansible Inventory Guide (hosts)
What is an Inventory?
The inventory is the "Source of Truth" for Ansible. It tells Ansible which servers to manage and how to communicate with them.
Inventories can be:
-
Static: A text file (INI or YAML) where you manually list servers (Focus of this guide).
-
Dynamic: Scripts or plugins that fetch server lists automatically from Cloud Providers (AWS, Azure, etc.).
Basic Static Inventory Structure
The most common format is INI. You can list IP addresses or Hostnames.
Simple List (ungrouped)
192.168.1.50
192.168.1.51
server1.company.comGrouped Hosts
Grouping allows you to run automation on specific sets of servers (e.g., only Webservers or only Databases).
[web]
192.168.1.10
192.168.1.11
[db]
192.168.1.20Connection Parameters (the "cheat Sheet")
You can define how Ansible connects to each host using special variables directly in the inventory line.
| Parameter | Description | Example |
|---|---|---|
ansible_host |
Real IP/Hostname if different from the alias. | web1 ansible_host=10.0.0.5 |
ansible_user |
The SSH user to login with. | ansible_user=ec2-user |
ansible_ssh_private_key_file |
Path to the private key (.pem). |
ansible_ssh_private_key_file=~/key.pem |
ansible_port |
SSH port (if not 22). | ansible_port=2222 |
ansible_connection |
Connection type (ssh, winrm, local). | ansible_connection=local |
ansible_ssh_pass |
SSH Password (if not using keys - Not Recommended). | ansible_ssh_pass=Secret123 |
Example with Parameters (inline)
[web]
# Syntax: <IP> <Parameter1> <Parameter2> ...
10.0.1.5 ansible_user=ec2-user ansible_ssh_private_key_file=~/ansible-project/your-key.pem
10.0.1.6 ansible_user=ec2-user ansible_ssh_private_key_file=~/ansible-project/your-key.pemOptimization: Group Variables
Instead of repeating the user and key path for every single line (like above), you can define variables for the entire group.
[web]
10.0.1.5
10.0.1.6
# Set variables for all hosts in the [web] group
[web:vars]
ansible_user=ec2-user
ansible_ssh_private_key_file=~/ansible-project/your-key.pemNow, both IPs automatically inherit the user and key settings.
Nested Groups (groups of Groups)
You can create a "Super Group" that contains other groups using the :children suffix. This is useful if you want to target your entire application stack (Web + DB) at once.
Syntax:
[web]
<node1_private_ip> ansible_user=ec2-user ansible_ssh_private_key_file=~/ansible-project/your-key.pem
[db]
<node2_private_ip> ansible_user=ec2-user ansible_ssh_private_key_file=~/ansible-project/your-key.pem
# "mysite" includes both "web" and "db"
[mysite:children]
web
dbUsage:
-
Target Web only:
ansible web -m ping -
Target DB only:
ansible db -m ping -
Target Everything:
ansible mysite -m ping
Testing Your Inventory
Once your file is created, use the ansible ad-hoc command to verify Ansible can see the groups.
List all hosts in the inventory:
ansible all --list-hostsPing a specific group:
ansible web -m pingPing a parent group (Children):
ansible mysite -m pingDefault Location
By default, Ansible looks for /etc/ansible/hosts. However, in most projects, we create a local hosts file and point to it in ansible.cfg:
ansible.cfg:
[defaults]
inventory = ./hosts
Comments
Loading the discussion…