Cleaning VM Creation
- Remove useless tf files - add extra disks capacity
This commit is contained in:
132
terraform/prod/01-virtual-machines.tf
Normal file
132
terraform/prod/01-virtual-machines.tf
Normal file
@@ -0,0 +1,132 @@
|
||||
# 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 et préparation des données pour Ansible
|
||||
locals {
|
||||
# OS supportés
|
||||
supported_os = toset(["ubuntu", "debian", "alpine", "rocky", "centos"])
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
ansible_vms = {
|
||||
for key, vm in proxmox_virtual_environment_vm.vms : key => {
|
||||
# Données issues de la ressource VM créée
|
||||
name = vm.name
|
||||
proxmox_node = vm.node_name
|
||||
|
||||
# Données issues de vos variables d'entrée
|
||||
ip_address = var.virtual_machines[key].ip
|
||||
os_distro = var.virtual_machines[key].os
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# 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)
|
||||
|
||||
# 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 directement
|
||||
# le réseau et l'utilisateur, sans fichier user_data externe.
|
||||
initialization {
|
||||
# Configuration IP
|
||||
ip_config {
|
||||
ipv4 {
|
||||
address = "${each.value.ip}/${lookup(each.value, "netmask", "24")}"
|
||||
gateway = each.value.gateway
|
||||
}
|
||||
}
|
||||
|
||||
# Configuration utilisateur
|
||||
user_account {
|
||||
username = lookup(each.value, "username", var.default_vm_user)
|
||||
keys = [file(var.admin_ssh_public_key_path)]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# 5. 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Génération automatique de l'inventaire Ansible
|
||||
resource "local_file" "ansible_inventory" {
|
||||
filename = "${path.module}/../../ansible/inventory/01_lab_hosts.yml"
|
||||
|
||||
content = templatefile("${path.module}/templates/inventory.tftpl", {
|
||||
vms = local.ansible_vms
|
||||
})
|
||||
|
||||
depends_on = [proxmox_virtual_environment_vm.vms]
|
||||
}
|
||||
Reference in New Issue
Block a user