Ansible
Ansible configure les LXC après leur provisioning par Terraform. Il installe Docker, déploie les fichiers .env contenant les secrets, et configure les services.
Règle d’or : Terraform crée les LXC, Ansible les configure.
Installation
Section titled “Installation”# Fedora / RHELsudo dnf install ansible
# Ubuntu / Debiansudo apt install ansible
# pip (toutes distributions)pip install ansibleStructure recommandée
Section titled “Structure recommandée”ansible/├── ansible.cfg├── .vault_pass # local uniquement — jamais committé├── .gitignore├── inventory/│ ├── hosts.yml│ └── group_vars/│ ├── all/│ │ ├── vars.yml # variables partagées│ │ └── vault.yml # secrets globaux chiffrés│ ├── <groupe>/│ │ ├── vars.yml # références vers vault│ │ └── vault.yml # secrets chiffrés│ └── ...├── roles/│ ├── docker/ # Installation Docker CE│ ├── env/ # Déploiement fichiers .env│ └── ssh_keys/ # Clés SSH autorisées└── playbooks/ ├── docker.yml └── <service>-env.ymlConfiguration
Section titled “Configuration”ansible.cfg
Section titled “ansible.cfg”[defaults]roles_path = ./rolesinventory = ./inventory/hosts.ymlhost_key_checking = Falsevault_password_file = .vault_passFichier vault pass
Section titled “Fichier vault pass”echo "<MOT_DE_PASSE_VAULT>" > ~/.vault_passchmod 600 ~/.vault_passln -s ~/.vault_pass /path/to/ansible/.vault_passInventaire
Section titled “Inventaire”all: children: <groupe>: hosts: <hostname>: ansible_host: <IP> ansible_user: root ansible_ssh_private_key_file: ~/.ssh/id_rsaExemple avec plusieurs groupes :
all: children: lxc: children: core: hosts: core: ansible_host: <CORE_IP> ansible_user: root ansible_ssh_private_key_file: ~/.ssh/id_rsa apps: hosts: apps: ansible_host: <APPS_IP> ansible_user: root ansible_ssh_private_key_file: ~/.ssh/id_rsa dmz: hosts: blog-prod: ansible_host: <DMZ_IP> ansible_user: root ansible_ssh_private_key_file: ~/.ssh/id_rsaAnsible Vault
Section titled “Ansible Vault”Les secrets sont chiffrés avec Ansible Vault (AES256) dans inventory/group_vars/<groupe>/vault.yml. Le .vault_pass est local et gitignored — référencé dans ansible.cfg.
Convention vars / vault
Section titled “Convention vars / vault”# vars.yml — référence la variable vaultdb_password: "{{ vault_db_password }}"
# vault.yml — valeur chiffrée (préfixe vault_)vault_db_password: "le_vrai_mot_de_passe"Commandes
Section titled “Commandes”# Créer un nouveau fichier vaultansible-vault create inventory/group_vars/<groupe>/vault.yml
# Éditeransible-vault edit inventory/group_vars/<groupe>/vault.yml
# Voir le contenu déchiffréansible-vault view inventory/group_vars/<groupe>/vault.yml
# Chiffrer un fichier existantansible-vault encrypt inventory/group_vars/<groupe>/vault.yml
# Chiffrer une valeur seuleansible-vault encrypt_string 'valeur_secrete' --name 'vault_ma_variable'Rôle Docker
Section titled “Rôle Docker”Installe Docker CE et Docker Compose sur Ubuntu 24.04 LXC.
---- name: Install dependencies apt: name: [ca-certificates, curl, gnupg] state: present update_cache: true
- name: Create keyrings directory file: path: /etc/apt/keyrings state: directory mode: '0755'
- name: Add Docker GPG key get_url: url: https://download.docker.com/linux/ubuntu/gpg dest: /etc/apt/keyrings/docker.asc mode: '0644'
- name: Get architecture command: dpkg --print-architecture register: dpkg_arch changed_when: false
- name: Get Ubuntu codename shell: ". /etc/os-release && echo $VERSION_CODENAME" register: ubuntu_codename changed_when: false
- name: Add Docker repository apt_repository: repo: "deb [arch={{ dpkg_arch.stdout }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ubuntu_codename.stdout }} stable" state: present filename: docker
- name: Install Docker apt: name: [docker-ce, docker-ce-cli, containerd.io, docker-compose-plugin] state: present update_cache: true
- name: Enable and start Docker systemd: name: docker enabled: true state: startedPlaybook Docker
Section titled “Playbook Docker”---- hosts: lxc roles: - docker# Installer Docker sur tous les LXCansible-playbook playbooks/docker.yml
# Sur un groupe spécifiqueansible-playbook playbooks/docker.yml --limit appsPlaybook déploiement .env
Section titled “Playbook déploiement .env”Les fichiers .env sont généralement exclus du rsync CI/CD. Ce pattern permet de les déployer via Ansible à partir des secrets vault :
---- name: Déployer le fichier .env template: src: "{{ env_template }}" dest: "{{ env_base_dir }}/{{ service_name }}/.env" owner: root group: root mode: '0600' notify: Restart service# playbooks/<service>-env.yml---- hosts: <groupe> vars: service_name: <SERVICE> env_template: <SERVICE>.env.j2 roles: - envCommandes utiles
Section titled “Commandes utiles”# Tester la connexion à tous les hostsansible all -m ping
# Dry-run d'un playbookansible-playbook playbooks/<playbook>.yml --check --diff
# Exécuter une commande ad-hocansible <groupe> -m shell -a "docker ps --format '{{.Names}}'"
# Vérifier Docker sur tous les LXCansible all -m shell -a "docker --version && docker compose version"
# Lister l'inventaireansible-inventory --list
# Voir les variables d'un hostansible-inventory --host <hostname>