Aller au contenu
Infrastructure as Code medium

Versionner et publier un rôle Ansible : Git semver, Galaxy, Automation Hub

10 min de lecture

Logo Ansible

Une fois votre rôle stable et testé, l'étape finale est la publication : tag Git semver, CHANGELOG mis à jour, push sur Ansible Galaxy (public) ou Automation Hub (privé). Cette page couvre le workflow complet, de l'écriture du CHANGELOG à la release automatique via CI/CD.

  • Workflow Git semver : MAJOR.MINOR.PATCH.
  • Format CHANGELOG.md Keep a Changelog.
  • Publication sur Ansible Galaxy (méthodes webhook GitHub et CLI).
  • Automation Hub privé pour entreprise.
  • Release automatique via CI/CD (GitHub Actions ou GitLab CI).
MAJOR.MINOR.PATCH
│ │ └─ bugfix rétrocompatible (1.0.0 → 1.0.1)
│ └─────── nouvelle feature rétrocompatible (1.0.0 → 1.1.0)
└───────────── breaking change (1.0.0 → 2.0.0)

Règles :

  • MAJOR : variable renommée, structure cassée, suppression d'option.
  • MINOR : nouvelle variable optionnelle, nouvelle plateforme supportée.
  • PATCH : bugfix, typo, optimisation interne.

Format Keep a Changelog (keepachangelog.com) :

# Changelog — rôle webserver
## [1.2.0] - 2026-04-26
### Added
- Support multi-distribution (RHEL, AlmaLinux, Debian via vars/<os_family>.yml)
- Variable `webserver_worker_processes` configurable
- Validation argument_specs.yml
### Changed
- Module `package` (agnostique) au lieu de `dnf` direct
- Templates Jinja réorganisés pour lisibilité
### Fixed
- Idempotence handlers (Reload nginx déclenché 2× résolu)
## [1.1.0] - 2026-03-15
### Added
- Handlers Restart nginx et Reload nginx
- Validate sur le template nginx.conf
## [1.0.0] - 2026-02-01
### Added
- Premier release stable
- Installation nginx, démarrage, ouverture firewall HTTP

Sections standard : Added, Changed, Deprecated, Removed, Fixed, Security. Toutes optionnelles selon le contenu du release.

Fenêtre de terminal
# 1. Mettre à jour CHANGELOG.md
vim CHANGELOG.md
# Ajouter section ## [1.2.0] - 2026-04-26
# 2. Tester (Molecule + ansible-lint)
molecule test
ansible-lint --profile=production roles/webserver/
# 3. Commit
git add CHANGELOG.md
git commit -m "release: v1.2.0"
# 4. Tag semver
git tag -a v1.2.0 -m "Release v1.2.0 - Multi-distro support"
# 5. Push
git push origin main --tags

Note : tag avec préfixe v (v1.2.0, pas 1.2.0). Convention quasi-universelle.

  1. Compte sur galaxy.ansible.com lié à GitHub.
  2. Token API depuis votre profil → API Token.
  3. meta/main.yml complet avec galaxy_info.
  1. Sur Galaxy, My Content → Add Content → Import Role from GitHub.
  2. Sélectionner le repo stephrobert/ansible-role-webserver.
  3. Galaxy importe automatiquement à chaque nouveau tag Git semver.

Pas besoin de CLI, workflow GitOps natif.

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

Token disponible dans variables d'env CI/CD (ANSIBLE_GALAXY_TOKEN).

Pour les entreprises qui ne veulent pas exposer leurs rôles publiquement :

# ansible.cfg
[galaxy]
server_list = corporate_hub
[galaxy_server.corporate_hub]
url = https://hub.corp.example.com/api/automation-hub/
token = <token>

Avantages Automation Hub :

  • Privé : code interne non exposé sur Galaxy public.
  • Certifié Red Hat : collections supportées commercialement.
  • Allow-list : votre équipe sécurité valide chaque collection avant publication.
  • Mirror : réplique Galaxy public pour usage offline.
.github/workflows/release.yml
name: Release to Galaxy
on:
push:
tags: ['v*.*.*']
jobs:
release:
runs-on: ubuntu-24.04
permissions:
contents: read
steps:
- uses: actions/checkout@<SHA>
with:
persist-credentials: false
- name: Build and publish collection
run: |
ansible-galaxy collection build
ansible-galaxy collection publish *.tar.gz \
--api-key=${{ secrets.GALAXY_TOKEN }}

Token GitHub Secrets = GALAXY_TOKEN configuré dans Settings → Secrets.

# .gitlab-ci.yml (extrait)
release:
stage: release
rules:
- if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/
script:
- ansible-galaxy collection build
- ansible-galaxy collection publish *.tar.gz --api-key=$GALAXY_TOKEN

Token GitLab Variables = GALAXY_TOKEN (Masked + Protected).

Variables nouvelles, exemples mis à jour. Pas de drift entre code et doc.

Sur GitHub, Releases → Create a new release depuis le tag. Reprendre le contenu du CHANGELOG section correspondante.

Si MAJOR bump : ajouter un fichier MIGRATION.md qui détaille les changements et le pattern de migration pour les utilisateurs.

# Migration guide
## v1.x → v2.0
### Variable renommée
`webserver_port``webserver_listen_port`
Fix automatique :
```bash
sed -i 's/webserver_port/webserver_listen_port/g' playbooks/*.yml
## Pratiquer dans le lab
Cette page a un **lab d'accompagnement** : **`labs/galaxy/versionner-publier/`** dans
[stephrobert/ansible-training](https://github.com/stephrobert/ansible-training).
Le lab fournit `CHANGELOG.md` (3 versions semver) + `PUBLISH.md` (workflow complet). **7 tests structure** validés.
```bash
cd ~/Projets/ansible-training/labs/galaxy/versionner-publier/
cat CHANGELOG.md
cat PUBLISH.md
pytest -v challenge/tests/ # 7 tests
SymptômeCauseFix
Tag pushé mais Galaxy n'importe pasWebhook GitHub désactivéGalaxy → Imports → re-trigger manuel
ansible-galaxy collection publish échoueToken expiréRégénérer sur galaxy.ansible.com
Breaking change non signaléPas de bump MAJORToujours bump MAJOR + MIGRATION.md
Versions incohérentes entre code et tagPas de single source of truthMettre la version dans meta/main.yml ET tag Git
Release sur Galaxy privée mais code sur GitHub publicConfiguration mixteCohérent : code privé → Hub privé, code public → Galaxy public
  • Semver MAJOR.MINOR.PATCH = breaking, feature, bugfix.
  • Tag Git v1.2.0 + CHANGELOG.md Keep a Changelog.
  • Publication Galaxy : webhook GitHub (rôles) ou CLI (collections).
  • Automation Hub privé pour entreprise (privé, certifié Red Hat).
  • Release automatique via CI/CD (GitHub Actions / GitLab CI) sur tag.
  • MIGRATION.md mandatory sur tout breaking change.

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