Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: required variables settable through environment #6988

Closed
hubertgrzeskowiak opened this issue Nov 12, 2018 · 7 comments · Fixed by #8588
Closed

Feature Request: required variables settable through environment #6988

hubertgrzeskowiak opened this issue Nov 12, 2018 · 7 comments · Fixed by #8588
Labels
config-2.0 core Core components of Packer enhancement

Comments

@hubertgrzeskowiak
Copy link

hubertgrzeskowiak commented Nov 12, 2018

Right now we can either specify a user variable as null which means it's required and it can only be set using the CLI option, or we can use {{env `foo`}} to make it settable through the environment. I am missing an option to set a variable as required, with the option to set it through the environment.

Sidenote: Terraform offers this functionality. There all variables are required by default and can be set using the TV_VAR_ prefix for environment variables: https://www.terraform.io/docs/configuration/variables.html#environment-variables

@SwampDragons SwampDragons added enhancement core Core components of Packer labels Nov 12, 2018
@SwampDragons
Copy link
Contributor

So to clarify, you want to be able to have Packer check for environment variables if the variable isn't already set inside your packer config?

@hubertgrzeskowiak
Copy link
Author

@SwampDragons so my issue is the following. Let's say you have one Packer variable called access_key which is required.

You define it in Packer like this:

{
  "variables": {
    "access_key": null,
    ...

Now that variable is required and the only way to provide it is through command line, e.g.:

packer build -var "access_key=${ACCESS_KEY}" packer-conf.json

This call has some big downsides: it requires you to know all variable names defined in the packer file, it requires you to assemble a specific command line call rather than using a generic one - as it's common practice in more automated systems. Also the long command line becomes unreadable quickly .

What I can do in Packer instead is the following:

{
  "variables": {
    "access_key": "{{env `ACCESS_KEY`}}",
    ...

Now I can define my variables independently of the command line call, and I can access commonly known environment variables (e.g. AWS_ACCESS_KEY_ID or the like). The call is just simple:

packer build packer-conf.json 

While this solves a problem, it introduces a new one: what if my environment variable ACCESS_KEY is not set at all? Packer will just use an empty string. Sure, I can check for it in a provisioning step, but those are run late in the process, and I need to explicitly test for them. If I were to use any of my variables in the builders, I cannot check for them being set.

I wish I could tell Packer: "This variable can be set through command line, or environment, but it has to be set one way or another!"

Just to recap, I am not suggesting changing Packer's way of handling user variables in a backward incompatible way. I am suggesting adding some syntax to mark user variables as required, while preserving the possibility to set them through the environment.

@SwampDragons
Copy link
Contributor

Okay, I can see the benefit of that. It's a good idea. Off the top of my head, I'm not totally sure how easy it will be to implement because of the way Packer's architecture handles variables, so I'll have to take a look at that before deciding if it's something we can prioritize soon.

Meanwhile, have you considered using a var-file? This solves the issue of long and non-generic command lines, and gives you a fair amount of flexibility.

@akumria
Copy link

akumria commented Jan 28, 2019

I think the simplest way would be to assume that if an environment variable is empty, then default the value to be null this triggering the requirement that the value is actually set.

@SwampDragons
Copy link
Contributor

I think we may get this behavior "for free" as part of the HCL2 config effort that we're hoping to land this year.

@azr
Copy link
Contributor

azr commented Jan 17, 2020

Hello there this is implemented in #8588; only for hcl2 but basically after defining a variable; one will be able to set it by setting an env var like so: PKR_VAR_variablename.

@ghost
Copy link

ghost commented Apr 1, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
config-2.0 core Core components of Packer enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants