When trying to follow the steps in the packer tutorial (Variables | Packer - HashiCorp Learn)
the variable definitions do not get auto loaded from the files within the directory unless a default value is included in the HCL file.
packer version: Packer v1.8.0
OS: Linux 5.16.14-1-MANJARO
For the tutorial only 2 files are in use
docker-ubuntu.pkr.hcl
variables.auto.pkrvars.hcl
docker-ubuntu.pkr.hcl
packer {
required_plugins {
docker = {
version = ">= 0.0.7"
source = "github.com/hashicorp/docker"
}
}
}
source "docker" "ubuntu" {
image = var.docker_image
commit = true
}
source "docker" "ubuntu-bionic" {
image = "ubuntu:bionic"
commit = true
}
build {
name = "learn-packer"
sources = [
"source.docker.ubuntu",
"source.docker.ubuntu-bionic"
]
provisioner "shell" {
environment_vars = [
"FOO=hello world",
]
inline = [
"echo Adding file to Docker Container",
"echo \"FOO is $FOO\" > example.txt",
]
}
post-processor "docker-tag" {
repository = "learn-packer"
tags = [var.docker_tag, "packer-rocks"]
only = ["docker.ubuntu"]
}
post-processor "docker-tag" {
repository = "learn-packer"
tags = ["ubuntu-bionic", "packer-rocks"]
only = ["docker.ubuntu-bionic"]
}
}
variables.auto.pkrvars.hcl
docker_image = "ubuntu:groovy"
docker_tag = "ubuntu-groovy"
Then running
packer build .
Warning: Undefined variable
A "docker_image" variable was set but was not found in known variables. To
declare variable "docker_image", place this block in one of your .pkr files,
such as variables.pkr.hcl
....
--> learn-packer.docker.ubuntu-bionic: Imported Docker image: learn-packer:packer-rocks with tags learn-packer:ubuntu-bionic learn-packer:packer-rock
packer build -var-file=variables.auto.pkrvars.hcl .
Warning: Undefined variable
A "docker_image" variable was set but was not found in known variables. To
declare variable "docker_image", place this block in one of your .pkr files,
such as variables.pkr.hcl
....
--> learn-packer.docker.ubuntu-bionic: Imported Docker image: learn-packer:packer-rocks with tags learn-packer:ubuntu-bionic learn-packer:packer-rock
If I then add default variables to the hcl
docker-ubuntu.pkr.hcl
packer {
required_plugins {
docker = {
version = ">= 0.0.7"
source = "github.com/hashicorp/docker"
}
}
}
variable "docker_image" {
type = string
default = "ubuntu:xenial"
}
variable "docker_tag" {
type = string
default = "ubuntu-xenial"
}
source "docker" "ubuntu" {
image = var.docker_image
commit = true
}
source "docker" "ubuntu-bionic" {
image = "ubuntu:bionic"
commit = true
}
build {
name = "learn-packer"
sources = [
"source.docker.ubuntu",
"source.docker.ubuntu-bionic"
]
provisioner "shell" {
environment_vars = [
"FOO=hello world",
]
inline = [
"echo Adding file to Docker Container",
"echo \"FOO is $FOO\" > example.txt",
]
}
post-processor "docker-tag" {
repository = "learn-packer"
tags = [var.docker_tag, "packer-rocks"]
only = ["docker.ubuntu"]
}
post-processor "docker-tag" {
repository = "learn-packer"
tags = ["ubuntu-bionic", "packer-rocks"]
only = ["docker.ubuntu-bionic"]
}
}
packer build .
....
--> learn-packer.docker.ubuntu: Imported Docker image: learn-packer:packer-rocks with tags learn-packer:ubuntu-groovy learn-packer:packer-rocks
--> learn-packer.docker.ubuntu-bionic: Imported Docker image: learn-packer:packer-rocks with tags learn-packer:ubuntu-bionic learn-packer:packer-rocks
It does not mention in the tutorial or in the documentation for variables that a default is required in order for build command to run and seems that the build and validate commands will only run correctly if defaults are included.