Ansible
Auteur : Gautier RAYEROUX | Date : 2026-02-24 00:00:00
🔎 1. C’est quoi Ansible ?
Section titled “🔎 1. C’est quoi Ansible ?”Ansible est un outil d’automatisation :
- Configuration management
- Déploiement applicatif
- Orchestration
- Provisioning
✔ Agentless (SSH)
✔ Basé sur YAML
✔ Idempotent (rejouable sans casser)
📁 3. Structure de Projet Recommandée
Section titled “📁 3. Structure de Projet Recommandée”ansible-project/│├── inventory/│ ├── hosts.yml│├── group_vars/│ ├── all.yml│ ├── web.yml│├── host_vars/│ ├── server1.yml│├── roles/│ ├── nginx/│ │ ├── defaults/│ │ │ └── main.yml│ │ ├── tasks/│ │ │ └── main.yml│ │ ├── handlers/│ │ │ └── main.yml│ │ ├── templates/│ │ │ └── nginx.conf.j2│ │ ├── files/│ │ └── vars/│├── playbook.yml└── ansible.cfg🏗 2. Architecture Type
Section titled “🏗 2. Architecture Type”📌 Composants
Section titled “📌 Composants”🖥 Control Node
Section titled “🖥 Control Node”- Machine où Ansible est installé
- Contient :
- Playbooks
- Inventory
- Roles
- Se connecte en SSH
🖥 Managed Nodes
Section titled “🖥 Managed Nodes”- Serveurs cibles
- Pas d’agent requis
- Python installé
📂 Inventory
Section titled “📂 Inventory”Liste des hôtes gérés.
📜 4. Inventory
Section titled “📜 4. Inventory”YAML (recommandé)
Section titled “YAML (recommandé)”all: children: web: hosts: web1: ansible_host: 192.168.1.10 web2: ansible_host: 192.168.1.11
db: hosts: db1: ansible_host: 192.168.1.20▶ 5. Playbook Minimal
Section titled “▶ 5. Playbook Minimal”- name: Installer nginx hosts: web become: yes
tasks: - name: Installer nginx apt: name: nginx state: presentLancer :
ansible-playbook-i inventory/hosts.yml playbook.yml🔁 7. Conditions (when)
Section titled “🔁 7. Conditions (when)”Oui, Ansible supporte les conditions.
- name: Installer Apache sur Debian apt: name: apache2 state: present when: ansible_os_family == "Debian"Avec variable :
when: install_webserver is defined and install_webserver == true🧠 9. Variables
Section titled “🧠 9. Variables”Définition
Section titled “Définition”Dans group_vars/web.yml :
http_port: 80Utilisation :
- debug: msg:"Le port est {{ http_port }}"🎭 6. Les Roles
Section titled “🎭 6. Les Roles”Un rôle = module structuré réutilisable.
Dans roles/nginx/tasks/main.yml :
- name: Installer nginx apt: name: nginx state: presentDans playbook.yml :
- hosts: web roles: - nginx🔄 8. Boucles (loop)
Section titled “🔄 8. Boucles (loop)”Boucle simple
Section titled “Boucle simple”- name: Installer plusieurs paquets apt: name:"{{ item }}" state: present with_items: - git - curl - vimBoucle avec dictionnaire
Section titled “Boucle avec dictionnaire”- name: Créer utilisateurs user: name:"{{ item.name }}" shell:"{{ item.shell }}" loop: - { name:"dev1", shell:"/bin/bash" } - { name:"dev2", shell:"/bin/zsh" }🧩 10. Templates Jinja2
Section titled “🧩 10. Templates Jinja2”Ansible utilise Jinja.
📄 Template (nginx.conf.j2)
Section titled “📄 Template (nginx.conf.j2)”server { listen {{ http_port }}; server_name {{ domain_name }};
location / { proxy_pass http://{{ backend_ip }}; }}📜 Task
Section titled “📜 Task”- name: Déployer config nginx template: src: nginx.conf.j2 dest: /etc/nginx/sites-available/default notify: Restart nginx🔔 11. Handlers
Section titled “🔔 11. Handlers”roles/nginx/handlers/main.yml
- name: Restart nginx service: name: nginx state: restartedAppelé uniquement si changement 👍
📦 12. Modules Essentiels
Section titled “📦 12. Modules Essentiels”| Module | Usage |
|---|---|
| apt / yum | Gestion paquets |
| service | Services |
| copy | Copier fichier |
| template | Template Jinja |
| file | Permissions |
| user | Gestion utilisateurs |
| command | Commande simple |
| shell | Commande shell |
🛠 13. Commandes Utiles
Section titled “🛠 13. Commandes Utiles”Test ping
Section titled “Test ping”ansible all-mping-i inventory/hosts.ymlDry-run
Section titled “Dry-run”ansible-playbook playbook.yml--checkansible-playbook playbook.yml--diff🎯 14. Bonnes Pratiques
Section titled “🎯 14. Bonnes Pratiques”✔ Utiliser des roles
✔ Séparer variables / tasks
✔ Utiliser --check
✔ Versionner avec Git
✔ Utiliser Ansible Vault pour secrets
🔐 15. Ansible Vault
Section titled “🔐 15. Ansible Vault”ansible-vault create secrets.ymlansible-vault edit secrets.ymlansible-vault encrypt secrets.ymlDans playbook :
ansible-playbook playbook.yml--ask-vault-pass🚀 16. Workflow Type
Section titled “🚀 16. Workflow Type”- Écrire inventory
- Créer role
- Ajouter variables
- Tester en
-check - Déployer
🎓 Résumé Ultra Rapide
Section titled “🎓 Résumé Ultra Rapide”| Concept | Mot clé |
|---|---|
| Cible | hosts |
| Action | tasks |
| Logique | when |
| Boucle | loop |
| Réutilisable | roles |
| Config dynamique | template |
| Redémarrage | handlers |