
ansible-galaxy est la CLI pour gérer rôles et collections : créer un rôle vide, en installer depuis Galaxy/Git, lister ce qui est installé, supprimer, rechercher, et publier. Cette page passe en revue les commandes essentielles avec exemples concrets.
Ce que vous allez apprendre
Section intitulée « Ce que vous allez apprendre »ansible-galaxy role initpour créer un rôle vide.ansible-galaxy role/collection installdepuis Galaxy ou Git.ansible-galaxy listpour voir ce qui est installé.ansible-galaxy searchetinfopour explorer.ansible-galaxy collection build/publishpour publier.ansible-galaxy collection verifypour vérifier l’intégrité.
Initialisation
Section intitulée « Initialisation »# Créer un rôle vide avec la structure standardansible-galaxy role init nginx
# Créer dans un dossier spécifiqueansible-galaxy role init --init-path roles/ nginx
# Créer une collectionansible-galaxy collection init stephrobert.networkingrole init génère 8 dossiers (defaults, files, handlers, meta, tasks, templates, tests, vars) + README.md.
collection init génère la structure d’une collection : roles/, plugins/, playbooks/, meta/, galaxy.yml.
Recherche
Section intitulée « Recherche »# Rechercher un rôle sur Galaxyansible-galaxy search nginx --author geerlingguy
# Filtrer par tag platformansible-galaxy search nginx --platforms EL
# Info détaillée d'un rôleansible-galaxy role info geerlingguy.nginxrole info affiche : versions, dépendances, plateformes, dernière mise à jour, lien GitHub.
Installation
Section intitulée « Installation »Depuis Galaxy (rôle communautaire)
Section intitulée « Depuis Galaxy (rôle communautaire) »ansible-galaxy role install geerlingguy.nginxInstalle dans ~/.ansible/roles/ (par défaut) — visible par tous les playbooks.
Depuis Git (avec branche/tag)
Section intitulée « Depuis Git (avec branche/tag) »# Branche mainansible-galaxy role install git+https://github.com/geerlingguy/ansible-role-nginx.git,master
# Tag spécifiqueansible-galaxy role install git+https://github.com/geerlingguy/ansible-role-nginx.git,3.1.0
# Avec un nom local différentansible-galaxy role install git+https://github.com/...,3.1.0,nginx-prodDepuis requirements.yml
Section intitulée « Depuis requirements.yml »ansible-galaxy role install -r requirements.ymlansible-galaxy collection install -r requirements.ymlVoir la page installer-roles-galaxy pour le format complet.
Vers un dossier projet (recommandé pour CI/CD)
Section intitulée « Vers un dossier projet (recommandé pour CI/CD) »ansible-galaxy role install -r requirements.yml -p ./roles/ansible-galaxy collection install -r requirements.yml -p ./collections/-p ./roles/ = installation locale au projet, pas dans ~/.ansible/. Pattern recommandé pour reproductibilité CI/CD.
# Lister tous les rôles installésansible-galaxy role list
# Lister les collections installéesansible-galaxy collection list
# Filtrer par namespaceansible-galaxy collection list community.general
# Format JSON pour scriptsansible-galaxy collection list --format jsonSuppression
Section intitulée « Suppression »ansible-galaxy role remove geerlingguy.nginxPas d’équivalent pour les collections — il faut supprimer manuellement le dossier dans ~/.ansible/collections/ansible_collections/<namespace>/<collection>/.
Build & publish (collections)
Section intitulée « Build & publish (collections) »# Build une collection (génère .tar.gz)cd path/to/collection/ansible-galaxy collection build
# Publier sur Galaxy avec un token APIansible-galaxy collection publish stephrobert-networking-1.0.0.tar.gz \ --api-key=$GALAXY_TOKENToken API disponible sur galaxy.ansible.com → Profil → API Token.
Vérification
Section intitulée « Vérification »# Vérifier l'intégrité d'une collection installée vs Galaxyansible-galaxy collection verify community.general
# Avec signature GPG (Galaxy 2024+)ansible-galaxy collection verify community.general \ --signature https://galaxy.ansible.com/api/...Détecte : altération locale du code, fichiers modifiés post-install.
Variables d’environnement utiles
Section intitulée « Variables d’environnement utiles »| Variable | Effet |
|---|---|
ANSIBLE_GALAXY_TOKEN | Token Galaxy par défaut (évite --api-key) |
ANSIBLE_ROLES_PATH | Chemins de recherche des rôles |
ANSIBLE_COLLECTIONS_PATH | Chemins de recherche des collections |
ANSIBLE_GALAXY_SERVER | URL d’un Automation Hub privé |
Configuration ansible.cfg pour Galaxy
Section intitulée « Configuration ansible.cfg pour Galaxy »[galaxy]server_list = corporate_hub, galaxy
[galaxy_server.corporate_hub]url = https://hub.corp.example.com/api/automation-hub/token = <token>
[galaxy_server.galaxy]url = https://galaxy.ansible.com/Pattern entreprise : un mirror privé (Automation Hub) en premier, Galaxy public en fallback.
Pratiquer dans le lab
Section intitulée « Pratiquer dans le lab »Cette page a un lab d’accompagnement : labs/galaxy/ansible-galaxy-cli/ dans
stephrobert/ansible-training.
Le lab fournit un cheatsheet exhaustif des commandes ansible-galaxy. 7 tests structure validés (init, install, list, publish, verify).
cd ~/Projets/ansible-training/labs/galaxy/ansible-galaxy-cli/
cat cheatsheet.mdpytest -v challenge/tests/ # 7 testsPièges courants
Section intitulée « Pièges courants »| Symptôme | Cause | Fix |
|---|---|---|
Role not found après install | roles_path mal configuré | `ansible-config dump |
Collection community.X plante | Dépendance non installée | ansible-galaxy collection list pour voir |
| Token Galaxy invalide | Token expiré | Régénérer sur galaxy.ansible.com |
role import (Galaxy auto-import) ne déclenche pas | Webhook GitHub non configuré | Vérifier sur Galaxy → Imports |
| Collection installée 2 fois | ~/.ansible/ + projet local | Préférer -p ./collections/ (local) |
À retenir
Section intitulée « À retenir »role init+collection init= scaffolding officiel.install -r requirements.yml -p ./<dir>/= installation locale projet (CI/CD).list+info= inspection.build+publish= workflow de publication collection.verify= intégrité cryptographique.ansible.cfg [galaxy]= mirror privé Automation Hub.