Ansible Inventory File
Ansible needs to know which servers it can manage. The inventory file is a simple text file that lists managed nodes (hosts) and organizes them into groups.
What Is an Inventory File?
An inventory defines the target hosts for Ansible operations. It can be a static file (INI or YAML format) or dynamic (script that queries a cloud provider).
Simple INI Format
Create a file named
inventory.ini with the following content:# Individual hosts
192.168.1.10
192.168.1.11
# Hosts with aliases
web1 ansible_host=192.168.1.20
web2 ansible_host=192.168.1.21
# Groups
[webservers]
web1
web2
[databases]
db1 ansible_host=192.168.1.30Using the Inventory
Specify the inventory file with the
-i flag:ansible -i inventory.ini web1 -m pingTo target all hosts in a group:ansible -i inventory.ini webservers -m pingYAML Format
Ansible also supports YAML for inventories:
all:
hosts:
192.168.1.10:
192.168.1.11:
children:
webservers:
hosts:
web1:
ansible_host: 192.168.1.20
web2:
ansible_host: 192.168.1.21
databases:
hosts:
db1:
ansible_host: 192.168.1.30Inventory Variables
You can define variables for hosts or groups:
[webservers]
web1 ansible_user=admin ansible_port=2222
[all:vars]
ansible_python_interpreter=/usr/bin/python3Default Inventory Location
If you don’t specify
-i, Ansible looks for /etc/ansible/hosts. For most tutorials, it’s better to create a project‑specific inventory file.Two Minute Drill
- Inventory defines target hosts and groups (INI or YAML).
- Use
-iflag to specify inventory file. - Groups allow targeting multiple hosts (e.g.,
webservers). - Variables can be set per host or group.
Need more clarification?
Drop us an email at career@quipoinfotech.com
