Collections et modules personnalises 26 min de lecture

Collections Ansible et Galaxy

Qu'est-ce qu'une Collection ?

Une collection est le format de distribution standard pour le contenu Ansible. Elle peut contenir :

  • Des modules
  • Des roles
  • Des plugins (filter, lookup, callback, inventory)
  • De la documentation
  • Des playbooks

Structure d'une collection

mon_namespace/
  ma_collection/
    galaxy.yml          # Metadonnees de la collection
    README.md
    plugins/
      modules/          # Modules personnalises
        mon_module.py
      filter/           # Plugins de filtre
        mes_filtres.py
      lookup/           # Plugins de lookup
        mon_lookup.py
    roles/
      mon_role/         # Roles inclus
        tasks/
          main.yml
    docs/
    tests/

Fichier galaxy.yml

namespace: mon_entreprise
name: infra
version: 1.2.0
readme: README.md
authors:
  - Equipe DevOps
description: Collection d'automatisation infrastructure
license:
  - GPL-3.0-or-later
dependencies:
  community.general: ">=5.0.0"
  ansible.posix: ">=1.4.0"
repository: https://github.com/mon-entreprise/ansible-infra
tags:
  - infrastructure
  - linux
  - automation

Creer une collection

# Initialiser la structure
ansible-galaxy collection init mon_entreprise.infra

# Construire l'archive
cd mon_entreprise/infra
ansible-galaxy collection build

# Resultat : mon_entreprise-infra-1.2.0.tar.gz

Installer des collections

# Depuis Galaxy
ansible-galaxy collection install community.general

# Depuis un fichier
ansible-galaxy collection install mon_entreprise-infra-1.2.0.tar.gz

# Depuis un depot Git
ansible-galaxy collection install git+https://github.com/user/collection.git

# Fichier requirements.yml
collections:
  - name: community.docker
    version: ">=3.0.0"
  - name: kubernetes.core
    version: "2.4.0"
  - name: mon_entreprise.infra
    source: https://galaxy.example.com

Publier sur Ansible Galaxy

# Obtenir un token sur galaxy.ansible.com
ansible-galaxy collection publish \
  mon_entreprise-infra-1.2.0.tar.gz \
  --api-key=votre_token_galaxy
Organisation : Les collections remplacent les anciens roles Galaxy comme moyen principal de distribution du contenu Ansible.