我使用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
定义了两个名为client
和server
的虚拟机:
% 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...
...
哇!为什么会提起default
? According to the Vagrantfile
docs应该使用随盒子打包的Vagrantfile,并且当前目录中包含的其他Vagrantfile
应该合并到其中。但事实似乎并非如此。
Vagrant 1.7.2 是正在使用的版本。
我希望研讨会参与者能够调出框中包含的Vagrantfile
中定义的VM,而不在带外提供Vagrantfile
(以便最小化依赖性)。我错过了重要的事情吗?
答案 0 :(得分:0)
正如我们所知,Vagrant以预先确定的顺序加载和合并多个Vagrantfiles here(在同一部分中,他们也指出多个Vagrant.configure
块也可以)。< / p>
当Vagrant无法使用config.vm.communicator = "winrm"
获取捆绑在.box中的vagrantfile_template
配置时,我遇到了同样的问题。
但是,通过使用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
<强> TL; DR:强>
确保没有&#34; old&#34; vagrant.d
中存在可能会覆盖&#39; packer
- ed框中配置内容的Vagrantfiles(请参阅&#34;加载顺序和合并&#34;在上面的链接中)