2019-04-22 15:35:00 -07:00
---
page_title: Use jq and Packer to comment your templates - Guides
2020-04-02 19:39:47 -04:00
sidebar_title: Use jq to strip comments from a Packer template
2019-04-22 15:35:00 -07:00
description: |-
You can add detailed comments beyond the root-level underscore-prefixed field
supported by Packer, and remove them using jq.
---
2020-03-24 18:36:27 -04:00
# How to use jq to strip unsupported comments from a Packer template
2019-04-22 15:35:00 -07:00
2021-02-16 13:57:33 +01:00
-> **Note:** Packer supports HCL2 from version 1.6.0, the Hashicorp Configuration
Language allows to comment directly in template files. Consider upgrading your
2021-02-16 14:31:05 +01:00
JSON template to HCL2 using the `packer hcl2_upgrade` command.
2021-02-16 13:57:33 +01:00
2021-02-16 14:31:05 +01:00
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.
2019-04-22 15:35:00 -07:00
2020-03-24 18:36:27 -04:00
Let's say we have a file named `commented_template.json`
2019-04-22 15:35:00 -07:00
2020-03-12 10:05:08 -04:00
```json
2019-04-22 15:35:00 -07:00
{
2020-03-18 18:46:47 -04:00
"_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"]
}
]
2019-04-22 15:35:00 -07:00
}
```
2020-05-29 17:12:05 -04:00
```shell-session
$ jq 'walk(if type == "object" then del(._comment) else . end)' commented_template.json > uncommented_template.json
2019-04-22 15:35:00 -07:00
```
will produce a new file containing:
```json
{
"builders": [
{
"type": "null",
"communicator": "none"
}
],
"provisioners": [
{
"type": "shell",
2020-03-18 18:46:47 -04:00
"inline": ["echo hellooooo"]
2019-04-22 15:35:00 -07:00
}
]
}
```
Once you've got your uncommented file, you can call `packer build` on it like
you normally would.
## The walk function
2020-03-18 18:46:47 -04:00
2019-04-22 15:35:00 -07:00
If your install of jq does not have the walk function and you get an error like
2020-05-29 17:12:05 -04:00
```text
2019-04-22 15:35:00 -07:00
jq: error: walk/1 is not defined at <top-level>,
```
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.