With hcl2 templates use the build_name as part of the output_directory

I’m working on my first hcl2 template for packer and had a question about using the build name as part of the output_directory. This can be useful when you have two builds that use the same source and don’t want them to clobber each other.

The template is setup with two builds that are named build base & full.


Within the source I’m specifying the output_directory using the templating syntax. However the build_name is not what I expected. It appears to be the name specified in the source resource.

Am I missing something obvious here? Maybe this is a bug with the build_name not sourcing the correct value? My expectation is that the build_name would be the same value used in the -only command line flag which would be base.vmware-iso.macOS_11 or full.vmware-iso.macOS_11.

Please Advise,

I just went to look at this and I don’t think we have a way to do that. I think the issue here is that HCL differentiates between the source name and the build name, whereas in legacy Packer those were the same thing. So while we have ways to surface the source name – an example is"echo '${source.name}'", we don’t currently have that for the build name. I’m going to open an issue on our HCL implementation ticket.

1 Like

Just a note here: this has been implemented in https://github.com/hashicorp/packer/pull/10114 and will be out in the v1.6.5 release!

1 Like

Excellent!! Thank you for making this improvement so quickly! I’m porting a few templates to hcl2 that use this functionality and it will simplify things.

Awesome! Please feel free to open issues on the github repo if you find anything else that feels out of whack. We’re really hoping to get HCL out of “beta” by the new year.

Having https://github.com/hashicorp/packer/issues/9284 would help manage compatibility with hcl2 changes. Hopefully that can make 1.6.5 as well.

If anything else shows up I’ll get an issue opened.

Thanks again,