Env BUILDTIME not working


My Packer Template incudes the following two variables:

    "vm_name": "linux-ubuntu-18-base-tmpl",
    "buildtime": "{{env `BUILDTIME`}}"

In the “builders” section I have this:
“vm_name”: “{{ user vm_name }}-{{ user buildtime }}”

Running the the build process works fine, but my VM name remains without “buildtime”.
Like this: linux-ubuntu-18-base-tmpl-

I’m using latest Packer version, and “type”: “vsphere-iso”

Am I missing something?

the “builders” section is like this (not sure how the quotes got away:

        "vm_name": "{{ user `vm_name` }}-{{ user `buildtime` }}",

Hi there @krasi-github it looks to me that Packer is not finding the environment variable BUILDTIME. So packer is processing the variable as an empty string.

Is the environment variable being persisted to the shell running Packer?

Depending on how the environment variable is being set you need to make sure that it is persisted so that it is available to other programs like Packer, by either using export BUILDTIME=somevalue on *nix systems or set BUILDTIME=somevalue on Windows systems.

Alternatively if it is a one time env variable you can set it when you call Packer, for example:
BUILDTIME=somevalue packer build template.json

Lastly, I would like to mention that Packer has a timestamp function that you could use if you are looking to randomize the vm_name based off build times.

“vm_name”: “{{ user vm_name }}-{{timestamp}}”

Thanks @nywilken, much appreciated.
I think the timestamp function will do the job for me.

1 Like