diff --git a/website/content/docs/templates/legacy_json_templates/index.mdx b/website/content/docs/templates/legacy_json_templates/index.mdx index 642ad3ec5..130562370 100644 --- a/website/content/docs/templates/legacy_json_templates/index.mdx +++ b/website/content/docs/templates/legacy_json_templates/index.mdx @@ -79,6 +79,74 @@ key with an underscore. Example: **Important:** Only _root level_ keys can be underscore prefixed. Keys within builders, provisioners, etc. will still result in validation errors. +-> **Note:** Packer supports HCL2 from version 1.6.0. The Hashicorp +Configuration Language does support comments anywhere in template files. +If comments are important to you, consider upgrading your +JSON template to HCL2 using the `packer hcl2_upgrade` command. + +One workaround if you are not ready to upgrade to HCL is to use jq to strip +unsupported comments from a Packer template before you run `packer build`. + +For example, here is a file named `commented_template.json`: + +```json +{ + "_comment": ["this is", "a multi-line", "comment"], + "builders": [ + { + "_comment": "this is a comment inside a builder", + "type": "null", + "communicator": "none" + } + ], + "_comment": "this is a root level comment", + "provisioners": [ + { + "_comment": "this is a different comment", + "type": "shell", + "_comment": "this is yet another comment", + "inline": ["echo hellooooo"] + } + ] +} +``` + +If you use the following jq command: + +```shell-session +$ jq 'walk(if type == "object" then del(._comment) else . end)' commented_template.json > uncommented_template.json +``` + +The tool will produce a new file containing: + +```json +{ + "builders": [ + { + "type": "null", + "communicator": "none" + } + ], + "provisioners": [ + { + "type": "shell", + "inline": ["echo hellooooo"] + } + ] +} +``` + +Once you've got your uncommented file, you can call `packer build` on it like +you normally would. + +If your install of jq does not have the walk function and you get an error like + +```text +jq: error: walk/1 is not defined at , +``` + +You can create a file `~/.jq` and add the [walk function](https://github.com/stedolan/jq/blob/ad9fc9f559e78a764aac20f669f23cdd020cd943/src/builtin.jq#L255-L262) to it by hand. + ## Example Template Below is an example of a basic template that could be invoked with diff --git a/website/content/guides/workflow-tips-and-tricks/use-packer-with-comment.mdx b/website/content/guides/workflow-tips-and-tricks/use-packer-with-comment.mdx deleted file mode 100644 index 8ccaa6661..000000000 --- a/website/content/guides/workflow-tips-and-tricks/use-packer-with-comment.mdx +++ /dev/null @@ -1,75 +0,0 @@ ---- -page_title: Use jq and Packer to comment your templates - Guides -description: |- - You can add detailed comments beyond the root-level underscore-prefixed field - supported by Packer, and remove them using jq. ---- - -# How to use jq to strip unsupported comments from a Packer template - --> **Note:** Packer supports HCL2 from version 1.6.0, the Hashicorp Configuration -Language allows to comment directly in template files. Consider upgrading your -JSON template to HCL2 using the `packer hcl2_upgrade` command. - -One of the biggest complaints we get about Packer is that JSON doesn't use comments. -For Packer JSON templates, you can add detailed comments beyond the root-level underscore-prefixed field supported by Packer, and remove them using jq. - -Let's say we have a file named `commented_template.json` - -```json -{ - "_comment": ["this is", "a multi-line", "comment"], - "builders": [ - { - "_comment": "this is a comment inside a builder", - "type": "null", - "communicator": "none" - } - ], - "_comment": "this is a root level comment", - "provisioners": [ - { - "_comment": "this is a different comment", - "type": "shell", - "_comment": "this is yet another comment", - "inline": ["echo hellooooo"] - } - ] -} -``` - -```shell-session -$ jq 'walk(if type == "object" then del(._comment) else . end)' commented_template.json > uncommented_template.json -``` - -will produce a new file containing: - -```json -{ - "builders": [ - { - "type": "null", - "communicator": "none" - } - ], - "provisioners": [ - { - "type": "shell", - "inline": ["echo hellooooo"] - } - ] -} -``` - -Once you've got your uncommented file, you can call `packer build` on it like -you normally would. - -## The walk function - -If your install of jq does not have the walk function and you get an error like - -```text -jq: error: walk/1 is not defined at , -``` - -You can create a file `~/.jq` and add the [walk function](https://github.com/stedolan/jq/blob/ad9fc9f559e78a764aac20f669f23cdd020cd943/src/builtin.jq#L255-L262) to it by hand. diff --git a/website/data/guides-nav-data.json b/website/data/guides-nav-data.json index 0e3a2dd3b..b0631160b 100644 --- a/website/data/guides-nav-data.json +++ b/website/data/guides-nav-data.json @@ -45,19 +45,6 @@ } ] }, - { - "title": "Workflow Tips and Tricks", - "routes": [ - { - "title": "Overview", - "path": "workflow-tips-and-tricks" - }, - { - "title": "Use jq to strip comments from a Packer template", - "path": "workflow-tips-and-tricks/use-packer-with-comment" - } - ] - }, { "title": "Build Immutable Infrastructure with Packer in CI/CD", "routes": [