Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use user variable in vagrantfile_template with HCL2 packerfile #10828

Closed
aacebedo opened this issue Mar 25, 2021 · 6 comments
Closed

Comments

@aacebedo
Copy link

Overview of the Issue

I am generating a vagrantfile when creating a box with packer. In this vagrantfile, I use variables defined in the packer file.
In the vagrantfile.tpl I add tags like {{ user myvariable }}. When I use a JSON packer file, everything is ok.
I upgraded my packer json file into the HCL2 format and the syntax seems to have changed for those variables as they are not replaced anymore. I may be missing something here but the document "engine" page of the JSON section does not exist in the HCL section.

Reproduction Steps

  • Create a HCL2 packer file using the vagrant post-processor
  • In the vagrant post-processor define a vagranfile_template file
  • In the vagrantfile template refers to user defined variables coming from the packer file using the {{ user myvariable }} syntax.
  • Run packer build, the variables are not changed.

Packer version

1.7.0

Simplified Packer Buildfile

file.pkr.hcl
[...]
  locals {
   mylocal = "helloworld"
  }
  post-processor "vagrant" {
    keep_input_artifact = true
    vagrantfile_template = ./vagrantfile.tpl
  }
[...]

file vagrantfile.tpl
[...]
something = {{ user `mylocal` }}
[...]

Operating system and Environment details

Linux x86_64

Log Fragments and crash.log files

N/A

@aacebedo aacebedo added the bug label Mar 25, 2021
@aacebedo
Copy link
Author

I have investigate a little bit.
It appears that putting {{ user "VariableName" }} shall be the way to get the values.
However even if I put a local or a variable in the HCL file, the ctx.UserVariables is not empty when the post-processor is called.
This leads to enter the line funcs.go:222 located in packer-plugin-sdk/template/interpolate where the code is:

		if ctx == nil || ctx.UserVariables == nil {
			return "", errors.New("test")
		}

So when the UserVariables variable is empty I got a weird error containing "test" and it fails the build with:

* Post-processor failed: template: root:4:2: executing "root" at <user `.Toto`>: error calling user: test

I am still searching why the UserVariables is empty when the post processor is called

@SwampDragons
Copy link
Contributor

The vagrantfile isn't a full template, and the only variables that can currently be used in it are {{ .BoxName }} and {{ .SyncedFolder }}

https://www.packer.io/docs/builders/vagrant#template

@aacebedo
Copy link
Author

Unfortunately it is the case in the json version and it is a feature I heavily relies on. Thus I won't be able to migrate to hcl2 :(
The code gives the impression that it is a feature that will be added (i.e. the "test" error). Do you think it will be placed on the roadmap or juste considered as nice to have and implemented on a best effort basis ?

@aacebedo
Copy link
Author

aacebedo commented Mar 29, 2021

The vagrantfile isn't a full template, and the only variables that can currently be used in it are {{ .BoxName }} and {{ .SyncedFolder }}

https://www.packer.io/docs/builders/vagrant#template

Note that the link is pointing to the builder. The issue I saw is on the post-processor and its documentation mentions user variables (https://www.packer.io/docs/post-processors/vagrant#output).

@ghost
Copy link

ghost commented Apr 21, 2021

This issue has been automatically migrated to hashicorp/packer-plugin-vagrant#9 because it looks like an issue with that plugin. If you believe this is not an issue with the plugin, please reply to hashicorp/packer-plugin-vagrant#9.

@ghost
Copy link

ghost commented May 22, 2021

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.

@ghost ghost locked as resolved and limited conversation to collaborators May 22, 2021
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants