HCL and Multiple Sources and Environments

Okay, this might be a long one.

Using a GitLab/GitHub CI/CD pipeline, I’d like to run multiple source/builder configs that specify the same variables for different environments on different providers.

Providers/Builders are AWS, Azure and vSphere

Environments would be something like production and development

Sources would be single-session, multisession, server_2016 and server 2019

I’d like to have a variables file that defines the variables in a main environment variables .pkrvars.hcl or .pkr.hcl file. One of the issues I ran into is that packer doesn’t seem to allow the combination of the both the .pkrvars and the var.pkr.hcl files together with a main builder.pkr.hcl file. I’d like to not have to declare/define the variables in the main builder HCL config file, if at all possible. Maybe this isn’t possible, or maybe it is.


  • I believe this file can be used to define a variable, but not declare variables or use expressions


  • It looks like this file can both declare an define variables, but not just declare variables



  • This would be the main builder file that’s called depending on the specific pipeline being executed

A couple options I’ve been playing around with, at least in my head and testing:

  1. Create a <development,production>_variables.pkr.hcl file that both defines and delares the variables and uses the <provider/builder>.pkr.hcl file to define the sources/builders/provisioners, etc… having each set of config files in their own directories and called from the cicd.yml file.

  2. Create a <provider/builder>.pkr.hcl that contains the declarations and defines the defaults, as well as the builders and etc… programatically create variables file from the cicd.yml file that contains the correct variables based on the environment, and place that in the same folder as the <provider/builder>.pkr.hcl file.

  3. Create a custom .json file that contains all of the variables I’d like defaulted for each of the environment, and just parse through that to build a .pkr.hcl file that can be used as a variables file based on the environment being called. If it’s not the “main” branch, then it would be a development build, vs if it was a main branch and triggered, it would be production build.

I’m assuming that I could then use the -only parameter to specify which sources I’d like to run, or does that just control which builders?

Or am I overthinking this, and someone has a better idea. The main goal here is to not have to modify several different .yml files every time something in the environment changes. I’d like to have one file that defines what all the variables should be per environment.

As a side note. Our current process uses different imported .yml files for our ci/cd pipleine, and all of the variables are declared in the pipeline and passed to packer as a .json file. Every variable is define in predefined template blocks in the .yml for the ci/cd pipeline.

Sorry about the length, this has been bugging me for a few days now.

I’m trying to get the most out of this that I can, since I’m switching over my json files to the .hcl files.

I guess I should have asked. Is it possible to use a .pkrvar.hcl for declare a variable, us a .pkr.hcl to define a variable, and use a pkr.hcl to contain just the source, build, processor, and post processors?

I think I’m just going to go with managing the variables with a json file. I’ll then parse that json, and make any changes that are necessary into another .hcl file that’s called at the time of the “packer.exe” build process.