diff --git a/shared_modules/domain.tf b/shared_modules/domain.tf index dc3205f..f8a309a 100644 --- a/shared_modules/domain.tf +++ b/shared_modules/domain.tf @@ -20,19 +20,15 @@ resource "libvirt_domain" "domain" { # ---- optional UEFI support ------------------------------------ # Firmware – only add the string when a path is supplied - firmware = can(var.uefi_firmware) && length(var.uefi_firmware) > 0 ? var.uefi_firmware : null + firmware = local.detected_firmware # NVRAM block – dynamic block that is evaluated once per VM dynamic "nvram" { - # create the block once if a firmware path *and* a template were given - for_each = (can(var.uefi_firmware) && length(var.uefi_firmware) > 0 - && can(var.uefi_nvram_template) && length(var.uefi_nvram_template) > 0 - ) ? [1] : [] + for_each = (local.detected_firmware != null && local.detected_nvram != null) ? [1] : [] content { - # The NVRAM filename is per‑VM, but we can honour an optional suffix file = "/var/lib/libvirt/qemu/nvram/${var.vm_name}-${count.index}${var.uefi_nvram_file_suffix}_VARS.fd" - template = var.uefi_nvram_template + template = local.detected_nvram } } # ---------------------------------------------------------------- diff --git a/shared_modules/variables.tf b/shared_modules/variables.tf index 1580a9c..869ae14 100644 --- a/shared_modules/variables.tf +++ b/shared_modules/variables.tf @@ -105,23 +105,25 @@ variable "dns_local_only" { default = false } +# Improved UEFI variables with automatic detection variable "uefi_firmware" { description = < true if fileexists(path) + } + + # Function to get first available firmware path or null + detected_firmware = ( + var.uefi_firmware == "true" || var.uefi_firmware == "" ? ( + length(local.find_first_existing) > 0 ? + keys(local.find_first_existing)[0] : + null + ) : ( + var.uefi_firmware != "" ? var.uefi_firmware : null + ) + ) + + # Same for NV-RAM template + find_first_nvram = { + for path in local.uefi_nvram_paths : + path => true if fileexists(path) + } + + detected_nvram = ( + var.uefi_firmware == "true" || var.uefi_firmware == "" ? ( + length(local.find_first_nvram) > 0 ? + keys(local.find_first_nvram)[0] : + null + ) : ( + var.uefi_nvram_template != "" ? var.uefi_nvram_template : null + ) + ) + + # Validate that both firmware and NVRAM are provided together if one is specified + firmware_and_nvram_valid = ( + (local.detected_firmware != null && local.detected_nvram == null) || + (local.detected_firmware == null && local.detected_nvram == null) || + (local.detected_firmware != null && local.detected_nvram != null) + ) } \ No newline at end of file