I am trying to create auto install Ubuntu OS with packer on Hyper-V and I have project like this:
packer-main/
|
|--->http/
| |--->user-data
|
|--->templates/
| |--->build.pkr.hcl
|
|--->variables
| |--->pkrvars.hcl
content in user-data
#cloud-config
autoinstall:
version: 1
identity:
hostname: jammy-daily
username: ubuntu
content of pkrvars.hcl
hostname = "jammy-daily"
username = "ubuntu"
and build.pkr.hcl
...
ssh_username = "${var.username}"
vm_name = "${var.hostname}"
...
My challenge lies in the need to dynamically modify variables within the user-data before initiating the build process. While I am aware that PowerShell scripts or Bash scripts can serve this purpose, I am curious if there are specialized tools available for streamlining such automations.
I am currently working through something very similar. I found a feature in Packer called templatefile so you can put variables inside your user-data file like so:
#cloud-config
autoinstall:
version: 1
identity:
hostname: ${var.hostname}
username: ${var.build_username}
Those variables can be passed in through var files or at run time.
Then in your build.pkr.hcl
file under source you refer to your template file with http_content, like so:
source "test" "example" {
http_content = {
"/meta-data" = file("${path.root}/http/meta-data")
"/user-data" = templatefile("${path.root}/http/user-data.pkrtpl.hcl", { var = var, local = local })
}
}
Keep in mind that http_directory
can't be defined at the same time as http_content
.