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

packer functions not recognized in map variables #12559

Closed
youngturk2 opened this issue Aug 8, 2023 · 3 comments
Closed

packer functions not recognized in map variables #12559

youngturk2 opened this issue Aug 8, 2023 · 3 comments

Comments

@youngturk2
Copy link

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 other comments that do not add relevant new information or questions, 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

Overview of the Issue

I am trying to use Packer's formatdate and timestamp functions to create a variable, eg.:

formatdate("YYYY-MM-DD", timestamp())

However, I can't seem to get these functions to work when using them to declare a variable. Attempting to declare the below variable:

variable testfunc {
    default = formatdate("YYYY-MM-DD", timestamp())
}

Results in the below error message:

Error: Call to unknown function
  on variables.pkr.hcl line 48, in variable "testfunc":
  48:     default = formatdate("YYYY-MM-DD", timestamp())


  on variables.pkr.hcl line 48, in variable "testfunc":
There is no function named "formatdate".
  48:     default = formatdate("YYYY-MM-DD", timestamp())

I only seem to be able to use packer's built-in functions if they are utilized in a local or locals block, so the below locals block works perfectly fine:

locals {
  tags = {
    createdon = formatdate("YYYY-MM-DD", timestamp())
  }
}

Can't imagine that this is by design, as I have seen other past issues that appear to also reference this issue, but are now closed (eg #9430 ). Not sure why the issue was closed, as the issue does not appear to have been fixed.

Reproduction Steps

  1. Create a packer config file
  2. define a variable and set the default value to use the timestamp function
  3. run packer init .

Packer version

packer -version
1.9.2

Simplified Packer Template

variable testfunc {
    default = formatdate("YYYY-MM-DD", timestamp())
}

Operating system and Environment details

Windows server 2016 x64

Log Fragments and crash.log files

Error: Call to unknown function
  on variables.pkr.hcl line 48, in variable "testfunc":
  48:     default = formatdate("YYYY-MM-DD", timestamp())


  on variables.pkr.hcl line 48, in variable "testfunc":
There is no function named "formatdate".
  48:     default = formatdate("YYYY-MM-DD", timestamp())


2023/08/07 20:14:51 [INFO] (telemetry) Finalizing.
There is no function named "formatdate".


2023/08/07 20:14:52 waiting for all plugin processes to complete...
@youngturk2 youngturk2 added the bug label Aug 8, 2023
@Stromweld
Copy link

This is actually by design of the HCL language. In the issue you linked in your description the reason why that one was closed is found in this comment #9430 (comment). Variables are meant to be constant values and local variables are meant for computing a value. This separation is meant to keep it clear what is generated and what is provided by the caller.

@nywilken
Copy link
Contributor

nywilken commented Dec 7, 2023

Thanks for the assist @Stromweld. @youngturk2 I'm going to close this as it is not an issue with variables. But please feel free to reach out if you run into any other unexpected variable questions or behaviors.

Copy link

github-actions bot commented Jan 7, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants