我使用packer为我正在运行的车间创建vagrant框,并通过Vagrant后处理器中的vagrantfile_template指令将vagrantfile包装在框中,如下所示:

...
"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的虚拟机:
% 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 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(包括当前目录中的Vagrantfile)合并到其中。但这似乎并非如此。

Vagrant 1.7.2 是正在使用的版本。

我希望研讨会的参与者能够调出框中包含的Vagrantfile定义的VM,而不必提供带外的ojit_code(以使依赖性最小化)。我错过了重要的事情吗?

最佳答案

众所周知,Vagrant以预定顺序here加载并合并了多个Vagrantfile(在同一部分中,他们还指出,多个Vagrant.configure块也可以)。

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

但是,通过使用vagrant --debug,我注意到Vagrant似乎已经缓存了同一盒先前版本中的 bundle 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

TL; DR:

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

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

10-11 00:22