I’ve been trying to figure out how to help create a kind of opinionated template using packer. Essentially I want to use a base source as a “template” and make it work with some pre-provided data but I want some of the values to be able to be overridden for the builds that are prescribed, i.e. iterate through a local defined object but allow a defined variable to work using the lookup function
locals {
build_types = {
test_build1 = {
test1 = "value1"
ssh_user = "myuser1"
}
test_build2 = {
test1 = "foo"
ssh_user = "my_user2"
}
}
test_lookup = {
null = "myval"
test = "mytest"
}
}
variable "testvar" {
type = string
#default = ""
}
source "null" "template" {
ssh_host = "127.0.0.1"
ssh_password = "password"
}
build {
name = "testing"
description = "dynamic build testing"
dynamic "source" {
for_each = local.build_types
labels = ["source.null.template"]
content {
name = source.key
ssh_username = lookup(var, testvar, source.value.ssh_user)
}
}
provisioner "shell-local" {
inline = [
"echo Build Name is ${source.name}",
"echo Test1 Value is ${local.build_types[source.name].test1}",
"echo Lookup Value is ${lookup(local.test_lookup, source.type, local.test_lookup["test"])}"
]
}
}
I get different errors on the commented section. If I keep it as is (with the comment), it complains that the variable is unset:
❯ packer validate .
Error: Unset variable "testvar"
A used variable must be set or have a default value; see
https://packer.io/docs/templates/hcl_templates/syntax for details.
If I uncomment the line, it gives a different error
❯ packer validate .
Error: Unknown variable
on file.pkr.hcl line 39:
(source code not available)
There is no variable named "testvar".
Error: Unknown variable
on file.pkr.hcl line 39:
(source code not available)
There is no variable named "testvar".
From what I can tell, it’s because you are not allowed to use the lookup function in a source block at all. Has anyone else found a way to work around this type of scenario, or am I trying to do something that shouldn’t be possible?