4
votes

I'm building a vagrant .box file using packer. I've got something like this in my packer .json config:

"post-processors": [
//SNIP
  "include": [
    "./choco.ps1",
    "./vagrantUp.ps1",
    ]
]

It's building the .box, and if I manually untar the file I can see those are included in there. When I end up running "vagrant box add Mybox http://whatever" I can even see them in my ~/.vagrant.d/boxes/MyBox/0/virtualbox folder.

 ~ $ ls ~/.vagrant.d/boxes/MyBox/0/virtualbox/
Vagrantfile                 metadata.json
box.ovf                     packer-virtualbox-iso-1424829938-disk1.vmdk
choco.ps1                   vagrantUp.ps1

Also, in my packer vagrant template I have a line:

config.vm.provision  "shell", path: "vagrantUp.ps1"

Next, I want to initialize this machine. I did the following:

~ $ cd ~/Vagrant
Vagrant $ Vagrant mkdir MyBox; cd MyBox
MyBox $ vagrant init MyBox
MyBox $ ls
VagrantFile

This file looks like the basic vagrant default, but at the top it mentions config.vm.box = "MyBox".

But then if I vagrant up, I get the following error:

* `path` for shell provisioner does not exist on the host system: /Users/me/Vagrant/MyBox/vagrantUp.ps1

It looks to me like the VagrantFile in /Vagrant/MyBox is referencing the VagrantFile in ~/.vagrant.d/, which is good, that's what I want. But then it's still using the paths relative to /Vagrant/MyBox, when the files are actually relative to ~/.vagrant.d/

Am I missing a step that tells vagrant to copy those files to the instance directory? Or am I referencing them incorrectly in my vagrant template?

2

2 Answers

2
votes

The scripts will be located relative to the Vagrantfile, so on the host machine. Check out the path section in here: http://docs.vagrantup.com/v2/provisioning/shell.html

It clearly states:

Relative paths, such as above, are expanded relative to the location of the root Vagrantfile for your project.

I think that there is no functionality in vagrant init or vagrant init that can extract the scripts and copy it to the local folder where the Vagrantfile resides. I have no idea why they have this functionality in the Packer Vagrant post-processor, under include:

Paths to files to include in the Vagrant box. These files will each be copied into the top level directory of the Vagrant box (regardless of their paths). They can then be used from the Vagrantfile.

3
votes

In the Vagrantfile within the box, you can refer to other files within the box using __FILE__ to the get path to that box Vagrantfile, and then use path instead of inline with the shell provisioner so that the script gets uploaded to the VM and then executed:

Vagrant.configure("2") do |config|
  # ... other configuration

  box_root = File.expand_path(File.dirname(__FILE__))
  config.vm.provision "shell",
    path: File.join(box_root, "vagrantUp.ps1")
end