Comments Inside a Packer Template

A common problem I have with packer packer.io 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).


Archlinux_Base.json

import json
from collections import OrderedDict
packer_template = OrderedDict(
{
"variables": {
"iso_url": "http://mirror.rackspace.com/archlinux/iso/2018.03.01/archlinux-2018.03.01-x86_64.iso",
(. . .)
"post-processors": [ [
{
"type": "vagrant",
"keep_input_artifact": **True**,
"output": "{{user `vm_hostname`}}-{{ .Provider }}-{{isotime \"2006-01-02\"}}.box"
},
{
#See issue https://github.com/hashicorp/packer/issues/4885
#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 arch_headless.py ; packer build packer_template.json

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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