112 lines
3.0 KiB
HCL
112 lines
3.0 KiB
HCL
# Fichier: terraform/prod/01-virtual-machines.tf
|
|
# Description: Déploiement des machines virtuelles sur Proxmox
|
|
|
|
# 1. Récupération des templates disponibles sur Proxmox
|
|
data "proxmox_virtual_environment_vms" "templates" {
|
|
node_name = var.target_node
|
|
tags = ["template"]
|
|
}
|
|
|
|
# 2. Mapping des templates
|
|
locals {
|
|
# OS supportés
|
|
supported_os = toset(["ubuntu", "debian", "alpine", "rocky", "ipfabric"])
|
|
|
|
# Map des templates par OS
|
|
template_map = {
|
|
for template in data.proxmox_virtual_environment_vms.templates.vms :
|
|
one(setintersection(local.supported_os, template.tags)) => template
|
|
if length(setintersection(local.supported_os, template.tags)) > 0
|
|
}
|
|
}
|
|
|
|
# 3. Création des machines virtuelles
|
|
resource "proxmox_virtual_environment_vm" "vms" {
|
|
for_each = var.virtual_machines
|
|
|
|
name = "${each.key}.${var.domain_suffix}"
|
|
node_name = var.target_node
|
|
tags = concat(["terraform-managed", "vm"], each.value.tags)
|
|
on_boot = lookup(each.value, "on_boot", true)
|
|
stop_on_destroy = true
|
|
|
|
# Configuration de l'agent QEMU
|
|
agent {
|
|
enabled = true
|
|
}
|
|
|
|
# Configuration CPU
|
|
cpu {
|
|
cores = each.value.cores
|
|
type = lookup(each.value, "cpu_type", "host")
|
|
}
|
|
|
|
# Configuration mémoire
|
|
memory {
|
|
dedicated = each.value.memory
|
|
}
|
|
|
|
# Configuration disque principal
|
|
disk {
|
|
interface = "scsi0"
|
|
datastore_id = lookup(each.value, "datastore", "local-lvm")
|
|
size = each.value.disk_gb
|
|
file_format = lookup(each.value, "disk_format", "raw")
|
|
}
|
|
|
|
# Disques supplémentaires (optionnel)
|
|
dynamic "disk" {
|
|
for_each = lookup(each.value, "additional_disks", [])
|
|
content {
|
|
interface = disk.value.interface
|
|
datastore_id = disk.value.datastore
|
|
size = disk.value.size
|
|
file_format = lookup(disk.value, "format", "raw")
|
|
}
|
|
}
|
|
|
|
# Configuration réseau
|
|
network_device {
|
|
bridge = lookup(each.value, "network_bridge", "vmbr0")
|
|
model = lookup(each.value, "network_model", "virtio")
|
|
}
|
|
|
|
# Clonage depuis le template
|
|
clone {
|
|
vm_id = local.template_map[each.value.os].vm_id
|
|
}
|
|
|
|
# On utilise le bloc 'initialization' pour configurer Cloud-Init
|
|
initialization {
|
|
# Configuration DNS (nécessaire au bootstrap : apt, curl, install Tailscale...)
|
|
dns {
|
|
servers = var.dns_servers
|
|
}
|
|
|
|
# Configuration IP
|
|
ip_config {
|
|
ipv4 {
|
|
address = "${each.value.ip}/${lookup(each.value, "netmask", "24")}"
|
|
gateway = each.value.gateway
|
|
}
|
|
}
|
|
|
|
# Données Cloud-Init via le fichier de template
|
|
user_data_file_id = proxmox_virtual_environment_file.cloud_config[each.key].id
|
|
}
|
|
}
|
|
|
|
# 4. Outputs pour récupérer les informations des VMs
|
|
output "vm_info" {
|
|
description = "Informations des machines virtuelles créées"
|
|
value = {
|
|
for k, v in proxmox_virtual_environment_vm.vms : k => {
|
|
id = v.id
|
|
name = v.name
|
|
node = v.node_name
|
|
ip = var.virtual_machines[k].ip
|
|
mac = v.network_device[0].mac_address
|
|
}
|
|
}
|
|
}
|