Aller au contenu
Infrastructure as Code medium

ansible-galaxy CLI : init, install, list, search, remove, info

8 min de lecture

Logo Ansible

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.

  • ansible-galaxy role init pour créer un rôle vide.
  • ansible-galaxy role/collection install depuis Galaxy ou Git.
  • ansible-galaxy list pour voir ce qui est installé.
  • ansible-galaxy search et info pour explorer.
  • ansible-galaxy collection build/publish pour publier.
  • ansible-galaxy collection verify pour vérifier l'intégrité.
Fenêtre de terminal
# Créer un rôle vide avec la structure standard
ansible-galaxy role init nginx
# Créer dans un dossier spécifique
ansible-galaxy role init --init-path roles/ nginx
# Créer une collection
ansible-galaxy collection init stephrobert.networking

role 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.

Fenêtre de terminal
# Rechercher un rôle sur Galaxy
ansible-galaxy search nginx --author geerlingguy
# Filtrer par tag platform
ansible-galaxy search nginx --platforms EL
# Info détaillée d'un rôle
ansible-galaxy role info geerlingguy.nginx

role info affiche : versions, dépendances, plateformes, dernière mise à jour, lien GitHub.

Fenêtre de terminal
ansible-galaxy role install geerlingguy.nginx

Installe dans ~/.ansible/roles/ (par défaut), visible par tous les playbooks.

Fenêtre de terminal
# Branche main
ansible-galaxy role install git+https://github.com/geerlingguy/ansible-role-nginx.git,master
# Tag spécifique
ansible-galaxy role install git+https://github.com/geerlingguy/ansible-role-nginx.git,3.1.0
# Avec un nom local différent
ansible-galaxy role install git+https://github.com/...,3.1.0,nginx-prod
Fenêtre de terminal
ansible-galaxy role install -r requirements.yml
ansible-galaxy collection install -r requirements.yml

Voir la page installer-roles-galaxy pour le format complet.

Fenêtre de terminal
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.

Fenêtre de terminal
# Lister tous les rôles installés
ansible-galaxy role list
# Lister les collections installées
ansible-galaxy collection list
# Filtrer par namespace
ansible-galaxy collection list community.general
# Format JSON pour scripts
ansible-galaxy collection list --format json
Fenêtre de terminal
ansible-galaxy role remove geerlingguy.nginx

Pas d'équivalent pour les collections, il faut supprimer manuellement le dossier dans ~/.ansible/collections/ansible_collections/<namespace>/<collection>/.

Fenêtre de terminal
# Build une collection (génère .tar.gz)
cd path/to/collection/
ansible-galaxy collection build
# Publier sur Galaxy avec un token API
ansible-galaxy collection publish stephrobert-networking-1.0.0.tar.gz \
--api-key=$GALAXY_TOKEN

Token API disponible sur galaxy.ansible.com → Profil → API Token.

Fenêtre de terminal
# Vérifier l'intégrité d'une collection installée vs Galaxy
ansible-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.

VariableEffet
ANSIBLE_GALAXY_TOKENToken Galaxy par défaut (évite --api-key)
ANSIBLE_ROLES_PATHChemins de recherche des rôles
ANSIBLE_COLLECTIONS_PATHChemins de recherche des collections
ANSIBLE_GALAXY_SERVERURL d'un Automation Hub privé
[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.

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).

Fenêtre de terminal
cd ~/Projets/ansible-training/labs/galaxy/ansible-galaxy-cli/
cat cheatsheet.md
pytest -v challenge/tests/ # 7 tests
SymptômeCauseFix
Role not found après installroles_path mal configuré`ansible-config dump
Collection community.X planteDépendance non installéeansible-galaxy collection list pour voir
Token Galaxy invalideToken expiréRégénérer sur galaxy.ansible.com
role import (Galaxy auto-import) ne déclenche pasWebhook GitHub non configuréVérifier sur Galaxy → Imports
Collection installée 2 fois~/.ansible/ + projet localPréférer -p ./collections/ (local)
  • 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.

Ce site vous est utile ?

Sachez que moins de 1% des lecteurs soutiennent ce site.

Je maintiens +700 guides gratuits, sans pub ni tracking. Un soutien, même symbolique, m'aide à couvrir l'hébergement et à garder ces ressources gratuites. Merci pour votre appui.

Le formulaire ne s'affiche pas ? Ouvrir Ko-fi dans un onglet.

Abonnez-vous et suivez mon actualité DevSecOps sur LinkedIn