将 Packer 1.6 vsphere-iso 配置代码从 JSON 转换为 HCL2

Converting Packer 1.6 vsphere-iso configuration code from JSON to HCL2

随着 release of Packer 1.6 的出现,vsphere-iso 构建器中出现了几个折旧的字段。从外观上看,似乎是 format/type 更改,因为这些字段实际上仍然存在,但看起来就像属性一样。更改示例如下:


在 Packer 1.5.6 中工作:

JSON

"disk_size": 123456,
"disk_thin_provisioned": true
"network": "VM Network",
"network_card": "vmxnet3"

在 Packer 1.6.0 中工作:

JSON

"storage": [
    {
        "disk_size": 123456,
        "disk_thin_provisioned": true
    }
],
"network_adapters": [
    {
        "network": "VM Network",
        "network_card": "vmxnet3"
    }
]

我目前遇到的问题是我正在使用 Packer 1.6.0 并试图将上述工作 JSON 代码转换为 HCL2。我无法弄清楚支持 Packer 1.6.0 中所做更改的 HCL2 语法。

我试过以下方法:

network_adapters = {
    network_card = "vmxnet3"
    network = "VM Network"
}

输出:

An argument named "network_adapter" is not expected here.


network_adapters = (
    network_card = "vmxnet3"
    network = "VM Network"
)

输出:

Error: Unbalanced parentheses

on .\Packer\ConfigFileName.pkr.hcl line 19, in source "vsphere-iso" "Test": 18: storage = ( 19: disk_thin_provisioned = true

Expected a closing parenthesis to terminate the expression.


network_adapters = [
    network_card = "vmxnet3",
    network = "VM Network"
]

输出:

Error: Missing item separator

on .\Packer\ConfigFileName.pkr.hcl line 19, in source "vsphere-iso" "Test": 18: storage = [ 19: disk_thin_provisioned = true,

Expected a comma to mark the beginning of the next item.

我还尝试了其他几种不同集合语法的排列组合,但到目前为止都没有成功。任何建议或提示将不胜感激

正确的语法如下:

network_adapters  {
    network_card = "vmxnet3",
    network = "VM Network"
}

请注意,它没有在 network_adapters{

之间使用赋值运算符 =

感谢 SwampDragons 在 Packer 论坛上指出了这一点。


如果您有兴趣知道原因:2020 年 5 月随着 Packer 1.5.6 的发布,HCL2 中处理映射的方式发生了变化

core/hcl2: Maps are now treated as settable arguments as opposed to blocks. For example tags = {} instead of tags {} [GH-9035]

参考:https://github.com/hashicorp/packer/blob/master/CHANGELOG.md#156-may-1-2020