Inventory management
Chapter objectives
- Understand the role of the inventory
- Create static inventories
- Organize hosts into groups
- Use dynamic inventories
1 - What is the inventory?
Definition
The inventory is the list of machines (hosts) that Ansible can manage.
Supported formats
| Format | Extension | Use |
|---|---|---|
| INI | .ini | Simple, readable |
| YAML | .yml | Structured, powerful |
| Dynamic | Script/Plugin | Cloud, CMDB |
2 - INI inventory (basic)
Basic syntax
# inventory/hosts.ini
# Hôtes sans groupe (groupe implicite "ungrouped")
server1.example.com
192.168.1.10
# Groupe webservers
[webservers]
web-01 ansible_host=192.168.1.11
web-02 ansible_host=192.168.1.12
web-03 ansible_host=192.168.1.13
# Groupe databases
[databases]
db-01 ansible_host=192.168.1.20
db-02 ansible_host=192.168.1.21
# Groupe loadbalancers
[loadbalancers]
lb-01 ansible_host=192.168.1.5
Groups of groups (children)
# inventory/hosts.ini
[webservers]
web-01
web-02
[databases]
db-01
[loadbalancers]
lb-01
# Groupe parent contenant d'autres groupes
[production:children]
webservers
databases
loadbalancers
# Autre exemple
[frontend:children]
webservers
loadbalancers
[backend:children]
databases
Variables in the inventory
# Variables pour un hôte
[webservers]
web-01 ansible_host=192.168.1.11 http_port=80 max_clients=200
web-02 ansible_host=192.168.1.12 http_port=8080
# Variables pour un groupe
[webservers:vars]
nginx_version=1.24
document_root=/var/www/html
ansible_user=deploy
[databases:vars]
db_port=5432
backup_enabled=true
3 - YAML inventory
YAML syntax
# inventory/hosts.yml
all:
children:
webservers:
hosts:
web-01:
ansible_host: 192.168.1.11
http_port: 80
web-02:
ansible_host: 192.168.1.12
http_port: 8080
vars:
nginx_version: "1.24"
document_root: /var/www/html
databases:
hosts:
db-01:
ansible_host: 192.168.1.20
db-02:
ansible_host: 192.168.1.21
vars:
db_port: 5432
production:
children:
webservers:
databases:
INI vs YAML comparison
4 - Host patterns
Target hosts
# Tous les hôtes
ansible all -m ping
# Un groupe spécifique
ansible webservers -m ping
# Un hôte spécifique
ansible web-01 -m ping
# Plusieurs groupes (OR)
ansible 'webservers:databases' -m ping
# Intersection (AND)
ansible 'webservers:&production' -m ping
# Exclusion
ansible 'webservers:!web-03' -m ping
# Pattern avec wildcard
ansible 'web-*' -m ping
# Par numéro (range)
ansible 'web-[01:03]' -m ping
Pattern examples
5 - Inventory variables
Variable hierarchy
group_vars and host_vars files
inventory/
├── hosts.ini
├── group_vars/
│ ├── all.yml # Variables pour tous
│ ├── webservers.yml # Variables groupe webservers
│ └── databases.yml # Variables groupe databases
└── host_vars/
├── web-01.yml # Variables spécifiques web-01
└── db-01.yml # Variables spécifiques db-01
# group_vars/all.yml
---
ansible_user: deploy
timezone: Europe/Paris
ntp_servers:
- ntp1.example.com
- ntp2.example.com
# group_vars/webservers.yml
---
nginx_version: "1.24"
nginx_worker_processes: auto
nginx_worker_connections: 1024
# host_vars/web-01.yml
---
nginx_worker_processes: 4
backup_enabled: true
6 - Connection variables
Built-in Ansible variables
# Connexion SSH
ansible_host: 192.168.1.10 # IP ou hostname
ansible_port: 22 # Port SSH
ansible_user: deploy # Utilisateur distant
ansible_ssh_private_key_file: ~/.ssh/deploy_key
# Escalade de privilèges
ansible_become: true # Activer sudo
ansible_become_method: sudo # Méthode (sudo, su, doas)
ansible_become_user: root # Utilisateur cible
ansible_become_password: secret # Mot de passe sudo
# Python
ansible_python_interpreter: /usr/bin/python3
# Type de connexion
ansible_connection: ssh # ssh, local, docker, winrm
Complete example
# inventory/hosts.ini
[webservers]
web-01 ansible_host=192.168.1.11 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/aws.pem
web-02 ansible_host=192.168.1.12 ansible_user=ubuntu
[databases]
db-01 ansible_host=192.168.1.20 ansible_user=postgres ansible_become_password=dbadmin
[windows]
win-01 ansible_host=192.168.1.30 ansible_connection=winrm ansible_user=Administrator ansible_password=P@ssw0rd
[local]
localhost ansible_connection=local
7 - Dynamic inventories
Concept
AWS EC2
# inventory/aws_ec2.yml
plugin: amazon.aws.aws_ec2
regions:
- eu-west-1
- eu-central-1
filters:
tag:Environment: production
instance-state-name: running
keyed_groups:
- key: tags.Role
prefix: role
- key: placement.availability_zone
prefix: az
compose:
ansible_host: public_ip_address
# Installer la collection
ansible-galaxy collection install amazon.aws
# Lister les hôtes
ansible-inventory -i inventory/aws_ec2.yml --list
# Utiliser
ansible all -i inventory/aws_ec2.yml -m ping
Custom script
#!/usr/bin/env python3
# inventory/dynamic.py
import json
import argparse
def get_inventory():
return {
"webservers": {
"hosts": ["web-01", "web-02"],
"vars": {
"http_port": 80
}
},
"databases": {
"hosts": ["db-01"]
},
"_meta": {
"hostvars": {
"web-01": {"ansible_host": "192.168.1.11"},
"web-02": {"ansible_host": "192.168.1.12"},
"db-01": {"ansible_host": "192.168.1.20"}
}
}
}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--list', action='store_true')
parser.add_argument('--host', action='store')
args = parser.parse_args()
if args.list:
print(json.dumps(get_inventory()))
elif args.host:
print(json.dumps({}))
# Rendre exécutable
chmod +x inventory/dynamic.py
# Tester
./inventory/dynamic.py --list
# Utiliser
ansible all -i inventory/dynamic.py -m ping
8 - Multi-environment
Recommended structure
inventory/
├── production/
│ ├── hosts.ini
│ ├── group_vars/
│ │ ├── all.yml
│ │ └── webservers.yml
│ └── host_vars/
│ └── web-01.yml
├── staging/
│ ├── hosts.ini
│ └── group_vars/
│ └── all.yml
└── development/
├── hosts.ini
└── group_vars/
└── all.yml
Usage
# Déploiement production
ansible-playbook -i inventory/production/ site.yml
# Déploiement staging
ansible-playbook -i inventory/staging/ site.yml
# Déploiement dev
ansible-playbook -i inventory/development/ site.yml
9 - Useful commands
Inspect the inventory
# Lister tous les hôtes
ansible-inventory --list
# Format graphique
ansible-inventory --graph
# Afficher les variables d'un hôte
ansible-inventory --host web-01
# Valider la syntaxe
ansible-inventory -i inventory/ --list
Example of --graph output
@all:
|--@production:
| |--@webservers:
| | |--web-01
| | |--web-02
| |--@databases:
| | |--db-01
|--@ungrouped:
Summary
Key points
- The inventory defines YOUR target hosts
- Organize with hierarchical groups
- Separate variables into group_vars/host_vars
- Use dynamic inventories for the cloud
Practical exercises
- Create an inventory with 3 groups: web, db, cache
- Add group and host variables
- Test the selection patterns
- Create a multi-environment inventory