From 902420a3ea7ad3c1b2c6b337458b814ac3d0102f Mon Sep 17 00:00:00 2001 From: Lukas Greve Date: Thu, 18 Sep 2025 20:24:38 +0200 Subject: [PATCH] Make enabling cloud_init optionnal For cloud images that needs it, cloud_init can be enabled in the main.tf file. This change will allow deployments that does not require cloud_init to be supported --- multiple/environments/fedora-cloud-server-42-bios/main.tf | 3 ++- .../environments/ubuntu-cloud-server-2404-bios/main.tf | 1 + .../environments/ubuntu-cloud-server-2404-uefi/main.tf | 1 + multiple/shared_modules/cloud-init.tf | 7 +++++-- multiple/shared_modules/domain.tf | 4 +++- multiple/shared_modules/variables.tf | 6 ++++++ 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/multiple/environments/fedora-cloud-server-42-bios/main.tf b/multiple/environments/fedora-cloud-server-42-bios/main.tf index 7421120..9a8908b 100644 --- a/multiple/environments/fedora-cloud-server-42-bios/main.tf +++ b/multiple/environments/fedora-cloud-server-42-bios/main.tf @@ -15,7 +15,8 @@ provider "libvirt" { module "shared_modules" { source = "../../shared_modules" - vm_name = "fedora-cloud-server-42-bios" + vm_name = "f42-bios" image_location = "https://download.fedoraproject.org/pub/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2" ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDMSuVlvOsMqx9qOrKKB1295FjCf2QhHfR1qola9brGkUcFL9dAztG2qdQnpiuPQ4OJpkedrO3C/ixEw1MLTL8l12SvYy/Q9QFguwylp35Nbw1p8h7jrX1FcNLRYltxkMgVhCs1InT5m0lf56bu1h7JfsMs7Ovsy3lU5OdK4h2MysTSKOLctsE4jDJ+XbJYQzj4rbfB/U7/9ple366cGl6xlaHxVfI4BUFWUOiVU4HWvZjrOM5fqPt+AUFRx1l2D7hLUZgOdVQwgO8GFn0sCyCIw0NCXbDn/H05pvWtTUPnyhj5TiseF8qW1byrrT5G8saxwvx8nbIK2tpPfKFdIiL7aj9bYQdltn1knJtvk3hpTPy4QvAbaoGfnfrPAsyU1A/CTw9SD/idvDT2wt1hVsm8EsnpovF7WT5z22fcgoFLDo+QCQrp7t1Wx0/Djay2nThi3FO3N051y5fQWoKOvTsm+rRhrzpDoc+Wtrtss3ua54qnQxHRx3YC0M5Xl9DINkwrcunbZBhozsDG2DzX9qcyzJsSfm9Zt5yM2lpcq+dGPRO1wedw4ogoOpobRr9Cja9W/lJvxmjgIiHz2HbSFPtk/VGjL6M7aQor/GDNN3ugSsfUoTTmNaS9+lWeg+tQWcFUPhYQtQB4/gHQ2u7+mQ0H3hVybsIKIh5XBpAdHQ7pww==" + enable_cloudinit = true } \ No newline at end of file diff --git a/multiple/environments/ubuntu-cloud-server-2404-bios/main.tf b/multiple/environments/ubuntu-cloud-server-2404-bios/main.tf index 7e786d2..5a9a946 100644 --- a/multiple/environments/ubuntu-cloud-server-2404-bios/main.tf +++ b/multiple/environments/ubuntu-cloud-server-2404-bios/main.tf @@ -18,4 +18,5 @@ module "shared_modules" { vm_name = "u24-bios" image_location = "/var/lib/libvirt/images/noble-server-cloudimg-amd64.img" ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDMSuVlvOsMqx9qOrKKB1295FjCf2QhHfR1qola9brGkUcFL9dAztG2qdQnpiuPQ4OJpkedrO3C/ixEw1MLTL8l12SvYy/Q9QFguwylp35Nbw1p8h7jrX1FcNLRYltxkMgVhCs1InT5m0lf56bu1h7JfsMs7Ovsy3lU5OdK4h2MysTSKOLctsE4jDJ+XbJYQzj4rbfB/U7/9ple366cGl6xlaHxVfI4BUFWUOiVU4HWvZjrOM5fqPt+AUFRx1l2D7hLUZgOdVQwgO8GFn0sCyCIw0NCXbDn/H05pvWtTUPnyhj5TiseF8qW1byrrT5G8saxwvx8nbIK2tpPfKFdIiL7aj9bYQdltn1knJtvk3hpTPy4QvAbaoGfnfrPAsyU1A/CTw9SD/idvDT2wt1hVsm8EsnpovF7WT5z22fcgoFLDo+QCQrp7t1Wx0/Djay2nThi3FO3N051y5fQWoKOvTsm+rRhrzpDoc+Wtrtss3ua54qnQxHRx3YC0M5Xl9DINkwrcunbZBhozsDG2DzX9qcyzJsSfm9Zt5yM2lpcq+dGPRO1wedw4ogoOpobRr9Cja9W/lJvxmjgIiHz2HbSFPtk/VGjL6M7aQor/GDNN3ugSsfUoTTmNaS9+lWeg+tQWcFUPhYQtQB4/gHQ2u7+mQ0H3hVybsIKIh5XBpAdHQ7pww==" + enable_cloudinit = true } \ No newline at end of file diff --git a/multiple/environments/ubuntu-cloud-server-2404-uefi/main.tf b/multiple/environments/ubuntu-cloud-server-2404-uefi/main.tf index 2e8209c..b09558c 100644 --- a/multiple/environments/ubuntu-cloud-server-2404-uefi/main.tf +++ b/multiple/environments/ubuntu-cloud-server-2404-uefi/main.tf @@ -18,6 +18,7 @@ module "shared_modules" { vm_name = "u24-uefi" image_location = "/var/lib/libvirt/images/noble-server-cloudimg-amd64.img" ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDMSuVlvOsMqx9qOrKKB1295FjCf2QhHfR1qola9brGkUcFL9dAztG2qdQnpiuPQ4OJpkedrO3C/ixEw1MLTL8l12SvYy/Q9QFguwylp35Nbw1p8h7jrX1FcNLRYltxkMgVhCs1InT5m0lf56bu1h7JfsMs7Ovsy3lU5OdK4h2MysTSKOLctsE4jDJ+XbJYQzj4rbfB/U7/9ple366cGl6xlaHxVfI4BUFWUOiVU4HWvZjrOM5fqPt+AUFRx1l2D7hLUZgOdVQwgO8GFn0sCyCIw0NCXbDn/H05pvWtTUPnyhj5TiseF8qW1byrrT5G8saxwvx8nbIK2tpPfKFdIiL7aj9bYQdltn1knJtvk3hpTPy4QvAbaoGfnfrPAsyU1A/CTw9SD/idvDT2wt1hVsm8EsnpovF7WT5z22fcgoFLDo+QCQrp7t1Wx0/Djay2nThi3FO3N051y5fQWoKOvTsm+rRhrzpDoc+Wtrtss3ua54qnQxHRx3YC0M5Xl9DINkwrcunbZBhozsDG2DzX9qcyzJsSfm9Zt5yM2lpcq+dGPRO1wedw4ogoOpobRr9Cja9W/lJvxmjgIiHz2HbSFPtk/VGjL6M7aQor/GDNN3ugSsfUoTTmNaS9+lWeg+tQWcFUPhYQtQB4/gHQ2u7+mQ0H3hVybsIKIh5XBpAdHQ7pww==" + enable_cloudinit = true # ---- OPTIONAL UEFI SETTINGS ---------------------------------------------- uefi_firmware = "/usr/share/edk2/x64/OVMF_CODE.4m.fd" uefi_nvram_template = "/usr/share/edk2/x64/OVMF_VARS.4m.fd" diff --git a/multiple/shared_modules/cloud-init.tf b/multiple/shared_modules/cloud-init.tf index 8b52949..346064a 100644 --- a/multiple/shared_modules/cloud-init.tf +++ b/multiple/shared_modules/cloud-init.tf @@ -1,8 +1,11 @@ +# Only create the cloudinit disk if enabled resource "libvirt_cloudinit_disk" "commoninit" { - name = var.cloudinit_filename + count = var.enable_cloudinit ? var.instance_count : 0 + + name = "${var.cloudinit_filename}-${count.index}" user_data = templatefile("${path.module}/cloud_init.yaml", { ssh_key = var.ssh_key }) - pool = "${var.vm_name}-pool" + pool = "${var.vm_name}-pool" depends_on = [libvirt_pool.tf_tmp_storage] } \ No newline at end of file diff --git a/multiple/shared_modules/domain.tf b/multiple/shared_modules/domain.tf index b2e50be..e6006db 100644 --- a/multiple/shared_modules/domain.tf +++ b/multiple/shared_modules/domain.tf @@ -3,7 +3,9 @@ resource "libvirt_domain" "domain" { name = "${var.vm_name}-${count.index}" memory = var.memory vcpu = var.vcpu - cloudinit = libvirt_cloudinit_disk.commoninit.id + + # Only include cloudinit if enabled + cloudinit = var.enable_cloudinit ? libvirt_cloudinit_disk.commoninit[count.index].id : null # ---- optional UEFI support ------------------------------------ # Firmware – only add the string when a path is supplied diff --git a/multiple/shared_modules/variables.tf b/multiple/shared_modules/variables.tf index 6df61da..9dcf592 100644 --- a/multiple/shared_modules/variables.tf +++ b/multiple/shared_modules/variables.tf @@ -40,6 +40,12 @@ variable "cloudinit_filename" { default = "commoninit.iso" } +variable "enable_cloudinit" { + description = "Enable cloud-init support" + type = bool + default = false +} + variable "ssh_key" { description = "SSH authorized keys for cloud-init" type = string