snippetMinor
Is there an idiomatic way to create reusable Packer templates?
Viewed 0 times
packercreateidiomaticwayreusabletemplatesthere
Problem
I'm creating about 10-12 Packer templates which almost all work the same way. Same builder (Amazon EBS), with some small variations in AMI names, and almost the same provisioner (Ansible Remote) - sometimes with additional variables and sometimes multiple playbooks.
But, much of the Packer templates remain the same.
I know I can use variables to get some of the changeable values into one place in the file. But I'm keen to cut down on all of the copy-pasta in the templates.
I could do this with a wrapper shell script utilising
Is there an idiomatic way of doing this - or at least a generally accepted way?
But, much of the Packer templates remain the same.
I know I can use variables to get some of the changeable values into one place in the file. But I'm keen to cut down on all of the copy-pasta in the templates.
I could do this with a wrapper shell script utilising
jq or something. But each complexity I add gives something else for others to learn. Try as I might, I can't seem to find a 'blessed' idiomatic way of doing this with Packer.Is there an idiomatic way of doing this - or at least a generally accepted way?
Solution
So far, I've taken to defining the default
I've got files called eg.
I've then written a quick Bash script that takes the name of the template one wants to build, finds all the
As a bonus, I'm also using
It works well, but I'm not a massive fan of it because it's custom and it adds an additional barrier for entry for others coming across it (much more to consider than just
So I'm really keen for additional answers here if there's a better way to do this.
builders and provisioners we use, with user variables interpolated in where required, eg:"type": "amazon-ebs",
"instance_type": "t2.micro",
"ami_name": "{{ user `ami-name` }}I've got files called eg.
packer-builders.json and packer-provisioners.json, and then all the separate Packer templates in their own directories.I've then written a quick Bash script that takes the name of the template one wants to build, finds all the
packer-.json files, and uses jq --slurp to merge them in (eg. jq --slurp '.[0] .[1] * .[2]' $LIST_OF_FILES) with the supplied template at the end. It saves the output and sends that along to packer build.As a bonus, I'm also using
jsmin before hitting jq so that I can include comments in any of the JSON files.It works well, but I'm not a massive fan of it because it's custom and it adds an additional barrier for entry for others coming across it (much more to consider than just
packer build something.json!So I'm really keen for additional answers here if there's a better way to do this.
Code Snippets
"type": "amazon-ebs",
"instance_type": "t2.micro",
"ami_name": "{{ user `ami-name` }}Context
StackExchange DevOps Q#4312, answer score: 3
Revisions (0)
No revisions yet.