🚀 Workflow Terraform : Gestion des Conteneurs LXC
Ce guide explique comment transformer un conteneur Proxmox existant (ou nouveau) en un objet géré par le code (Infrastructure as Code).
1. Actions Ă faire UNE SEULE FOIS (Setup)
A. Préparation de l'environnement (Terminal)
Avant de lancer Terraform, il faut charger les accès de sécurité dans la session actuelle :
# Token API Proxmox
export PROXMOX_VE_API_TOKEN='USER@REALM!TOKENID=UUID'terraform@pam!awx-provisioning=c20efe74-ec94-4623-a53d-613ad538fcc9'
# Variables pour les fichiers Terraform (Préfixe TF_VAR_)
export TF_VAR_lxc_root_password='Ghilghame$h8863!'
export TF_VAR_admin_ssh_key='ssh-ed25519 AAAAC3Nza...'
B. Initialisation du dossier Live
Se placer dans infra/terraform/live/02-lxcs/ et télécharger le connecteur Proxmox :
terraform init
2. Actions à RÉPÉTER (Pour chaque conteneur)
Pour chaque nouveau service (ex: Bitwarden, Bookstack), suivez ce cycle : Coder -> Adopter -> Vérifier.
Étape A : Créer le fichier de configuration
Créer un fichier par service (ex: bitwarden.tf) dans live/02-lxcs/ :
module "bitwarden" {
source = "../../modules/proxmox_lxc"
hostname = "bitwarden"
vmid = 119
target_node = "proxmox"
pool = "SERVICES"
cores = 2
memory = 1024
disk_size = 8
disk_datastore = "ssd1tb"
ip_address = "10.151.151.231/24"
gateway = "10.151.151.1"
password = var.lxc_root_password
ssh_public_keys = var.admin_ssh_key
template_file_id = "ssd1tb:vztmpl/debian-12-standard..."
}
Étape B : L'adoption (Uniquement pour l'existant)
Si le conteneur existe déjà sur Proxmox, il faut le "capturer" dans la mémoire de Terraform :
# Syntaxe : terraform import module.NOM_DU_MODULE.proxmox_virtual_environment_container.lxc NOEUD/VMID
terraform import module.bitwarden.proxmox_virtual_environment_container.lxc proxmox/119
Étape C : La vérification
Comparer le code avec la réalité. Le résultat doit être "0 to add, 0 to change" :
terraform plan
3. Récapitulatif des fichiers types (.tf)
| Fichier | RĂ´le | Contenu type |
|---|---|---|
providers.tf |
Connexion API | provider "proxmox" { ... } |
variables.tf |
Déclaration des entrées | variable "lxc_root_password" { ... } |
main.tf (ou service.tf) |
Le bon de commande | module "nom" { source = "..." } |
⚠️ Rappel crucial : La commande
import ne se fait qu'une seule fois par machine. Une fois importée, toute modification doit se faire dans le fichier .tf suivi d'un terraform apply.