hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.

Home Page:http://www.packer.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

jamesla opened this issue · comments

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?

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.

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.

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.