L'inventaire est un fichier texte listant tous les hôtes gérés par Ansible, organisés en groupes logiques.
[webservers]
web01 ansible_host=192.168.1.10
web02 ansible_host=192.168.1.11
Groupe bases de données
[dbservers]
db01 ansible_host=192.168.1.20
db02 ansible_host=192.168.1.21
Groupe production (contient d'autres groupes)
[production:children]
webservers
dbservers
Remarque: je recommande ce format
all:
children:
webservers:
hosts:
web01:
ansible_host: 192.168.1.10
web02:
ansible_host: 192.168.1.11
dbservers:
hosts:
db01:
ansible_host: 192.168.1.20
production:
children:
webservers: {}
dbservers: {}
[webservers]
web01 ansible_host=192.168.1.10 http_port=8080
web02 ansible_host=192.168.1.11 http_port=8080
[webservers:vars]
dns_servers="8.8.8.8 8.8.4.4"
ntp_server=pool.ntp.org
[webservers]
web01 ansible_host=192.168.1.10 special_var=value
web02 ansible_host=192.168.1.11
group_vars/)Structure recommandée
.
├── inventory
├── group_vars/
│ ├── webservers.yml
│ └── dbservers.yml
├── host_vars/
└── web01.yml
group_vars/webservers.yml http_port: 80
https_port: 443
firewall_zone: "public"
backup_schedule: "weekly"
Pinger tous les hôtes
$ ansible all -m ping
Pinger un groupe spécifique
$ ansible webservers -m ping
Pinger avec authentification explicite
$ ansible all -m ping --ask-pass --ask-user
Exporter le résultat en JSON
$ ansible all -m ping -o > ping_results.json
web01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
web02 | SUCCESS => {
"changed": false,
"ping": "pong"
}
| Problème | Cause probable | Solution |
|---|---|---|
UNREACHABLE |
Connexion SSH impossible | Vérifier pare-feu, service SSH actif |
FAILED |
Échec du module | Vérifier permissions, dépendances |
Permission denied |
Clé SSH incorrecte | Re-générer ou recopier les clés |
Host key verification failed |
Changement de clé | Supprimer l'entrée dans known_hosts |
Ajouter exo
Un inventaire dynamique génère la liste des hôtes à la volée via un script exécutable, en interrogeant une API cloud (AWS, Azure, Google Cloud, etc.).
Installation du plugin
pip install boto3 botocore
Script inventaire fourni par Ansible
ansible-inventory -i aws_ec2.yml --listyaml
Configuration aws_ec2.yml
plugin: amazon.aws.aws_ec2
regions:
eu-west-3
filters:
tag:Environment: production