不使用打包在盒子中的 Vagrantfile(通过打包器)

标签 vagrant vagrantfile packer

我用了创建一个 我正在运行和包装的车间的盒子 通过 Vagrant 后处理器中的 vagrantfile_template 指令在框中输入,如下所示:

...
"post-processors": [{
  "type": "vagrant",
  "vagrantfile_template": "vagrantfile_templates/workshop",
  "compression_level": "{{user `compression_level`}}",
  "output": "fedora-22-x86_64.box"
}],
...

生成的 .box 的内容是:

% tar -tf workshop.box
Vagrantfile
box.ovf
metadata.json
packer-fedora-22-x86_64-disk1.vmdk

框中Vagrantfile 的内容似乎没问题,并且包含打包程序配置中指定的vagrantfile_template 的内容。请注意,此 Vagrantfile 定义了两个名为 clientserver 的 VM:

% tar -O -xf freeipa-workshop.box Vagrantfile

# The contents below were provided by the Packer Vagrant post-processor

Vagrant.configure("2") do |config|
  config.vm.base_mac = "0800278AF3E8"
end


# The contents below (if any) are custom contents provided by the
# Packer template during image build.
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|

  config.vm.box = "workshop"
  config.vm.synced_folder ".", "/vagrant", disabled: true

  config.vm.define "server" do |server|
    server.vm.network "private_network", ip: "192.168.33.10"
    server.vm.hostname = "server.ipademo.local"
  end

  config.vm.define "client" do |client|
    client.vm.network "private_network", ip: "192.168.33.20"
    client.vm.hostname = "client.ipademo.local"
  end

end

我将盒子添加到名为 workshop 的 vagrant 中:

% vagrant box add --name workshop workshop.box 
==> box: Adding box 'workshop' (v0) for provider: 
    box: Downloading: file:///.../workshop.box
==> box: Successfully added box 'workshop' (v0) for 'virtualbox'!
% vagrant box list
workshop                  (virtualbox, 0)

问题描述

当我执行 vagrant init workshop 然后执行 vagrant up 时,框中包含的 Vagrantfile 未应用:

% vagrant init workshop
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
% cat Vagrantfile 
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "workshop"

  ... and so on (the rest of the default Vagrantfile)

% vagrant up --provider virtualbox
Bringing machine 'default' up with 'virtualbox' provider...
...

哇哦!为什么会出现 defaultAccording to the Vagrantfile docs应使用随盒子打包的 Vagrantfile,并且应将当前目录中的其他 Vagrantfile 合并到其中。但事实并非如此。

Vagrant 1.7.2 是正在使用的版本。

我希望研讨会参与者能够启动包装盒中包含的 Vagrantfile 中定义的 VM,而无需带外提供 Vagrantfile(以便最小化依赖)。我错过了什么重要的事情吗?

最佳答案

众所周知,Vagrant 以预先确定的顺序加载和合并多个 Vagrantfiles here (在同一部分中,他们还指出多个 Vagrant.configure block 也是可以的)。

当 Vagrant 无法获取使用 vagrantfile_template 捆绑在 .box 中的 config.vm.communicator = "winrm" 配置时,我遇到了同样的问题>。

但是,通过使用 vagrant --debug,我注意到 Vagrant 似乎已经缓存了来自同一盒子的先前构建的捆绑 Vagrantfile:

INFO loader: Set :"26224240_win7sp1_x64_virtualbox" = ["#<Pathname:/home/thomas/.vagrant.d/boxes/win7sp1_x64/0/virtualbox/Vagrantfile>"]

有人会认为 vagrant destroy 会删除 vagrant.d 中的关联文件,但事实并非如此,所以我能够通过手动删除 来解决问题>~/.vagrant.d/boxes/win7sp1_x64.

我通过查看确实在使用 WinRM 确认它现在正在按预期工作:

==> default: Waiting for machine to boot. This may take a few minutes...
    default: WinRM address: 127.0.0.1:55985
    default: WinRM username: vagrant
    default: WinRM execution_time_limit: PT2H
    default: WinRM transport: negotiate
==> default: Machine booted and ready!

即使初始化目录下的Vagrantfile没有那个配置:

Vagrant.configure("2") do |config|
    config.vm.box = "win7sp1_x64"
    config.vm.box_url = "/media/data/VagrantBaseBoxes/win7sp1_x64/win7sp1_x64-virtualbox.box"
end

长话短说:

确保 vagrant.d 中不存在可能会覆盖 'packer-ed 框中配置的“旧”Vagrantfile(请参阅“加载顺序和合并”在上面的链接中)。

关于不使用打包在盒子中的 Vagrantfile(通过打包器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229713/

相关文章:

ubuntu - Ubuntu 主机的 Vagrant nfs 同步文件夹问题

bash - Vagrant 报告一个没有任何运行的端口冲突

tdd - 从 Vagrant Ubuntu VM 向主机发送自动测试/保护桌面通知(W7 和 OS X)

chef-infra - 是否有相当于 Berkshelf 的产品,但用于 Puppet 模块?

vagrant - 将 NFS 与 vagrant 一起使用不起作用

amazon-web-services - ami_block_device_mappings 无法与打包程序一起正常工作

packer - 如何在 Packer 中的文件配置器中使用用户变量?

docker - 在打包机构建中使用 docker 卷

ruby - 共享文件夹前的 Vagrant 预配置

ubuntu - 在 ubuntu 18.04 上安装 v8js