4

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?

4

2 回答 2

3

Vagrantfile框中,您可以使用该框__FILE__的获取路径来引用框内的其他文件Vagrantfile,然后使用path而不是inlineshell配置器一起使用,以便将脚本上传到VM然后执行:

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
于 2015-08-13T01:03:12.240 回答
2

脚本将相对于Vagrantfile, 所以位于主机上。查看此处的path部分:http: //docs.vagrantup.com/v2/provisioning/shell.html

它明确指出:

相对路径(例如上面)相对于项目的根 Vagrantfile 的位置进行扩展。

我认为其中没有vagrant initvagrant init可以提取脚本并将其复制到所在的本地文件夹的功能Vagrantfile。我不知道为什么他们在 Packer Vagrant 后处理器中有这个功能,在include

要包含在 Vagrant 框中的文件的路径。这些文件都将被复制到 Vagrant 框的顶级目录中(无论它们的路径如何)。然后可以从 Vagrantfile 使用它们。

于 2015-02-25T07:23:59.207 回答