在启动vagrant box时,“default”这个名称从何而来?
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
有没有一种方法来设置这个?
在启动vagrant box时,“default”这个名称从何而来?
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
有没有一种方法来设置这个?
我发现有多个选项令人困惑,因此我决定测试所有选项,以便准确了解它们的作用。
我正在使用VirtualBox 4.2.16-r86992和Vagrant 1.3.3。
我创建了一个名为nametest
的目录,并运行了
vagrant init precise64 http://files.vagrantup.com/precise64.box
生成默认的Vagrantfile。然后我打开VirtualBox GUI,以便查看我创建的虚拟机名称。
默认的Vagrantfile
Vagrant.configure('2') do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
end
VirtualBox GUI 名称:"nametest_default_1386347922"
注释:名称默认为DIRECTORY_default_TIMESTAMP格式。
定义虚拟机
Vagrant.configure('2') do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.define "foohost"
end
VirtualBox GUI名称: "nametest_foohost_1386347922"
评论: 如果您明确定义了虚拟机, 使用的名称将替换标记“default”。这是vagrant在控制台上输出的名称。根据zook
的输入进行简化。
设置提供程序名称
Vagrant.configure('2') do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.provider :virtualbox do |vb|
vb.name = "foohost"
end
end
VirtualBox GUI名称:“foohost”
注释:如果在提供程序配置块中设置name
属性,则该名称将成为在VirtualBox GUI中显示的完整名称。
组合示例:定义虚拟机和设置提供程序名称
Vagrant.configure('2') do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.define "foohost"
config.vm.provider :virtualbox do |vb|
vb.name = "barhost"
end
end
VirtualBox GUI名称:"barhost"
评论:如果同时使用两种方法,则在提供者配置块中分配给name
的值将获胜。根据评论者zook
的意见进行简化
设置hostname
(奖励)
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.hostname = "buzbar"
end
注释: 这将设置VM内部的主机名。这将是VM中hostname
命令的输出,也是在提示符中可见的内容,例如vagrant@<hostname>
,这里它会显示为vagrant@buzbar
Vagrant.configure('2') do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.hostname = "buzbar"
config.vm.define "foohost"
config.vm.provider :virtualbox do |vb|
vb.name = "barhost"
end
end
所以这就是它。你现在知道了3个不同的选项和它们的影响。我想现在只是一个偏好的问题?(我对Vagrant还很新,所以我还不能谈论最佳实践。)VBoxManage list vms
。 - odigitydo...end
块是空的,实际上不需要它。 config.vm.define "foohost"
就可以正常工作。 - Zook以下是我为各个虚拟机分配名称的方法。将YOURNAMEHERE
更改为您想要的名称。
Vagrantfile的内容:
Vagrant.configure("2") do |config|
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "precise32"
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "http://files.vagrantup.com/precise32.box"
config.vm.define :YOURNAMEHERE do |t|
end
end
$ vagrant status
Current machine states:
YOURNAMEHERE not created (virtualbox)
config.vm.define :app_name
就可以了。 - Vidurconfig.vm.define YOURNAMEHERE do |t| end
- Radu我通过在 VagrantFile 中定义来指定名称,同时指定了主机名,这样我就可以在执行Linux命令时独立于设备的操作系统,并享受看到项目名称的乐趣。✌️
config.vm.define "abc"
config.vm.hostname = "abc"
如果你想要改变除了“default”之外的任何东西,那么只需将这些额外的行添加到你的Vagrantfile中:
config.vm.define "tendo" do |tendo|
end
"tendo"将代替默认名称显示
:OBJECT_VALUE
。 - Andre Leon Rangeldo ... end
部分似乎完全不需要之外,@AndresLeonRangel。 - 0xC0000022L您可以通过修改config.vm.define
的值来更改vagrant默认虚拟机名称。
以下是一个简单的Vagrantfile,它使用getopts并允许您动态更改名称:
# -*- mode: ruby -*-
require 'getoptlong'
opts = GetoptLong.new(
[ '--vm-name', GetoptLong::OPTIONAL_ARGUMENT ],
)
vm_name = ENV['VM_NAME'] || 'default'
begin
opts.each do |opt, arg|
case opt
when '--vm-name'
vm_name = arg
end
end
rescue
end
Vagrant.configure(2) do |config|
config.vm.define vm_name
config.vm.provider "virtualbox" do |vbox, override|
override.vm.box = "ubuntu/wily64"
# ...
end
# ...
end
所以如果想要使用不同的名称,您可以运行以下命令:
vagrant --vm-name=my_name up --no-provision
注意:在执行up
命令之前需要指定--vm-name
参数。
或者:
VM_NAME=my_name vagrant up --no-provision
是的,对于Virtualbox提供程序,可以像这样操作:
Vagrant.configure("2") do |config|
# ...other options...
config.vm.provider "virtualbox" do |p|
p.name = "something-else"
end
end
vagrant destroy
完全拆除了虚拟机,然后重新启动它,但它仍然称之为默认值。 - Kyle Kelley如果有很多人使用您的vagrant文件,您可能希望动态设置名称。以下是一个示例,说明如何使用来自主机机器的用户名作为盒子和主机名的名称:
require 'etc'
vagrant_name = "yourProjectName-" + Etc.getlogin
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.hostname = vagrant_name
config.vm.provider "virtualbox" do |v|
v.name = vagrant_name
end
end