Description
Terraform est un outil open source écrit en go par Hashicorp
Le code source est disponible sur Github à cette adresse : Lien repo
Installation on Debian12
| sudo apt-get update && sudo apt-get install -y gnupg2 curl
|
| curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmour -o /usr/share/keyrings/terraform.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/terraform.gpg] https://apt.releases.hashicorp.com bookworm main" | sudo tee -a /etc/apt/sources.list.d/terraform.list
|
- Mise à jour des packages et installation de terraform
| sudo apt update
sudo apt install terraform
|
- Ajout de l'auto-complétions aux commandes Terraform
| terraform -install-autocomplete
|
Vérification de l'installation
| terraform --version
Terraform v1.5.4
on linux_amd64
|
Gestion des workspace
| terraform workspace new proxmox
Created and switched to workspace "proxmox"!
You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
|
| terraform workspace list
default
* proxmox
|
- Afficher le workspace courant
| terraform workspace show
proxmox
|
| terraform workspace select default
Switched to workspace "default".
|
| terraform workspace delete proxmox
Deleted workspace "proxmox"!
|
Utilisation du provider Proxmox
Utilisateur dédié
Création d'un utilisateur dédié dans proxmox pour ne pas utiliser le compte root.
| sudo pveum useradd terraform-user@pve
|
Attribution d'un mot de passe à cet utilisateur
| sudo pveum passwd terraform-user@pve
Enter new password: ************************************
Retype new password: ************************************
|
Création d'un rôle dédié sur proxmox
| sudo pveum role add TerraformRole -privs "Datastore.Allocate Datastore.AllocateSpace Datastore.Audit Pool.Allocate Sys.Audit Sys.Console Sys.Modify VM.Allocate VM.Audit VM.Clone VM.Config.CDROM VM.Config.Cloudinit VM.Config.CPU VM.Config.Disk VM.Config.HWType VM.Config.Memory VM.Config.Network VM.Config.Options VM.Console VM.Migrate VM.Monitor VM.PowerMgmt SDN.Use"
|
Attribution du rôle à notre utilisateur crée précédemment
| sudo pveum aclmod / -user terraform-user@pve -role TerraformRole
|
Arborescence
Création de l'arborescence des fichiers nécessaires à Terraform pour interagir avec proxmox
| ls -l
main.tf
variables.tf
|
Le fichier main.tf va contenir les informations du provider utilisé :
| cat main.tf
terraform {
required_providers {
proxmox = {
source = "Telmate/proxmox"
version = "2.9.14"
}
}
}
provider "proxmox" {
pm_parallel = 1
pm_tls_insecure = true
pm_api_url = var.pm_api_url
pm_user = var.pm_user
pm_password = var.pm_password
}
|
Le fichier variables.tf va venir peupler les variables qui seront utilisés par terraform:
| cat variables.tf
variable "admin_user" {
default = "<USER_REDACTED>"
}
variable "pm_api_url" {
default = "<URL_REDACTED>/api2/json"
}
variable "pm_user" {
default = "terraform-user@pve"
}
variable "pm_password" {
default = "<PASSWORD_REDACTED>"
}
variable "private_key" {
default = "<CHEMIN_VERS_PRIVATE_KEY_SSH_REDACTED>"
}
|
On peut maintenant tester que la configuration de terraform est bonne qu'il se connecte bien à l'api de proxmox.
| terraform plan
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
|
Cette commande n'effectue pas de modification sur proxmox, elle sert à planifier les actions qui devront être réalisées.
Déploiement LXC
Afin de déployer un conteneur , il faut tout d'abord télécharger les templates disponibles sur proxmox:

Nous pouvons maintenant créer le fichier permettant la déclaration de notre premier conteneur lxc :
La documentation est disponible sur le site de terraform pour savoir quels paramètres doivent être déclarés: https://registry.terraform.io/providers/Telmate/proxmox/latest/docs/resources/lxc
| vi lxc-dns.tf
resource "proxmox_lxc" "lxc-dns" {
target_node = "REDACTED"
vmid = "1"
hostname = "lxc-dns.internal"
description = "Test deploiement terraform"
ostemplate = "local:vztmpl/debian-12-standard_12.7-1_amd64.tar.zst"
memory = "512"
swap = "512"
arch = "amd64"
cores = "1"
cpulimit = "0"
cpuunits = "100"
password = "MySuperSecretPassword"
unprivileged = true
nameserver = "10.0.0.3"
onboot = false
ostype = "debian"
start = true
startup = "order=1,up=15"
ssh_public_keys = <<-EOT
<REDACTED_PUBLIC_KEY>
EOT
rootfs {
storage = "local"
size = "4G"
}
features {
nesting = "1"
}
network {
name = "eth0"
bridge = "vmbr1"
ip = "<REDACTED_IP>/24"
gw = "<REDACTED_GW>"
firewall = false
}
}
|
Maintenant l'arborescence devrait ressembler à ça:
| ls -l
lxc-dns.tf
main.tf
variables.tf
|
On va pouvoir relancer la commande terraform plan pour voir si ce nouveau fichier a bien été pris en compte:
| terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# proxmox_lxc.lxc-dns will be created
+ resource "proxmox_lxc" "lxc-dns" {
+ arch = "amd64"
+ cmode = "tty"
+ console = true
..........
..........
+ rootfs {
+ size = "4G"
+ storage = "local"
+ volume = (known after apply)
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
|
Le fichier de déclaration de notre nouveau conteneur a bien été pris en compte, terraform nous informe qu'il va devoir l'ajouter avec les paramètres déclarés.
Il ne nous reste plus qu'à lancer le déploiement avec la commande terraform apply
| terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# proxmox_lxc.lxc-dns will be created
+ resource "proxmox_lxc" "lxc-dns" {
+ arch = "amd64"
+ cmode = "tty"
+ console = true
+ cores = 1
+ cpulimit = 0
........
........
|
Il faudra confirmer que c'est bien l'action d'ajour qu'on veut effectuer:
| Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
|
L'action se poursuit jusqu'à la création du conteneur.
| proxmox_lxc.lxc-dns: Creating...
proxmox_lxc.lxc-dns: Still creating... [10s elapsed]
proxmox_lxc.lxc-dns: Creation complete after 42s
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
|
On peut constater que le conteneur a bien été créé dans Proxmox.
Suppression des ressources déployées
Effacement total
Afin de supprimer toutes les ressources déployées on peut utiliser la commande terraform destroy.
Attention cela va complètement supprimer tous les conteneurs ou machines virtuelles existantes.
| $ terraform destroy
proxmox_lxc.lxc-dns: Refreshing state...
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# proxmox_lxc.lxc-dns will be destroyed
- resource "proxmox_lxc" "lxc-dns" {
- arch = "amd64" -> null
- bwlimit = 0 -> null
- cmode = "tty" -> null
- console = true -> null
- cores = 1 -> null
........
........
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
|
Saisir "yes" afin de confirmer la suppression des ressources une fois les informations affichées vérifiées.
Effacement que de certaines ressources
Commencer par lister les ressources déployées avec terraform:
| $ terraform state list
proxmox_lxc.lxc-dns
proxmox_lxc.lxc-test
|
Sélectionner l'élément souhaité et le passer en argument de la commande de destruction.
| $ terraform destroy -target=proxmox_lxc.lxc-test
proxmox_lxc.lxc-test: Refreshing state...
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# proxmox_lxc.lxc-test will be destroyed
- resource "proxmox_lxc" "lxc-test" {
- arch = "amd64" -> null
- bwlimit = 0 -> null
- cmode = "tty" -> null
- console = true -> null
- cores = 1 -> null
..........
..........
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
proxmox_lxc.lxc-test: Destroying...
proxmox_lxc.lxc-test: Destruction complete after 4s
Destroy complete! Resources: 1 destroyed.
|