如何更改stream浪者'默认'的机器名称?
在启动一个stream浪箱时,“默认”这个名字来自哪里?
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... 有没有办法设置这个?
我发现多个选项令人困惑,所以我决定testing所有这些选项,看看它们究竟做了什么。
我使用的是VirtualBox 4.2.16-r86992和Vagrant 1.3.3。
 我创build了一个名为nametest的目录并运行 
 vagrant init precise64 http://files.vagrantup.com/precise64.box 
生成一个默认的Vagrantfile。 然后我打开VirtualBox GUI,看看我创build的盒子名称会显示为什么。
- 
默认的Vagrantfile Vagrant.configure('2') do |config| config.vm.box = "precise64" config.vm.box_url = "http://files.vagrantup.com/precise64.box" endVirtualBox GUI名称: “nametest_default_1386347922” 注释:名称默认为DIRECTORY_default_TIMESTAMP格式。 
- 
定义VM Vagrant.configure('2') do |config| config.vm.box = "precise64" config.vm.box_url = "http://files.vagrantup.com/precise64.box" config.vm.define "foohost" endVirtualBox GUI名称: “nametest_foohost_1386347922” 注释:如果您明确定义了一个VM,则使用的名称会replace标记“default”。 这是控制台上的vagrant输出。 根据 zook的(评论者)input简化
- 
设置提供者名称 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 endVirtualBox GUI名称: “foohost” 注释:如果您在提供程序configuration块中设置了 name属性,则该名称将成为VirtualBox GUI中显示的完整名称。组合示例:定义VM – 并且设置提供者名称 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 endVirtualBox GUI名称: “barhost” 注释:如果您同时使用这两种方法,则分配给提供程序configuration块中的 name的值将胜出。 根据zook的(评论者)input简化
- 
设置 hostname(BONUS)Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.hostname = "buzbar" end评论:这设置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的新手,所以我不能说最佳做法。)
 这是我为每个虚拟机分配名称的方式。 将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 
terminal输出:
 $ vagrant status Current machine states: YOURNAMEHERE not created (virtualbox) 
如果你想改变其他的东西而不是默认的,那么把这些额外的行添加到你的Vagrantfile中:
改变基地名称,当使用“stream浪状态”
  config.vm.define "tendo" do |tendo| end 
在哪里“tendo”将会出现,而不是默认的名称
是的,对于Virtualbox提供商,可以这样做:
 Vagrant.configure("2") do |config| # ...other options... config.vm.provider "virtualbox" do |p| p.name = "something-else" end end 
 您可以通过更改config.vm.define值来更改vagrant默认机器名称。 
这里是使用getopts的简单Vagrantfile,并允许您dynamic更改名称:
 # -*- 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命令之前up需要指定--vm-name参数。 
要么:
 VM_NAME=my_name vagrant up --no-provision