Possible to include templates for different builders, same provisioners?

19 views
Skip to first unread message

Alex Gottschalk

unread,
Nov 19, 2018, 7:33:08 PM11/19/18
to Packer
I have a nice amazon-ebs build template that I am developing to create a standardized AMI.  For compliance reasons, the AMI needs to be encrypted.  For the CI testing however, we do not want to do the cross-region copy of the output AMI.  So it would be very helpful to be able to include the same common provisioner template from a couple of different builder templates.  Specifically, one builder template for production that does the full-blown encryption and cross-region distribution of the AMI, and one builder template for CI testing that does not bother with said encryption and copying.  Of course I can just create separate templates for both environments, but the provisioner section is fairly complex, and avoiding breaking DRY principle is pretty important here.

The blocking issue here is this section of the template (used for prod):
        "ami_regions": "{{user `ami_regions`}}",
       
"region_kms_key_ids": {
           
"us-east-1": "{{user `ami_kms_encryption_key_use1`}}",
           
"us-east-2": "{{user `ami_kms_encryption_key_use2`}}",
           
"us-west-1": "{{user `ami_kms_encryption_key_usw1`}}",
           
"us-west-2": "{{user `ami_kms_encryption_key_usw2`}}"
       
},

When "ami_regions" is an empty string (for testing) instead of a comma-separated list (for prod), packer fails with this error:
4 error(s) occurred:

* Region us-east-2 is in region_kms_key_ids but not in ami_regions
* Region us-west-1 is in region_kms_key_ids but not in ami_regions
* Region us-west-2 is in region_kms_key_ids but not in ami_regions
* Region us-east-1 is in region_kms_key_ids but not in ami_regions

This would be trivial to work around if it were possible to use objects for variable values, but oh well.

Does anyone have any suggestions here, besides keeping multiple copies of the provisioner code in sync between separate template files?

Thanks!
--Alex

Alex Gottschalk

unread,
Nov 19, 2018, 8:39:33 PM11/19/18
to Packer
I've hacked together a workaround that will do what I want here:

#!/usr/bin/env python3

import json
from os import environ


builder
= json.load(open('builder-' + sys.environ['BUILDENV']))
provisioner
= json.load(open('provisioners.json'))
json
.dump({**builder, **provisioner}, open('out.json', 'w'))


Rickard von Essen

unread,
Nov 20, 2018, 1:36:31 AM11/20/18
to packe...@googlegroups.com
Or just use jq to delete region_kms_key_ids and ami_regions from the template in CI. 

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/331a2653-3c7d-4f61-91e4-80d206c0d72c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages