hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.11k stars 3.33k forks source link

Better support of standard environment variables in HCL2 #10187

Closed zfouts closed 4 years ago

zfouts commented 4 years ago

Please search the existing issues for relevant feature requests, and use the reaction feature (https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to add upvotes to pre-existing requests.

Community Note

Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request. Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request. If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Description

HCL2 seemingly does not support using standard system environment variables without resetting them to PKR_VAR_<NEW_ENV>.

For example, with JSON you can do the following:

    "jenkins_build_user": "{{env `BUILD_USER_ID`}}",

With the template conversion, it creates the following:

variable "jenkins_build_user" {
  type    = string
  default = "${var.BUILD_USER_ID}"
}

Results in the following error:

Error: Variables not allowed

  on centos7-base.json.pkr.hcl line 51, in variable "jenkins_build_user":
  51:   default = "${var.BUILD_USER_ID}"

Variables may not be used here.

Use Case(s)

Being able to utilize standard env vars from your system without having to set some packer specific environment variable such as PKR_VAR_BLAH would be more than beneficial.

Potential configuration

Potentially allow for a ${env.Foo} setting within the variable block, such as:

variable "jenkins_build_user" {
  type    = string
  default = "${env.BUILD_USER_ID}"
}

Potential References

https://discuss.hashicorp.com/t/hcl2-environment-variables/9290

nywilken commented 4 years ago

Hi thanks for reaching out. The PKR_VAR prefix is a requirement to ensure proper variable handling in HCL2 enabled templates. @azr did a great job explaining it here.

I'm going to close this issue as it has already been answered. But if you think we can improve the documentation a bit to call out the requirement. Reach out with the suggestion and we will gladly make the update.

SwampDragons commented 3 years ago

We spent some time discussing this option internally and decided to reverse course on our decision -- you'll be able to use an env function in HCL once https://github.com/hashicorp/packer/pull/10240 is merged.

zfouts commented 3 years ago

Fantastic news!

ghost commented 3 years ago

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.