Vagrant provision 使用 .box 中包含的文件从 packer

Vagrant provision using files included in .box from packer

我正在使用加壳器构建一个 vagrant .box 文件。我的打包机中有这样的东西。json 配置:

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

它正在构建 .box,如果我手动解压文件,我可以看到它们包含在其中。当我结束时 运行 "vagrant box add Mybox http://whatever" 我什至可以在我的 ~/.vagrant.d/boxes/MyBox/0/virtualbox 文件夹中看到它们。

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

此外,在我的 packer vagrant 模板中有一行:

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

接下来,我要初始化这台机器。我做了以下事情:

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

这个文件看起来像基本的 vagrant 默认值,但在顶部它提到了 config.vm.box = "MyBox"

但是如果我 vagrant up,我会收到以下错误:

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

在我看来,/Vagrant/MyBox 中的 VagrantFile 引用了 ~/.vagrant.d/ 中的 VagrantFile,这很好,这就是我想要的。但是当文件实际上是相对于 ~/.vagrant.d/

时,它仍然使用相对于 /Vagrant/MyBox 的路径

我是否遗漏了告诉 vagrant 将这些文件复制到实例目录的步骤?还是我在 vagrant 模板中错误地引用了它们?

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

它明确指出:

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

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

下的 Packer Vagrant post-处理器中具有此功能

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.

在框中的 Vagrantfile 中,您可以使用 __FILE__ 引用框中的其他文件到该框 Vagrantfile 的获取路径,然后使用 path 而不是 inlineshell provisioner 以便脚本被上传到 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