hashicorp / hashicorp/packer

Cannot interpolate build variables in nested source block

Open
#12,439 0 comments 21 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

I'd like to use a [build variable](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/contextual-variables#build-variables) in a nested `source` block like can be done in `provisioner` blocks. That way builder options can be filled in using build variables. Unfortunately, I get `Unsupported attribute` errors from usage in a `source` block.

#### Reproduction Steps

With the following HCL template where the `build` block has both a `source` block and `provisioner` block reference build variables:

```
source "qemu" "vm" {
iso_url = "https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2"
iso_checksum = "file:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS"
disk_image = true
disk_size = "3G"
format = "qcow2"
ssh_username = "debian"

qemuargs = [
# Tell cloud-init to use the HTTP content as the NoCloud datasource
# via DMI data.
["-smbios", "type=1,serial=ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/"],
]
}

build {
name = "qemu"

source "qemu.vm" {
name = "test"

http_content = {
"/meta-data" = yamlencode({
"instance-id" = "packer"
"local-hostname" = build.name
"public-keys" = [build.SSHPublicKey]
})
}
}

provisioner "shell" {
inline = ["echo '${build.name} ${build.SSHPublicKey}'"]
}
}
```

Validation fails as follows:

```
$ packer validate build.pkr.hcl
Error: Unsupported attribute

on build.pkr.hcl line 25:
(source code not available)

This object does not have an attribute named "name".

Error: Unsupported attribute

on build.pkr.hcl line 26:
(source code not available)

This object does not have an attribute named "SSHPublicKey".

```

So, the `build` variables were interpolated correctly in the `provisioner` block, but failed in the `source` block.

### Packer version

Packer v1.8.7

### Simplified Packer Template

See above.

### Operating system and Environment details

OS, Architecture, and any other information you can provide about the
environment.

Linux x86_64

### Log Fragments and crash.log files

Include appropriate log fragments. If the log is longer than a few dozen lines,
please include the URL to the [gist](https://gist.github.com/) of the log or
use the [Github detailed format](https://gist.github.com/ericclemmons/b146fe5da72ca1f706b2ef72a20ac39d) instead of posting it directly in the issue.

Set the env var `PACKER_LOG=1` for maximum log detail.

```
$ PACKER_LOG=1 packer validate build.pkr.hcl
2023/05/25 11:41:58 [INFO] Packer version: 1.8.7 [go1.20.4 linux amd64]
2023/05/25 11:41:58 Old default config directory found: /home/dan/.packer.d
2023/05/25 11:41:58 [TRACE] discovering plugins in /usr/bin
2023/05/25 11:41:58 Old default config directory found: /home/dan/.packer.d
2023/05/25 11:41:58 [TRACE] discovering plugins in /home/dan/.packer.d/plugins
2023/05/25 11:41:58 [TRACE] discovering plugins in .
2023/05/25 11:41:58 [INFO] PACKER_CONFIG env var not set; checking the default config file path
2023/05/25 11:41:58 [INFO] PACKER_CONFIG env var set; attempting to open config file: /home/dan/.packerconfig
2023/05/25 11:41:58 [WARN] Config file doesn't exist: /home/dan/.packerconfig
2023/05/25 11:41:58 Old default config directory found: /home/dan/.packer.d
2023/05/25 11:41:58 [INFO] Setting cache directory: /home/dan/.cache/packer
2023/05/25 11:41:58 Old default config directory found: /home/dan/.packer.d
2023/05/25 11:41:58 [TRACE] Starting internal plugin packer-builder-qemu
2023/05/25 11:41:58 Starting plugin: /usr/bin/packer []string{"/usr/bin/packer", "plugin", "packer-builder-qemu"}
2023/05/25 11:41:58 Waiting for RPC address for: /usr/bin/packer
2023/05/25 11:41:58 packer-builder-qemu plugin: [INFO] Packer version: 1.8.7 [go1.20.4 linux amd64]
2023/05/25 11:41:58 packer-builder-qemu plugin: Old default config directory found: /home/dan/.packer.d
2023/05/25 11:41:58 packer-builder-qemu plugin: [INFO] PACKER_CONFIG env var not set; checking the default config file path
2023/05/25 11:41:58 packer-builder-qemu plugin: [INFO] PACKER_CONFIG env var set; attempting to open config file: /home/dan/.packerconfig
2023/05/25 11:41:58 packer-builder-qemu plugin: [WARN] Config file doesn't exist: /home/dan/.packerconfig
2023/05/25 11:41:58 packer-builder-qemu plugin: Old default config directory found: /home/dan/.packer.d
2023/05/25 11:41:58 packer-builder-qemu plugin: [INFO] Setting cache directory: /home/dan/.cache/packer
2023/05/25 11:41:58 packer-builder-qemu plugin: args: []string{"packer-builder-qemu"}
2023/05/25 11:41:58 packer-builder-qemu plugin: Old default config directory found: /home/dan/.packer.d
2023/05/25 11:41:58 packer-builder-qemu plugin: Plugin address: unix /tmp/packer-plugin23911092
2023/05/25 11:41:58 packer-builder-qemu plugin: Waiting for connection...
2023/05/25 11:41:58 Received unix RPC address for /usr/bin/packer: addr is /tmp/packer-plugin23911092
2023/05/25 11:41:58 packer-builder-qemu plugin: Serving a plugin connection...

on build.pkr.hcl line 25:
(source code not available)

This object does not have an attribute named "name".

Error: Unsupported attribute

on build.pkr.hcl line 26:
(source code not available)
Error: Unsupported attribute

This object does not have an attribute named "SSHPublicKey".

2023/05/25 11:41:58 [INFO] (telemetry) Finalizing.
on build.pkr.hcl line 25:
(source code not available)

This object does not have an attribute named "name".

Error: Unsupported attribute

on build.pkr.hcl line 26:
(source code not available)

This object does not have an attribute named "SSHPublicKey".

2023/05/25 11:41:58 waiting for all plugin processes to complete...
2023/05/25 11:41:58 /usr/bin/packer: plugin process exited
```

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.