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

user variables not working in vagrant post provisioner when using vagrantfile.template #12682

Closed
jamesla opened this issue Nov 4, 2023 · 3 comments

Comments

@jamesla
Copy link

jamesla commented Nov 4, 2023

I have configured a variable in my packer file like so:

{
  "variables": {
    "some_variable": "some_value"
  },
  // ... other configurations ...
}

configuring my vagrant post provisioner like this:

post-processor "vagrant" {
  vagrantfile_template = "path/to/Vagrantfile.template"
}

and trying to interpolate the variable like this in my vagrantfile.template

Vagrant.configure("2") do |config|
  # Use the variable like this:
  config.vm.provider "virtualbox" do |vb|
    vb.name = "{{ user `some_variable` }}"
  end
end

and get the following error:

* Post-processor failed: template: root:17:25: executing "root" at <user `some_variable`>: error calling user: test

The docs are very limited in terms of this functionality - am I doing this right? How is it supposed to work?

Copy link

github-actions bot commented Nov 4, 2023

Hi 👋 thanks for reaching out.

For general questions we recommend reaching out to the [community forum](https://discuss.hashicorp.com/c/packer) for greater visibility.
As the GitHub issue tracker is only watched by a small subset of maintainers and is really reserved for bugs and enhancements, you'll have a better chance of finding someone who can help you in the forum.
We'll mark this issue as needs-reply to help inform maintainers that this question is awaiting a response.
If no activity is taken on this question within 30 days it will be automatically closed.

If you find the forum to be more helpful or if you've found the answer to your question elsewhere please feel free to post a response and close the issue.

@jamesla jamesla changed the title user variables not working in vagrant post provisioner user variables not working in vagrant post provisioner when using vagrantfile.template Nov 4, 2023
@nywilken
Copy link
Contributor

nywilken commented Nov 10, 2023

Hi @jamesla user variables within the vagrant template file is not something that is supported. Only the following template variables are allowed for use with a vagrant template: {{ .BoxName }}, {{ .SyncedFolder }}, and {{.InsertKey}}, which correspond to the Packer options box_name, synced_folder, and insert_key.

Alternatively, the template variable {{.DefaultTemplate}} is available for use if you wish to extend the default generated template.

Since you are looking to create a template with custom variable data I recommend you take a look at the templatefile function for generating the vagrant file.

locals {
 vagrantfile            = {
        "path/to/Vagrantfile.template" = templatefile("${path.root}/vagrantfile.pkrtpl", var.some_variable)
      }
}

post-processor "vagrant" {
  vagrantfile_template = local.vagrantfile
}

If you're struggling to get Packer working for the first time, I'd recommend reaching out to the community forum for help. The GitHub issue tracker is only watched by a small subset of maintainers and is really reserved for bugs rather than questions. You'll have a better chance of finding someone who can help you in the forum.

I'm going to close this issue as it is not a bug but feel free to reach out if you run into an issue with the templatefile function that you believe may be a bug.

Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 11, 2023
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

2 participants