Inventaire statique INI
L'inventaire definit les hotes et groupes que Ansible peut gerer.
# inventory/hosts (format INI)
[webservers]
web1.example.com
web2.example.com ansible_port=2222
[dbservers]
db1.example.com ansible_user=postgres
db2.example.com
[production:children]
webservers
dbservers
[all:vars]
ansible_python_interpreter=/usr/bin/python3
Inventaire YAML
# inventory/hosts.yml
all:
children:
webservers:
hosts:
web1.example.com:
web2.example.com:
ansible_port: 2222
dbservers:
hosts:
db1.example.com:
ansible_user: postgres
db2.example.com:
host_vars et group_vars
Les variables specifiques aux hotes et groupes sont stockees dans des fichiers dedies.
# Structure de repertoires
project/
inventory/
hosts
host_vars/
web1.example.com.yml # Variables pour web1
db1.example.com.yml # Variables pour db1
group_vars/
webservers.yml # Variables pour le groupe webservers
dbservers.yml # Variables pour le groupe dbservers
all.yml # Variables pour tous les hotes
# group_vars/webservers.yml
http_port: 80
max_clients: 200
document_root: /var/www/html
# host_vars/web1.example.com.yml
nginx_worker_processes: 4
ssl_certificate: /etc/ssl/web1.pem
Verifier l'inventaire
# Lister tous les hotes
ansible all --list-hosts -i inventory/hosts
# Lister un groupe specifique
ansible webservers --list-hosts -i inventory/hosts
# Afficher les variables d'un hote
ansible web1.example.com -m debug -a "var=hostvars[inventory_hostname]"
Astuce : Utilisez
ansible-inventory --graph pour visualiser la hierarchie de votre inventaire.