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.hcloudLe token est passé via variable d’environnement :
export HCLOUD_TOKEN="votre_token"
ansible-inventory -i hcloud.yml --list --yamlPipeline 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.yamlPoints 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: runningTest local avec act
act push -s HCLOUD_TOKEN="token" --container-architecture linux/amd64