Comments Inside a Packer Template

A common problem I have with packer is remembering all the little hacks I put in the .json template to make a specific provider work.

Since the json parser of packer is somewhat restrictive, I decided to wrap my templates inside a python skeleton script. It also have the benefit of laying some ground work into automating template creation in a near future.

So far, the only caveat other than not being able to comment directly a packer template was to rename all json boolean (true/false) to python boolean (True/False).


import json
from collections import OrderedDict
packer_template = OrderedDict(
"variables": {
"iso_url": "",
(. . .)
"post-processors": [ [
"type": "vagrant",
"keep_input_artifact": **True**,
"output": "{{user `vm_hostname`}}-{{ .Provider }}-{{isotime \"2006-01-02\"}}.box"
#See issue
#Note: vm_hostname is used for output-dir
"type": "shell-local",
"inline": "sed -i.backup 's/ide0:0.filename.*$/ide0:0.autodetect = \"TRUE\"/' {{user `vm_hostname`}}/*.vmx"

with open("packer_template.json", 'w') as f:
f.writelines(json.dumps(packer_template, indent=4))

Building is just one more step than using the regular JSON:
python ; packer build packer_template.json

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s