Terraform & Proxmox
Le 2023-12-07
Avant-Propos
Terraform est un outil IAC (Infrastructure As Code) développé par Hashicorps.
Il permet grâce à une syntaxe déclarative relativement simple de créer une infrastructure dans le cloud ou en on-premise. Terraform dispose de nombreux fournisseurs (providers) adaptés aux principaux environnements du marché (kubernetes, Azure, AWS, GCP, Openstack, Vsphere, Proxmox, …)
Il est habituellement utilisé pour créer des VM ensuite configurées par des playbooks Ansible
Installation de Terraform
Sur une Debian 12 Bookworm, la dernière version est la 1.6.5 (au 7/12/2023)
sudo apt install software-properties-common curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main" sudo apt update sudo apt install terraform ... terraform --version Terraform v1.6.5 on linux_amd64
Mise en oeuvre de Terraform avec le provider Telmate/Proxmox
L'objectif est ici d'utiliser Terraform pour déployer automatiquement des VM KVM sur un serveur Proxmox 7.
4 étapes : init, plan, apply, destroy
Les commandes de base Terraform :
- init : Prépare le répertoire pour Terraform
- validate : vérifie la validité de la configuration
- plan : montre les changements requis par la configuration
- apply : crée ou met à jour l'infrastructure
- destroy : détruit l'infrastructure précédemment créée
1 - init
mkdir infra-prj && cd infra-prj touch main.tf vars.tf
le fichier provider.tf (minimal) - On utilise ici le provider Telmate/Proxmox (plus maintenu depuis la version 2.9.14 et incompatible avec Proxmox 8.1)
terraform {
required_providers {
proxmox = {
source = "telmate/proxmox"
version = "2.9.14"
}
}
}
On initialise le répertoire projet avec terraform init:
# terraform init Initializing the backend... Initializing provider plugins... - Finding telmate/proxmox versions matching "2.9.11"... - Installing telmate/proxmox v2.9.11... - Installed telmate/proxmox v2.9.11 (self-signed, key ID A9EBBE091B35AFCE) Partner and community providers are signed by their developers. If you'd like to know more about provider signing, you can read about it here: https://www.terraform.io/docs/cli/plugins/signing.html Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
2 - Validate
le fichier main.tf : il décrit les ressources (VMs, conteneurs, …) à créer
provider "proxmox" { # description de la connexion au serveur proxmox
pm_tls_insecure = true
pm_api_url = "https://proxmox-host-ip-or-hostname:8006/api2/json"
pm_user = "root@pam"
pm_password = "some-password"
}
resource "proxmox_vm_qemu" "cloudinit-test" {
name = "tf-test"
desc = "testing terraform proxmox plugin"
target_node = "pve" # la machine Proxmox cible
clone = "TemplateDebian11" # le nom de la template a cloner
cores = 1
sockets = 1
memory = 1024
ipconfig0 = "gw=x.x.x.x,ip=y.y.y.y/24" # ou bien ip=dhcp
ssh_user = "debian" # le compte SSH de connexion par défaut
sshkeys = <<EOF
ssh-rsa somekey user@host
EOF
network {
# id = 0
model = "virtio"
bridge = "vmbr0" # le bridge standard pour l'interface réseau
}
disk {
# id = 0
typ:e = "scsi"
storage = "local-lvm"
storage_type = "qcow2"
size = "8G"
format = "raw"
}
}
Pour valider la configuration du fichier:
terraform validate
Pour afficher les changements qui seront réalisés :
terraform plan
3. Apply
Pour lancer :
terraform apply
