Inventaire dynamique Hetzner Cloud avec Ansible et Gitea Actions

devops
ansible
hetzner
gitea
Author

Sylvain Pham

Published

December 29, 2025

Automatiser la récupération de l’inventaire des VMs Hetzner Cloud avec Ansible, sans maintenir de fichier statique. Les serveurs sont automatiquement groupés par datacenter et labels.

Configuration

Fichier hcloud.yml :

plugin: hetzner.hcloud.hcloud

Le token est passé via variable d’environnement :

export HCLOUD_TOKEN="votre_token"
ansible-inventory -i hcloud.yml --list --yaml

Pipeline Gitea Actions

name: List Hcloud Inventory

on:
  push:
  workflow_dispatch:

jobs:
  inventory:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install dependencies
        run: |
          python3 -m venv venv
          source venv/bin/activate
          pip install ansible hcloud

      - name: List inventory
        env:
          HCLOUD_TOKEN: ${{ secrets.HCLOUD_TOKEN }}
        run: |
          source venv/bin/activate
          ansible-inventory -i hcloud.yml --list --yaml | tee inventory.yaml

      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: hcloud-inventory
          path: inventory.yaml

Points clés

Contrainte Solution
PEP 668 (pip bloqué) Utiliser python3 -m venv
upload-artifact@v4 Non supporté Gitea, utiliser v3
Token sécurisé Secret Gitea, pas en dur

Groupes générés

  • hcloud : tous les serveurs
  • Par datacenter : hel1, fsn1, nbg1
  • Par labels personnalisés

Exemple de sortie

all:
  children:
    hcloud:
      hosts:
        mon-serveur:
          ansible_host: 157.180.x.x
          hcloud_server_type: cx22
          hcloud_datacenter: hel1-dc2
          hcloud_status: running

Test local avec act

act push -s HCLOUD_TOKEN="token" --container-architecture linux/amd64

Liens