Templating user variables into boot_command

Hey all, does anyone know if there is any documentation or examples for plugging user variables into a boot_command using the VirtualBox provisioner? I know Packer has a couple magic variables for serving up things like kickstart files (.HTTPIP and .HTTPPort), but I’m not sure how to substitute user variables in the same way. The syntax I keep seeing is {user variable}, but I get this error when I try that during a build

Build ‘virtualbox-iso.opnsense’ errored after 14 seconds 935 milliseconds: Error preparing boot command: template: root:1:58: executing “root” at <user foo>: error calling user: test

Anyone know what I’m doing wrong?

It is double curly brackets and the variable in backticks.

{{ user `vm_boot_command` }}

That doesn’t appear to be working
boot_command = ["{{ user `foo` }}"]

==> virtualbox-iso.opnsense: Error preparing boot command: template: root:1:11: executing “root” at : error calling user: test
Also not sure if it’s relevant, but I’m using HCL spec files instead of JSON. Packer version is 1.6.6

HCL is another approach. Have you read the documentation?

1 Like

Huh, I searched through the docs a few times and never saw this guide, guess I must have missed it. Looks like the syntax for HCL is different, more akin to traditional bash interpolation e.g. ${var.sudo_password}

That works for me, thanks for pointing that out.

1 Like

Could you share your config, please?

I’d like to see your working example as well, @rbkaspr. Im using HCL2 (packer 1.6.6) and trying to substitute a var in my boot_command (for virtualbox-iso builder) with${var.preseed_path}, which is an HCL var that I want to set differently for different builds of the same build source. I see how to “evaluate” the variable syntactically in my boot_command, but I’m not seeing a way to set the variable in the build block or in the source block nested in my build block… Packer Validate gives me “Error: Argument or block definition required” when I put var.preseed_path in either of those locations.

Does this just mean that (by design or omission?), you cannot change var values in a packer build block or build source block, but you can only use them?

UPDATE: found this pre-HCL2-discussion about substituting vars in boot_command. Will try to follow up on this question as a new issue or feature request…