From 1dea1acaa0a31a74d81e19eec04f318673e821d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Sun, 9 Nov 2025 19:49:51 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Improve=20Proxmox=20sup?= =?UTF-8?q?port?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first iteration on Proxmox support relied on a single template image hosted on shared storage. This new iteration relies on template images hosted on local storage. It will detect the template VM to use on each node thanks to its tags. Note: later, we'll need to expose an easy way to switch between shared-store and local-store template images. --- .../virtual-machines/proxmox/main.tf | 49 ++++++++++++++++--- .../virtual-machines/proxmox/provider.tf | 2 +- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/prepare-labs/terraform/virtual-machines/proxmox/main.tf b/prepare-labs/terraform/virtual-machines/proxmox/main.tf index b74c8754..a978b5f1 100644 --- a/prepare-labs/terraform/virtual-machines/proxmox/main.tf +++ b/prepare-labs/terraform/virtual-machines/proxmox/main.tf @@ -1,12 +1,34 @@ data "proxmox_virtual_environment_nodes" "_" {} +data "proxmox_virtual_environment_vms" "_" { + filter { + name = "template" + values = [true] + } +} + +data "proxmox_virtual_environment_vms" "templates" { + for_each = toset(data.proxmox_virtual_environment_nodes._.names) + tags = ["ubuntu"] + filter { + name = "node_name" + values = [each.value] + } + filter { + name = "template" + values = [true] + } +} + locals { - pve_nodes = data.proxmox_virtual_environment_nodes._.names + pve_nodes = data.proxmox_virtual_environment_nodes._.names + pve_node = { for k, v in local.nodes : k => local.pve_nodes[v.node_index % length(local.pve_nodes)] } + pve_template_id = { for k, v in local.nodes : k => data.proxmox_virtual_environment_vms.templates[local.pve_node[k]].vms[0].vm_id } } resource "proxmox_virtual_environment_vm" "_" { - node_name = local.pve_nodes[each.value.node_index % length(local.pve_nodes)] for_each = local.nodes + node_name = local.pve_node[each.key] name = each.value.node_name tags = ["container.training", var.tag] stop_on_destroy = true @@ -24,9 +46,17 @@ resource "proxmox_virtual_environment_vm" "_" { # size = 30 # discard = "on" #} + ### Strategy 1: clone from shared storage + #clone { + # vm_id = var.proxmox_template_vm_id + # node_name = var.proxmox_template_node_name + # full = false + #} + ### Strategy 2: clone from local storage + ### (requires that the template exists on each node) clone { - vm_id = var.proxmox_template_vm_id - node_name = var.proxmox_template_node_name + vm_id = local.pve_template_id[each.key] + node_name = local.pve_node[each.key] full = false } agent { @@ -41,7 +71,9 @@ resource "proxmox_virtual_environment_vm" "_" { ip_config { ipv4 { address = "dhcp" - #gateway = + } + ipv6 { + address = "dhcp" } } } @@ -72,8 +104,11 @@ resource "proxmox_virtual_environment_vm" "_" { locals { ip_addresses = { for key, value in local.nodes : - key => [for addr in flatten(concat(proxmox_virtual_environment_vm._[key].ipv4_addresses, ["ERROR"])) : - addr if addr != "127.0.0.1"][0] + key => [for addr in flatten(concat( + proxmox_virtual_environment_vm._[key].ipv6_addresses, + proxmox_virtual_environment_vm._[key].ipv4_addresses, + ["ERROR"])) : + addr if addr != "127.0.0.1" && addr != "::1"][0] } } diff --git a/prepare-labs/terraform/virtual-machines/proxmox/provider.tf b/prepare-labs/terraform/virtual-machines/proxmox/provider.tf index 33790a5d..8085bd13 100644 --- a/prepare-labs/terraform/virtual-machines/proxmox/provider.tf +++ b/prepare-labs/terraform/virtual-machines/proxmox/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { proxmox = { source = "bpg/proxmox" - version = "~> 0.70.1" + version = "~> 0.86.0" } } }