admin管理员组

文章数量:1130349

Environment:

  • packer version = v1.11.2
  • target platform = vCenter (vSphere) v7.x
  • plugins = vsphere(v1.4.2)

I am trying to make the variables.pkr.hcl more readable besides having 25-30 variables defined in a flat notation. So the idea is to create grouped nested object variables. The issue I am facing is when I try to provide a "partial" override value within vars.pkrvars.hcl; Packer is not logically merging the nested default values with the override value (see error below).

TL;DR I want the final interpolation of the nested variable to use any missing values (not overridden within vars file) with the default value defined within the variables file.

variables.pkr.hcl (snippet)

# abspath: Absolute path
variable "cloud_init_details" {
    type = object({
        user_data = object({
            cloud_init_template = string
            cloud_init_file_abspath = string
        })
        meta_data = object({
            meta_data_file_abspath = string
        })
    })
    default = {
        user_data = {
            cloud_init_template = "./deployments/ubuntu_2404_template_20241222/user-data.pkrtpl"
            cloud_init_file_abspath = "^$HOME^/.cache/packer_cache/downloads/user-data"
        }
        meta_data = {
            meta_data_file_abspath = "^$HOME^/.cache/packer_cache/downloads/meta-data"
        }
    }
    description = <<-EOT
        cloud_init_template: Cloud-init user-data cloud-config template
        cloud_init_file_fqpn: VM user-data cloud-init file location on the local Packer server
        meta_data_file_fqpn: Cloud-init meta-data cloud-config template
    EOT
}

build.pkr.hcl (snippet)

#########################
# SOURCES
#########################

# Create user-data file on Packer HTTP server
source "file" "local_user_data_file_create" {
  content = templatefile(var.cloud_init_details.user_data.cloud_init_template,
                        {
                          "nic1_ip_gateway"           = var.vm_detailswork.ip_gateway
                          "nic1_ip_address_cidr"      = var.vm_detailswork.ip_address_cidr
                          "nic1_nameservers"          = var.vm_detailswork.name_servers
                          "nic1_searchdomains"        = var.vm_detailswork.extra_search_domains
                          "username"                  = local.os_username
                          "os_user_encrypted_password"= local.os_user_encrypted_password
                          "hostname"                  = local.hostname
                        }
                       )
  target = var.cloud_init_details.user_data.cloud_init_file_abspath
}

# Create meta-data file on Packer HTTP server
source "file" "local_meta_data_file_create" {
  content = ""
  target  = var.cloud_init_details.meta_data.meta_data_file_abspath
}

vars.pkrvars.hcl (snippet)

cloud_init_details = {
    user_data = {
        cloud_init_template = "./deployments/ubuntu_2404_template_20241222/user-data.pkrtpl"
    }
}

ERROR:

Error: Invalid value for variable

  on ./deployments/ubuntu_2404_template_20241222/vars.pkrvars.hcl line 8:
   8: cloud_init_details = {
   9:     user_data = {
  10:         cloud_init_template = "./deployments/ubuntu_2404_template_20241222/user-data.pkrtpl"
  11:     }
  12: }

The value for cloud_init_details is not compatible with the variable's type
constraint: attribute "meta_data" is required.

Environment:

  • packer version = v1.11.2
  • target platform = vCenter (vSphere) v7.x
  • plugins = vsphere(v1.4.2)

I am trying to make the variables.pkr.hcl more readable besides having 25-30 variables defined in a flat notation. So the idea is to create grouped nested object variables. The issue I am facing is when I try to provide a "partial" override value within vars.pkrvars.hcl; Packer is not logically merging the nested default values with the override value (see error below).

TL;DR I want the final interpolation of the nested variable to use any missing values (not overridden within vars file) with the default value defined within the variables file.

variables.pkr.hcl (snippet)

# abspath: Absolute path
variable "cloud_init_details" {
    type = object({
        user_data = object({
            cloud_init_template = string
            cloud_init_file_abspath = string
        })
        meta_data = object({
            meta_data_file_abspath = string
        })
    })
    default = {
        user_data = {
            cloud_init_template = "./deployments/ubuntu_2404_template_20241222/user-data.pkrtpl"
            cloud_init_file_abspath = "^$HOME^/.cache/packer_cache/downloads/user-data"
        }
        meta_data = {
            meta_data_file_abspath = "^$HOME^/.cache/packer_cache/downloads/meta-data"
        }
    }
    description = <<-EOT
        cloud_init_template: Cloud-init user-data cloud-config template
        cloud_init_file_fqpn: VM user-data cloud-init file location on the local Packer server
        meta_data_file_fqpn: Cloud-init meta-data cloud-config template
    EOT
}

build.pkr.hcl (snippet)

#########################
# SOURCES
#########################

# Create user-data file on Packer HTTP server
source "file" "local_user_data_file_create" {
  content = templatefile(var.cloud_init_details.user_data.cloud_init_template,
                        {
                          "nic1_ip_gateway"           = var.vm_detailswork.ip_gateway
                          "nic1_ip_address_cidr"      = var.vm_detailswork.ip_address_cidr
                          "nic1_nameservers"          = var.vm_detailswork.name_servers
                          "nic1_searchdomains"        = var.vm_detailswork.extra_search_domains
                          "username"                  = local.os_username
                          "os_user_encrypted_password"= local.os_user_encrypted_password
                          "hostname"                  = local.hostname
                        }
                       )
  target = var.cloud_init_details.user_data.cloud_init_file_abspath
}

# Create meta-data file on Packer HTTP server
source "file" "local_meta_data_file_create" {
  content = ""
  target  = var.cloud_init_details.meta_data.meta_data_file_abspath
}

vars.pkrvars.hcl (snippet)

cloud_init_details = {
    user_data = {
        cloud_init_template = "./deployments/ubuntu_2404_template_20241222/user-data.pkrtpl"
    }
}

ERROR:

Error: Invalid value for variable

  on ./deployments/ubuntu_2404_template_20241222/vars.pkrvars.hcl line 8:
   8: cloud_init_details = {
   9:     user_data = {
  10:         cloud_init_template = "./deployments/ubuntu_2404_template_20241222/user-data.pkrtpl"
  11:     }
  12: }

The value for cloud_init_details is not compatible with the variable's type
constraint: attribute "meta_data" is required.

本文标签: hclHCP PackerNested variablesDefaultsand overrides VARSStack Overflow