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
This commit is contained in:
Lukas Greve
2025-09-18 20:24:38 +02:00
parent 75b2ae6b40
commit 902420a3ea
6 changed files with 18 additions and 4 deletions

View File

@@ -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]
}

View File

@@ -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

View File

@@ -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