This is a feature request (unless it exists and I just can't figure out how it works). I would like to be able to use the {{ env }}
function with a default value defined in the packer template, such that if the environment variable is empty/unset then the default value is assigned to the variable. And if the variable is assigned a value using -var
or -var-file
, that would take precedence over the default or the environment variable.
I described this use case in the Google group, perhaps more clearly.
Thanks for the suggestion. This makes sense to me!
Not sure if not too much of a long-shot, but it would be nice to have the variables
section in the template to resolve any user
there too, so that we could re-use variables nicely. Might not be possible, though.
@kwilczynski I'm not sure I follow that one; can you give an example?
@cbednarski sure thing! :) Check the following:
https://gist.github.com/kwilczynski/477ac2dca1884522dffbc97a76aa4325
Especially, line no. 29 in the output. It would be handy to be able to re-use variables.
@kwilczynski Ah I see, thanks for clarifying. Doing that requires some recursive interpolation; this has been requested previously but is not planned. To clarify this is different from the {{ env }}
request.
@cbednarski ahh! I see! Thank you for getting back to me :) Re: {{ env }}
- yes, definitely. Sorry for stealing the spotlight here a little.
any update? This would allow some considerable cleanup in interaction with packer (currently defaults live either where they're used with some conditional shell stuff, or in a pre-packer shell script)
Just pinging on this one as I'd really like to see this feature as well.
+1
I've thought about implementing this (see PR), but I think we want to wait until we support HCL, which has default values natively.
Any chance that HCL fantasy is moving toward reality?
it's 100% a priority for us, but I don't have a date on it to share
Any updates on this? I ran in to the same problem today.
I don't need any fancy recursive evaluation, just a default like in shell scripting, eg
``
{
"variables": {
"alpine_version": "{{env
ALPINE_VERSION`:-3.9}}"
}
}
We are currently aiming to have the HCL2 integration in 6 months or so, and I believe this should be addressed by that change.
Ok, thanks.
Any update?
HCL2 is currently working in a branch that's about to be merged -- see PR #8423. We believe we'll be on target for releasing initial HCL2 support in this month's 1.5.0 release. At that point, we can revisit this issue and determine whether more work needs to be done to support it.
HCL2 is currently working in a branch that's about to be merged -- see PR #8423. We believe we'll be on target for releasing initial HCL2 support in this month's 1.5.0 release. At that point, we can revisit this issue and determine whether more work needs to be done to support it.
Thanks in advance
No update yet; while basic build features were added in #8423 we still have some work to do in order to add full variable support to the HCL2 files. @azr this issue is one to consider when adding variables to hcl2, since I know that's something you're investigating right now.
Hello here, I think this can be closed.
Variables behave similarly to what Terraform does: https://packer.io/docs/configuration/from-1.5/variables.html one can use env variables to set a packer variable.
The TLDR of it is:
1/ Declare a var:
# this can be declared in any *.pkr.hcl file depending on your preference.
# For example in vars.pkr.hcl if you want them all there
# or at the top of the file where it is being used
variable "foo" {
type = string
default = ""
}
2/ Set it either from:
PKR_VAR_foo=bar packer ...
*.auto.pkrvars.hcl
file : packer build folder/
# folder/varfile.auto.pkrvars.hcl
foo = "bar"
# this file needs to be in the same folder as your buildfiles and packer will just parse it as a varfile
packer -var-file /home/azr/shared_confs/foo.pkrvars.hcl -var foo=bar ...
:# this file can be anywhere but it's extension needs to be "pkrvars.hcl" ( or ". pkrvars json" for the json format )
foo = "bar"
The further down this list the more priority the setting will have. auto varfiles are sorted in lexical order of their filenames. -var
and -var-file
s are not auto sorted and the last one that sets a variable 'wins'.
⚠️ Note that pkrvars.hcl
files have a specific layout and permit setting variables directly without a body.
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.
Most helpful comment
We are currently aiming to have the HCL2 integration in 6 months or so, and I believe this should be addressed by that change.