Packer: Failed to interpolate variable

Created on 2 Jul 2020  ·  6Comments  ·  Source: hashicorp/packer

Overview of the Issue

There seems to be a breaking change from 1.5.6 to 1.6.0.

In 1.5.6, this works:
"variables": { "stored_image_name": "{{usersystem_name}}-{{userenvironment}}-{{isotime \"200601020304\"}}",
...
"managed_image_name": "{{userstored_image_name}}",

With 1.6.0 we now get this error:
Error initializing core: Failed to interpolate "stored_image_name": "{{usersystem_name}}-{{userenvironment}}-{{isotime "200601020304"}}"; error: template: root:1:2: executing "root" at <usersystem_name>: error calling user: Error: variable not set: system_name: Please make sure that the variable you're referencing has been defined; Packer treats all variables used to interpolate other user varaibles as required.

Reproduction Steps

Add user variables to variables in the "variables" section.

Packer version

1.6.0

Operating system and Environment details

Ubuntu 18.04LTS x64

bug core regression track-internal

Most helpful comment

Hi there, so looking into this issue further it actually appears that variable interpolation was not working as expected in versions prior to v1.5.5. As the error indicates any variable that is used for interpolation needs to be defined. In later versions of Packer 1.5.5+ variable handling has under gone some refactors to work properly with HCL2 and it appears that Packer is now throwing the correct error when referencing an undefined variable.

I believe the correct fix for this issue is to define any missing variables before trying to reference them. Since you are using a JSON Packer template you can add the variables with null values to ensure they are marked as required.

"variables": {
 "system_name": null,
"environment": null,
"stored_image_name": "{{user `system_name`}}-{{user `environment`}}-{{isotime \"200601020304\"}}"
}

The action item for this issue is to update the CHANGELOG to indicate that interpolation of undefined variables will now error as expected.

All 6 comments

Could you test writing only the value of variable system_name directly to see if Packer is able to interpolate {{user 'environment'}}?
Like:

"variables": { 
    "stored_image_name": "<system_name>-{{user `environment`}}-{{isotime \"200601020304\"}}"
}

I guess you are passing these values via command line, right?

Same issue:

2020-07-02T16:10:07.9050868Z Error initializing core: Failed to interpolate "stored_image_name": "system_name-{{user `environment`}}-{{isotime "200601020304"}}"; error: template: root:1:14: executing "root" at <user `environment`>: error calling user: Error: variable not set: environment: Please make sure that the variable you're referencing has been defined; Packer treats all variables used to interpolate other user varaibles as required.

That's correct, values are passed via command line. This all runs via the Azure Pipeline. Here's the full command:

packer build -force -var "cur_date=${DATE}" \
    -var "vm_size=${VMSIZE}"  \
    -var "assetname=${ASSET_NAME}" \
    -var "managed_image_resource_group_name=${MANAGED_IMAGE_RESOURCE_GROUP_NAME}" \
    -var "virtual_network_name=${VIRTUAL_NETWORK_NAME}" \
    -var "virtual_network_subnet_name=${VIRTUAL_NETWORK_SUBNET_NAME}" \
    -var "virtual_network_resource_group_name=${VIRTUAL_NETWORK_RESOURCE_GROUP_NAME}" \
    -var "build_resource_group_name=${BUILD_RESOURCE_GROUP_NAME}" \
    -var "INITSCRIPT=${REALSCRIPT}" \
    -var "PREPSCRIPT=${PREPSCRIPT}" \
    -var "POSTSCRIPT=${POSTSCRIPT}" \
    -var "system_name=${SYSTEM_NAME}" \
    -var "environment=${environment}" \
    -var "build_id=${BUILD_BUILDID}" \
    ${BUILD_SOURCESDIRECTORY}/packer/generic-pipelines/packer.json

This had no issues at all with 1.5.6.

Nice! Thanks for the details! We’ll take a look as soon as possible.

Hi there, so looking into this issue further it actually appears that variable interpolation was not working as expected in versions prior to v1.5.5. As the error indicates any variable that is used for interpolation needs to be defined. In later versions of Packer 1.5.5+ variable handling has under gone some refactors to work properly with HCL2 and it appears that Packer is now throwing the correct error when referencing an undefined variable.

I believe the correct fix for this issue is to define any missing variables before trying to reference them. Since you are using a JSON Packer template you can add the variables with null values to ensure they are marked as required.

"variables": {
 "system_name": null,
"environment": null,
"stored_image_name": "{{user `system_name`}}-{{user `environment`}}-{{isotime \"200601020304\"}}"
}

The action item for this issue is to update the CHANGELOG to indicate that interpolation of undefined variables will now error as expected.

Apologies again for the confusion here. I've updated the changelog to document this change under the v1.5.5 release notes; I've marked it as a backwards incompatibility as it changes behavior for any user that may have relied on the workaround in previous versions. But I do want to emphasize that the use of undefined variables is an error, which was silently being ignored in previous versions of Packer.

Given how variables have evolved and with the added support for HCL2 I don't suspect that this is a feature we will look to support in the future.

I'm going to mark this issue as resolved. If however, you are still running into issues after defining the variables of if you would like to discuss this issue further please feel free to comment and I will gladly reopen. Cheers!

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings