2019-04-22 18:35:00 -04:00
---
layout: guides
2019-10-01 11:59:48 -04:00
sidebar_current: workflow-tips-and-tricks-use-packer-with-comment
2019-04-22 18:35:00 -04:00
page_title: Use jq and Packer to comment your templates - Guides
2020-03-24 18:36:27 -04:00
sidebar_title: 'Use jq to strip comments from a Packer template'
2019-04-22 18:35:00 -04: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 18:35:00 -04:00
One of the biggest complaints we get about packer is that json doesn't use comments. We're in the process of moving to HCL2, the same config language used by Terraform, which does allow comments. But in the meantime, 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
Let's say we have a file named `commented_template.json`
2019-04-22 18:35:00 -04:00
2020-03-12 10:05:08 -04:00
```json
2019-04-22 18:35:00 -04: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 18:35:00 -04:00
}
```
```sh
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",
2020-03-18 18:46:47 -04:00
"inline": ["echo hellooooo"]
2019-04-22 18:35:00 -04: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 18:35:00 -04:00
If your install of jq does not have the walk function and you get an error like
2020-03-24 18:36:27 -04:00
```shell
2019-04-22 18:35:00 -04: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.