Search code examples
jsonpackerhcl

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


With the release of Packer 1.6 came several depreciated fields in the vsphere-iso builder. From the looks of it, seems to be a format/type change because the fields actually still exists but just as properties it seems. An example of the changes are the following:


Working in Packer 1.5.6:

JSON

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

Working in Packer 1.6.0:

JSON

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

The issue I have at the moment is I'm using Packer 1.6.0 and am trying to convert the above working JSON code to HCL2. I can't figure out the HCL2 syntax that supports the changes that were made in Packer 1.6.0.

I've tried the following:

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

Output:

An argument named "network_adapter" is not expected here.


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

Output:

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"
]

Output:

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.

I've also tried several other permutations of different collection syntax together with no luck so far. Any suggestions or tips would greatly be appreciated


Solution

  • The correct syntax is the following:

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

    Note that it's not using an assignment operator = between network_adapters and {

    Credit goes to SwampDragons over on the Packer forums for pointing this out.


    If you're interested in knowing why: There was a change to how maps are treated in HCL2 back in May 2020 with the release of Packer 1.5.6

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

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