You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Packer HCL模板执行报错:An argument or block definition is required here 求助

问题排查与修复

1. 语法错误:多余闭合括号与块结构错误

你的模板开头存在两处语法问题:

  • required_plugins块末尾多了一个额外的}
  • required_plugins需要包裹在顶层packer块内才符合HCL2规范

修正后的插件配置块:

packer {
  required_plugins {
    azure = {
      version = ">= 1.0.0"
      source  = "github.com/hashicorp/azure"
    }
  }
}

2. 镜像SKU与系统类型不匹配

你指定的image_sku = "20_04-lts-gen2"是Ubuntu 20.04的SKU,但os_type = "Windows",两者完全冲突。需要替换为Visual Studio 2022对应的Windows SKU,例如:

image_offer     = "visualstudio2022"
image_publisher = "microsoftvisualstudio"
image_sku       = "vs-2022-ent-latest-win11" # 可根据需求选择对应Windows版本的VS2022 SKU
os_type         = "Windows"

可通过Azure CLI命令查询该区域可用的SKU:

az vm image list-skus --publisher microsoftvisualstudio --offer visualstudio2022 --location eastus --output table

3. PowerShell路径转义问题

原模板中Sysprep路径的单反斜杠会被HCL解析为转义字符,需要改为双反斜杠:

& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit

修复后的完整模板

packer {
  required_plugins {
    azure = {
      version = ">= 1.0.0"
      source  = "github.com/hashicorp/azure"
    }
  }
}

source "azure-arm" "example" {
  managed_image_name                 = "MT-Image"
  managed_image_resource_group_name  = "cg"
  location                           = "eastus"
  # Using Azure CLI for authentication
  use_azure_cli_auth = true
  image_offer     = "visualstudio2022"
  image_publisher = "microsoftvisualstudio"
  image_sku       = "vs-2022-ent-latest-win11"
  os_type         = "Windows"
  vm_size         = "Standard_E2b_v5"
  os_disk_size_gb = 64
  shared_image_gallery_destination {
    resource_group       = "cg"
    gallery_name         = "gallery1"
    image_name           = "newimage"
    image_version        = "1.0.1"
    replication_regions  = ["eastus"]
    storage_account_type = "Standard_LRS"
  }
}

build {
  sources = [
    "source.azure-arm.example"
  ]
  provisioner "powershell" {
    inline = [
      "Remove-WindowsFeature Web-Server",
      "while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
      "while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
      "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
      "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10  } else { break } }"
    ]
  }
}

内容的提问来源于stack exchange,提问作者kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 08:32:46