diff --git a/devto/posts/running-packer-hcl2upgrade-and-getting-pkrhcl-file-with-template-hcl2upgrade-unexpected-in-operand.md b/devto/posts/running-packer-hcl2upgrade-and-getting-pkrhcl-file-with-template-hcl2upgrade-unexpected-in-operand.md new file mode 100644 index 0000000..659c051 --- /dev/null +++ b/devto/posts/running-packer-hcl2upgrade-and-getting-pkrhcl-file-with-template-hcl2upgrade-unexpected-in-operand.md @@ -0,0 +1,83 @@ +--- +title: Running packer hcl2_upgrade and getting pkr.hcl file with template hcl2_upgrade unexpected in operand +published: true +description: 'Introduction Since Packer v1.7.0, it is recommended to use HCL2 instead of JSON for Packer...' +tags: + - Packer + - HashiCorp +published_at: '2023-05-31 22:00 +0000' +id: 1661911 +date: '2023-05-31T22:00:00.000Z' +--- + +## Introduction + +Since Packer v1.7.0, it is recommended to use HCL2 instead of JSON for Packer templates. +The `.pkr.hcl` file generated by the `hcl2_upgrade` command, which converts a Packer template written in JSON to HCL2 format, will output `"template: hcl2_upgrade:3: unexpected \"\\\\\" in operand"` in the `.pkr.hcl` file generated by executing the `.pkr.hcl` command. + +## Solution + +https://github.com/hashicorp/packer/issues/10728#issuecomment-793199077 + +In this case, the `\"` (escaped quote) used for escaping was not parsing JSON properly. +You can work around this problem by changing `\"` (escaped quote) to backtick. + +### Before + +Running `packer hcl2_upgrade` on a JSON file defined as follows... + +```json +{ + "builders": [{ + "type": "amazon-ebs", ... + ... + "ami_name": "ami_name-{{isotime \"2006-01-02-15-04-MST\"| clean_resource_name}}" + }], } +} +``` + +The following `.pkr.hcl` file will be generated. + +```hcl +# could not parse template for the following block: "template: hcl2_upgrade:3: unexpected \"\\\\\" in operand" + +source "amazon-ebs" "autogenerated_1" { + ami_name = "ami_name-{{isotime \"2006-01-02-15-04-MST\"| clean_resource_name}}" +``` + +### After + +After changing the description and running `packer hcl2_upgrade`... + +```json +{ + "builders": [{ + "type": "amazon-ebs",. + ... + "ami_name": "ami_name-{{isotime `2006-01-02-15-04-MST`| clean_resource_name}}" + }], ... +} +``` + +`"template: hcl2_upgrade:3: unexpected \"\\\\\" in operand"` is not output in the `.pkr.hcl` file, +but a `.pkr.hcl` file using the intended format (`legacy_isotime`). + +```hcl +source "amazon-ebs" "autogenerated_1" { + ami_name = "ami_name-{{ clean_resource_name `${legacy_isotime("2006-01-02-15-04-MST")}` }}" + +``` + +## References + +https://github.com/hashicorp/packer/issues/10728 + +https://developer.hashicorp.com/packer/docs/v1.7.x/commands/hcl2_upgrade + +https://developer.hashicorp.com/packer/tutorials/configuration-language/hcl2-upgrade + +https://developer.hashicorp.com/packer/docs/templates/hcl_templates/functions/datetime/legacy_isotime + +https://developer.hashicorp.com/packer/docs/templates/legacy_json_templates/engine + +https://www.packer.io/