-
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
Dynamic build blocks are not supported #11301
Comments
Hi there @mpalmer, I went ahead an opened a PR https://github.com/hashicorp/packer/pull/11404/files to call out where dynamic blocks can be used. Please let me know if that is enough detail. Dynamic blocks are only supported for creating nested blocks within top-level block constructs such as Do you have an example use case for wanting to generate build blocks? For what it is worth, you can use dynamic to generate source blocks within a build block. See the example below. variable "names" {
type = list(string)
default = ["testone", "testtwo"]
}
source "null" "example" {
communicator = "none"
}
build {
dynamic "source" {
for_each = var.names
labels = ["null.example"]
content {
name = source.value
}
}
provisioner "shell-local" {
inline = ["echo hello from ${source.name}"]
}
}
|
Yes, I think that your PR clarifies the situation sufficiently. My use case for dynamic source build blocks is (or rather, was, because I've ended up going in a different direction altogether now), roughly the following:
I was not able to figure out how to handle all these requirements in one go, and maintain parallelism, without copy-pasting the variable "source_mapping" {
type = map(string)
default = {
"testone" = "example-a",
"testtwo" = "example-b",
}
}
variable "names" {
type = list(string)
default = ["testone", "testtwo"]
}
source "null" "example-a" {
communicator = "none"
}
source "null" "example-b" {
communicator = "none"
}
build {
dynamic "source" {
for_each = var.names
labels = ["null.${var.source_mapping[source.value]}"]
content {
name = source.value
}
}
provisioner "shell-local" {
only = ["null.example-a"]
inline = ["echo hella from ${source.name}"]
}
provisioner "shell-local" {
only = ["null.example-b"]
inline = ["echo hellb from ${source.name}"]
}
} Neither of those provisioner blocks fire because the name of the source isn't Meanwhile, in a "dynamic build block" world, I thought something like this would do the trick: # same vars and sources
dynamic "build" {
for_each = var.source_mapping
sources = ["null.${build.value}"]
provisioner "shell-local" {
only = ["null.example-a"]
inline = ["echo hella from ${build.key}"]
}
provisioner "shell-local" {
only = ["null.example-b"]
inline = ["echo hellb from ${build.key}"]
}
} Now I realise that there'd be overlap between the build names (because the build name comes from source name, which would be the same for all builds that use the same source) but I vaguely recall a feature request somewhere for adding the ability to specify the build name in the build, rather than unconditionally using the source name. |
@mpalmer thanks for the detailed use case. This issue was closed with the PR merge. But given your use case I will reopen so that we can use it as feedback for the continued HCL2 work. Thanks again for opening up this issue, and for your detailed responses. |
Overview of the Issue
The description of dynamic blocks at https://www.packer.io/docs/templates/hcl_templates/expressions#dynamic-blocks states:
However, when attempting to create a dynamic build block, like this:
If dynamic
build
blocks aren't actually supposed to work (:cry:), the documentation should reflect the reality that dynamic blocks are not, in fact, "supported anywhere".Reproduction Steps
See above.
Packer version
1.7.6.
Simplified Packer Buildfile
Operating system and Environment details
Debian 11 on x86_64.
Log Fragments and crash.log files
The text was updated successfully, but these errors were encountered: