1

There's a packer script called ubuntu2204.pkr.hcl in this repo

It accepts the following parameter:

variable "azure_tags" {
  type    = map(string)
  default = {}
}

I've been trying to call this from a PowerShell script as below:

$azureTags = @{
    BusinessUnit = "IDS"
    Environment  = "Production"
}

packer build    -var "capture_name_prefix=$ResourcesNamePrefix" `
                -var "client_id=$ClientId" `
                -var "client_secret=$ClientSecret" `
                -var "install_password=$InstallPassword" `
                -var "location=$Location" `
                -var "resource_group=$ResourceGroup" `
                -var "storage_account=$StorageAccount" `
                -var "subscription_id=$SubscriptionId" `
                -var "temp_resource_group_name=$TempResourceGroupName" `
                -var "tenant_id=$TenantId" `
                -var "virtual_network_name=$VirtualNetworkName" `
                -var "virtual_network_resource_group_name=$VirtualNetworkRG" `
                -var "virtual_network_subnet_name=$VirtualNetworkSubnet" `
                -var "run_validation_diskspace=$env:RUN_VALIDATION_FLAG" `
                -var "azure_tags=$($azureTags | ConvertTo-Json -Compress)" `
                -color=false `
                $TemplatePath

This is giving the following error:

Build ubuntu2204 VM Error: Variables not allowed on value for var.azure_tags from arguments line 1:

I'm new to packer, anyone see what I've got wrong?

Rob Bowman
  • 7,632
  • 22
  • 93
  • 200
  • See https://developer.hashicorp.com/packer/tutorials/integrations/aws-windows-image – jdweng Aug 05 '23 at 08:52
  • What version of packer are you using? – Paolo Aug 05 '23 at 09:02
  • packer version is 1.9.2 – Rob Bowman Aug 05 '23 at 09:32
  • The sad reality up to PowerShell 7.2.x is that an _extra, manual_ layer of ``\``-escaping of embedded `"` characters is required in arguments passed to _external programs_. This has been fixed in PowerShell 7.3.0, with selective exceptions on Windows. – mklement0 Aug 05 '23 at 12:02

1 Answers1

1

I think that has something to do with the quotes of the serialized json. This worked for me with packer 1.9.2.

Given this file file.pkr.hcl:

variable "azure_tags" {
  type    = map(string)
  default = {}
}
$azureTags = @{
    BusinessUnit = "IDS"
    Environment  = "Production"
}
$tags=$($azureTags | ConvertTo-Json -Compress)
packer console -var "azure_tags=$($tags -replace '([\\]*)"', '$1$1\"')" .\file.pkr.hcl
> 
> var.azure_tags
{
    "BusinessUnit" = "IDS"
    "Environment" = "Production"
}
Paolo
  • 21,270
  • 6
  • 38
  • 69