-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Data Sources cannot use local values #11011
Comments
Hi @KOConchobhair thanks for reaching out. I believe this has to do with the fact that Packer tries to resolve datasources before locals, as locals can contain the output of a datasource as its value. Which essentially means that locals, at this time, can't be used as the arguments for a datasource because Packer does not resolved the "local" attributes yet. I could be mistaken here so I'll bubble this up to the team for some additional input. But as a workaround, if you are stuck, you can use variables instead of locals here since all of the values seem to be hard coded. Locals are great for when you have a variable whose value is some sort of expression (e.g result of datasource, external function call, or manipulation of variable data). |
This will be solved when we introduce a dependency graph to Packer HCL2. But we're shy on doing that while JSON is still inside Packer core. This will take some time. |
Okay, that all makes sense. I was just surprised I was the first to encounter this so I thought it might have been something I was doing wrong haha. I tend to use locals more like constants in HCL but I can certainly use variables here as a workaround. |
I can be wrong but the purpose of 'local' is to build a new variable out of existing variable(s). If you assign just one value to the variable it's better to use 'variable' with a default value. |
Just stumbled across this today, i wonder what makes it difficult in packer to fix this as it works on terraform? |
@project0 great question. It boils down to ordering and dependency handling between blocks in Packer. Terraform has the resource graph which manages dependencies and can determine if Packer on the other hand doesn't have a graph, yet, so it has to manually enforce order. In this case that datasources must be evaluated before locals in order for their values to be used in locals. We plan to introduce a graph in a later release of Packer, which is why this issue has been assigned the hcl2-dag label. I hope this explanation clarifies things a bit. |
Just curious how this looks on the current roadmap. Use of I know I could theoretically develop a custom data that supports external data similar to TF, but I am a bit wary after being one of the small number of Packer plugin third party developers and finding information gaps that I had to assemble between various other sources. Thanks! |
For anyone looking for a (partial) workaround - while we cannot yet use locals within a data source, we can use other data sources. What's even better, there is a null data source (undocumented?) which takes an arbitrary expression that produces a string. So you can do things like: data "null" "basic-auth" {
input = join(" ", [
"Basic", base64encode(join(":", ["username", vault("kv/data/secrets", "password")]))
])
}
data "http" "call-rest-api" {
url = "http://host:port/some/rest/api"
request_headers = {
Accept = "application/json"
Authorization = data.null.basic-auth.output
}
} i.e. you could re-use that computed |
Thanks for the cool workaround on the feature gap with the undocumented feature. There is an "optional provisioner" feature I worked out also using undocumented Packer features, so this kind of solution is not a rare implementation design. Now we have closer to best practices, and the code in data blocks does not have to be an unreadable mess. The above trick is also probably going to be a long-term solution for this also because of the reverse dependency decision, and the effort to support both would be a significant development effort that is currently stalled. Thanks again! Edit: Also thanks to Megan and Adrien for the |
This issue has been synced to JIRA for planning. JIRA ID: HPR-755 |
I want to bump this up again. It's still a problem in 2024! I also just tried to pass a computed url to the http data-source and got the error below and got totally confused since its there. We need to be able to have computed inputs for data-sources and it's wired that this shortcoming is not documented anywhere while the error message is not very helpful. Is there a update for the roadmap supporting this.
Thanks for the workaround @nirvana-msu |
I think this is now resolved, possibly by this feature in 1.12.0:
At least, I'm able to use a local variable in my data-source now. |
Overview of the Issue
Trying to use local values in a data source configuration and getting errors from packer validate.
Reproduction Steps
Packer version
Packer v1.7.2
Simplified Packer Buildfile
Operating system and Environment details
Linux KOCONNOR 4.4.0-17763-Microsoft #1432-Microsoft Mon Aug 18 18:18:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
(WSL in Windows 10)
Log Fragments and crash.log files
The text was updated successfully, but these errors were encountered: