传递字符串数组从PowerShell到Packer

huangapple go评论145阅读模式
英文:

Pass array of strings from PowerShell to Packer

问题

以下是您要翻译的内容:

此存储库中有一个名为ubuntu2204.pkr.hcl的打包脚本。

它接受以下参数:

  1. variable "azure_tags" {
  2. type = map(string)
  3. default = {}
  4. }

我一直在尝试从PowerShell脚本中调用它,如下所示:

  1. $azureTags = @{
  2. BusinessUnit = "IDS"
  3. Environment = "Production"
  4. }
  5. packer build -var "capture_name_prefix=$ResourcesNamePrefix" `
  6. -var "client_id=$ClientId" `
  7. -var "client_secret=$ClientSecret" `
  8. -var "install_password=$InstallPassword" `
  9. -var "location=$Location" `
  10. -var "resource_group=$ResourceGroup" `
  11. -var "storage_account=$StorageAccount" `
  12. -var "subscription_id=$SubscriptionId" `
  13. -var "temp_resource_group_name=$TempResourceGroupName" `
  14. -var "tenant_id=$TenantId" `
  15. -var "virtual_network_name=$VirtualNetworkName" `
  16. -var "virtual_network_resource_group_name=$VirtualNetworkRG" `
  17. -var "virtual_network_subnet_name=$VirtualNetworkSubnet" `
  18. -var "run_validation_diskspace=$env:RUN_VALIDATION_FLAG" `
  19. -var "azure_tags=$($azureTags | ConvertTo-Json -Compress)" `
  20. -color=false `
  21. $TemplatePath

这会产生以下错误:

> 构建ubuntu2204 VM
> 错误:不允许使用变量
> 在参数行1的var.azure_tags的值上:

我是packer的新手,有人看到我做错了什么吗?

英文:

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

It accepts the following parameter:

  1. variable "azure_tags" {
  2. type = map(string)
  3. default = {}
  4. }

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

  1. $azureTags = @{
  2. BusinessUnit = "IDS"
  3. Environment = "Production"
  4. }
  5. packer build -var "capture_name_prefix=$ResourcesNamePrefix" `
  6. -var "client_id=$ClientId" `
  7. -var "client_secret=$ClientSecret" `
  8. -var "install_password=$InstallPassword" `
  9. -var "location=$Location" `
  10. -var "resource_group=$ResourceGroup" `
  11. -var "storage_account=$StorageAccount" `
  12. -var "subscription_id=$SubscriptionId" `
  13. -var "temp_resource_group_name=$TempResourceGroupName" `
  14. -var "tenant_id=$TenantId" `
  15. -var "virtual_network_name=$VirtualNetworkName" `
  16. -var "virtual_network_resource_group_name=$VirtualNetworkRG" `
  17. -var "virtual_network_subnet_name=$VirtualNetworkSubnet" `
  18. -var "run_validation_diskspace=$env:RUN_VALIDATION_FLAG" `
  19. -var "azure_tags=$($azureTags | ConvertTo-Json -Compress)" `
  20. -color=false `
  21. $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?

答案1

得分: 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:

  1. variable "azure_tags" {
  2. type = map(string)
  3. default = {}
  4. }
  1. $azureTags = @{
  2. BusinessUnit = "IDS"
  3. Environment = "Production"
  4. }
  5. $tags=$($azureTags | ConvertTo-Json -Compress)
  6. packer console -var "azure_tags=$($tags -replace '\\\"','\\\\"')" .\file.pkr.hcl
  7. >
  8. > var.azure_tags
  9. {
  10. "BusinessUnit" = "IDS"
  11. "Environment" = "Production"
  12. }
英文:

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:

  1. variable "azure_tags" {
  2. type = map(string)
  3. default = {}
  4. }
  1. $azureTags = @{
  2. BusinessUnit = "IDS"
  3. Environment = "Production"
  4. }
  5. $tags=$($azureTags | ConvertTo-Json -Compress)
  6. packer console -var "azure_tags=$($tags -replace '([\\]*)"', '$1$1\"')" .\file.pkr.hcl
  7. >
  8. > var.azure_tags
  9. {
  10. "BusinessUnit" = "IDS"
  11. "Environment" = "Production"
  12. }

huangapple
  • 本文由 发表于 2023年8月5日 15:39:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定