当启动一个流浪盒子时,名字“默认”来自哪里?

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...

有办法设置这个吗?


是的,对于Virtualbox提供商做这样的事情:

Vagrant.configure("2") do |config|
    # ...other options...
    config.vm.provider "virtualbox" do |p|
        p.name = "something-else"
    end
end

这是我为单个vm分配名称的方式。把你的名字改为你想要的名字。

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)

我发现这些选项令人困惑,所以我决定测试所有选项,看看它们到底是怎么做的。

我使用的是VirtualBox 4.2.16-r86992和Vagrant 1.3.3。

我创建了一个名为nametest的目录并运行

vagrant init precise64 http://files.vagrantup.com/precise64.box

来生成一个默认的Vagrantfile。然后我打开VirtualBox GUI,这样我就可以看到我创建的盒子会显示为什么名称。

Default Vagrantfile Vagrant.configure('2') do |config| config.vm.box = "precise64" config.vm.box_url = "http://files.vagrantup.com/precise64.box" end VirtualBox GUI Name: "nametest_default_1386347922" Comments: The name defaults to the format DIRECTORY_default_TIMESTAMP. Define VM 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 Name: "nametest_foohost_1386347922" Comments: If you explicitly define a VM, the name used replaces the token 'default'. This is the name vagrant outputs on the console. Simplifying based on zook's (commenter) input Set Provider Name 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 Name: "foohost" Comments: If you set the name attribute in a provider configuration block, that name will become the entire name displayed in the VirtualBox GUI. Combined Example: Define VM -and- Set Provider Name 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 Name: "barhost" Comments: If you use both methods at the same time, the value assigned to name in the provider configuration block wins. Simplifying based on zook's (commenter) input Set hostname (BONUS) Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.hostname = "buzbar" end Comments: This sets the hostname inside the VM. This would be the output of hostname command in the VM and also this is what's visible in the prompt like vagrant@<hostname>, here it will look like 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,所以我还不能谈论最佳实践。)


如果你想改变其他任何东西而不是“默认”,那么只需添加这些额外的行到你的Vagrantfile:

当使用"vagrant status"时,更改basebox的名称

 config.vm.define "tendo" do |tendo|
  end

哪里“tendo”将是名称,将出现而不是默认


您可以通过更改config.vm.define的值来更改流浪机的默认名称。

下面是一个简单的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

注意:——vm-name参数需要在up命令之前指定。

or:

VM_NAME=my_name vagrant up --no-provision

我通过定义VagrantFile内部指定名称,并指定主机名,因此我喜欢在独立于设备的操作系统执行Linux命令时看到我的项目的名称。✌️

config.vm.define "abc"
config.vm.hostname = "abc"

如果有很多人使用你的流浪文件-你可能想要动态设置名称。下面是如何使用用户名从你的主机作为盒子的名称和主机名的例子:

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