I am trying to get this working:
packer {
required_version = ">= 1.8.0"
}
locals {
srvbuilds = [
{ buildname = "build1", builddesc = "Build Number 1" },
{ buildname = "build2", builddesc = "Build Number 2" },
{ buildname = "build3", builddesc = "Build Number 3" }
]
}
source "null" "base" {
ssh_host = "127.0.0.1"
ssh_username = "pkrtest"
}
build {
dynamic "source" {
labels = [ "source.null.base" ]
for_each = local.srvbuilds
content {
name = source.value.buildname
ssh_password = "Passw0rd"
}
} //Dynamic source
dynamic "provisioner" {
labels = [ "file" ]
iterator = testing
for_each = { for lb in local.srvbuilds : "mybuild" => lb.buildname
if(lb.buildname == source.name) }
content {
content = templatefile("${path.cwd}/template/testfile.pkrtpl.hcl",
{
re_var = testing.value
})
destination = format("%s-%s%s", "/tmp/packer", "template", ".txt")
}
} //Dynamic provisioner file
provisioner "shell-local" {
inline = [ "echo \"RESULT COMPARE: ${source.name} == $(cat /tmp/packer-template.txt) ?\"" ]
}
} // Build Block
template/testfile.pkrtpl.hcl is a file which contains “${re_var}”
Results:
[mauricio.silveira@localhost simple-test1]$ packer build -parallel-builds=1 . ; cat /tmp/packer-template.txt
null.build1: output will be in this color.
null.build2: output will be in this color.
null.build3: output will be in this color.
==> null.build1: Using SSH communicator to connect: 127.0.0.1
==> null.build1: Waiting for SSH to become available...
==> null.build1: Connected to SSH!
==> null.build1: Uploading /tmp/pkr-file-content3019602318 => <unknown>
null.build1: pkr-file-content3019602318 9 B / 9 B [=====================================================================================================================================================================] 100.00% 0s
==> null.build1: Running local shell script: /tmp/packer-shell3133175045
==> null.build1: cat: /tmp/packer-template.txt: No such file or directory
null.build1: RESULT COMPARE: build1 == ?
Build 'null.build1' finished after 218 milliseconds 809 microseconds.
==> null.build2: Using SSH communicator to connect: 127.0.0.1
==> null.build2: Waiting for SSH to become available...
==> null.build2: Connected to SSH!
==> null.build2: Uploading /tmp/pkr-file-content2581174171 => <unknown>
null.build2: pkr-file-content2581174171 9 B / 9 B [=====================================================================================================================================================================] 100.00% 0s
==> null.build2: Running local shell script: /tmp/packer-shell4188803963
==> null.build2: cat: /tmp/packer-template.txt: No such file or directory
null.build2: RESULT COMPARE: build2 == ?
Build 'null.build2' finished after 218 milliseconds 732 microseconds.
==> null.build3: Using SSH communicator to connect: 127.0.0.1
==> null.build3: Waiting for SSH to become available...
==> null.build3: Connected to SSH!
==> null.build3: Uploading /tmp/pkr-file-content1211750882 => <unknown>
null.build3: pkr-file-content1211750882 9 B / 9 B [=====================================================================================================================================================================] 100.00% 0s
==> null.build3: Running local shell script: /tmp/packer-shell2575105563
==> null.build3: cat: /tmp/packer-template.txt: No such file or directory
null.build3: RESULT COMPARE: build3 == ?
Build 'null.build3' finished after 205 milliseconds 11 microseconds.
==> Wait completed after 642 milliseconds 720 microseconds
==> Builds finished. The artifacts of successful builds are:
--> null.build1: Did not export anything. This is the null builder
--> null.build2: Did not export anything. This is the null builder
--> null.build3: Did not export anything. This is the null builder
cat: /tmp/packer-template.txt: No such file or directory
If I replace source.name with any build name ( build1, build2 or build3 ), it runs, but obviously with the wrong outcome:
[mauricio.silveira@localhost simple-test1]$ packer build -parallel-builds=1 . ; cat /tmp/packer-template.txt
null.build1: output will be in this color.
null.build2: output will be in this color.
null.build3: output will be in this color.
==> null.build1: Using SSH communicator to connect: 127.0.0.1
==> null.build1: Waiting for SSH to become available...
==> null.build1: Connected to SSH!
==> null.build1: Uploading /tmp/pkr-file-content395455136 => /tmp/packer-template.txt
null.build1: pkr-file-content395455136 7 B / 7 B [======================================================================================================================================================================] 100.00% 0s
==> null.build1: Running local shell script: /tmp/packer-shell3688447217
null.build1: RESULT COMPARE: build1 == build2 ?
Build 'null.build1' finished after 195 milliseconds 813 microseconds.
==> null.build2: Using SSH communicator to connect: 127.0.0.1
==> null.build2: Waiting for SSH to become available...
==> null.build2: Connected to SSH!
==> null.build2: Uploading /tmp/pkr-file-content3553321586 => /tmp/packer-template.txt
null.build2: pkr-file-content3553321586 7 B / 7 B [=====================================================================================================================================================================] 100.00% 0s
==> null.build2: Running local shell script: /tmp/packer-shell582934826
null.build2: RESULT COMPARE: build2 == build2 ?
Build 'null.build2' finished after 198 milliseconds 263 microseconds.
==> null.build3: Using SSH communicator to connect: 127.0.0.1
==> null.build3: Waiting for SSH to become available...
==> null.build3: Connected to SSH!
==> null.build3: Uploading /tmp/pkr-file-content4029464749 => /tmp/packer-template.txt
null.build3: pkr-file-content4029464749 7 B / 7 B [=====================================================================================================================================================================] 100.00% 0s
==> null.build3: Running local shell script: /tmp/packer-shell306758313
null.build3: RESULT COMPARE: build3 == build2 ?
Build 'null.build3' finished after 197 milliseconds 672 microseconds.
==> Wait completed after 591 milliseconds 956 microseconds
==> Builds finished. The artifacts of successful builds are:
--> null.build3: Did not export anything. This is the null builder
--> null.build1: Did not export anything. This is the null builder
--> null.build2: Did not export anything. This is the null builder
build2
Is this construct supposed to work?
Any workarounds, or should I open a bug report?