hashicorp / hashicorp/packer

Using contextual source variable in dynamic provisioner block fails

Open
#12,415 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
15.8k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

#### Community Note

* Please vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) 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

When filing a bug, please include the following headings if possible. Any
example text in this template can be deleted.

#### Overview of the Issue

When using `source.name` in the `for_each` of a dynamic provisioner block the result is`` and fails inconsistently.

The `templatefile` and `file` function does not error out, but maybe they should since no real data is produced. The direct use of `source` parameter in the file provisioner does cause an error when trying to validate that the file exists.

I would attempt to use the `sources` parameter, but that would remove the option to use templates.

The codeblock below generates this error message during validation:

```txt
Error: Failed preparing provisioner-block "file" ""

on test.pkr.hcl line 49:
(source code not available)

1 error(s) occurred:

* Bad source '': stat : no such file or directory

Error: Failed preparing provisioner-block "file" ""

on test.pkr.hcl line 49:
(source code not available)

1 error(s) occurred:

* Bad source '': stat : no such file or directory
```

#### Reproduction Steps

Running `packer validate .` on the codeblock below is enough to demonstrate the issue.

### Packer version

Packer v1.8.7

### Simplified Packer Template

```hcl2
locals {
# Remote directory for file uploads
upload_dir = "/tmp/packer-files"

# Used during compiling of .pktpl template files before uploading
template_vars = {
"obladi" = "oblada"
}

# Source files
filesets = {
"compiled" = [for f in fileset(path.root, "compiled/**") : abspath("${path.root}/${f}") if f != trimsuffix(f, ".pktpl")]
"raw1" = [for f in fileset(path.root, "raw1/**") : abspath("${path.root}/${f}") if f == trimsuffix(f, ".pktpl")]
"raw2" = [for f in fileset(path.root, "raw2/**") : abspath("${path.root}/${f}") if f == trimsuffix(f, ".pktpl")]
}
}

source "null" "base" {
communicator = "none"
}

build {
name = "application"

source "source.null.base" {
name = "raw1"
}

source "source.null.base" {
name = "raw2"
}

# Compile and upload template files
dynamic "provisioner" {
labels = ["file"]
for_each = local.filesets["compiled"]
iterator = item

content {
####
# This does not error, but maybe it should have, no real data is produced
####
content = templatefile(item.value, local.template_vars)
destination = "${local.upload_dir}/${trimsuffix(item.value, ".pktpl")}"
}
}

# Upload raw (non-template) files
dynamic "provisioner" {
labels = ["file"]
for_each = local.filesets[source.name]
iterator = item

content {
####
# This tries to validate that the file exists, and thus errors out
####
source = item.value
destination = "${local.upload_dir}/${item.value}"
}
}
}
```

### Operating system and Environment details

* Using pkenv wrapper
* Darwin thlarsen-mac.local 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar 6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000 arm64

### Log Fragments and crash.log files

https://gist.github.com/thomasfinstad/81be8e7be18c801ca3a9a9952eb367a7

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.