【问题标题】:How to change Vagrant 'default' machine name?如何更改 Vagrant 的“默认”机器名称?
【发布时间】:2013-07-24 14:45:57
【问题描述】:

启动 vagrant box 时,名称“default”从何而来?

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

有办法设置吗?

【问题讨论】:

  • 不要忘记检查虚拟化是否处于活动状态,在您的 BIOS 中启用 VT-x。那是我的问题

标签: configuration vagrant virtualbox vagrantfile


【解决方案1】:

我发现多个选项令人困惑,因此我决定测试所有选项以了解它们的确切作用。

我正在使用 VirtualBox 4.2.16-r86992 和 Vagrant 1.3.3。

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

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

生成一个默认的 Vagrantfile。然后我打开了 VirtualBox GUI,这样我就可以看到我创建的盒子的名称。

  1. 默认的 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 格式。

  2. 定义虚拟机

    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"

    评论:如果您明确定义 VM,则使用的名称将替换令牌“默认”。这是控制台上输出的名称 vagrant。根据zook(评论者)的输入进行简化

  3. 设置提供者名称

    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 中显示的整个名称。

    组合示例: 定义 VM -and- 设置提供程序名称

    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(评论者)的输入进行简化

  4. 设置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 的新手,所以我还不能谈论最佳实践。)

【讨论】:

  • 嘿,我对相关问题进行了更多研究。 (1) 名称中的目录是第一次生成,重命名目录时不会改变。 (2) 名称中的时间戳是第一次生成,重启后不会改变。 (3) 项目通过UUID映射到VirtualBox VM,保存在PROJECT_ROOT/.vagrant/machines/default/virtualbox/id。此 UUID 在 VirtualBox GUI 中不可见 (virtualbox.org/ticket/6989),但您可以在命令行中使用 VBoxManage list vms。
  • 我在上一条评论中使用的有用参考资料之一:stackoverflow.com/questions/9434313/…
  • 值得注意的是,define VM 方法名用于 Vagrant 标准输出和日志,而set provider name 名用于管理与提供者的盒子。所以两者都是相关的。
  • 如果do... end 块为空,您实际上并不需要它。 config.vm.define "foohost" 工作得很好。
  • 我喜欢这个答案的细节,但它并没有真正解决原始问题。是的,这些配置将导致 Virtualbox GUI 中正确显示“名称”,如果您使用 vboxmanage 列出它们,也是如此。但是,当您执行 vagrant up 时,这仍然会保留 Bringing machine 'default' up with 'virtualbox' provider...
【解决方案2】:

这是我为各个虚拟机分配名称的方式。将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 也可以。
  • 如果 YOURNAMEHERE 是一个变量,那么应该删除冒号“:”:config.vm.define YOURNAMEHERE do |t| end
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    如果您想更改任何其他内容而不是“默认”,那么只需将这些附加行添加到您的 Vagrantfile:

    使用“vagrant status”时更改basebox名称

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

    其中“tendo”将是出现的名称而不是默认名称

    【讨论】:

    • 我认为你的答案更精致,很明显你设置的是一个字符串,与其他使用`:OBJECT_VALUE`的答案相反
    • @AndresLeonRangel 除了do ... end 部分似乎根本不需要?
    【解决方案5】:

    您可以通过更改 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
    

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

    或:

    VM_NAME=my_name vagrant up --no-provision
    

    【讨论】:

      【解决方案6】:

      是的,对于 Virtualbox 提供商,请执行以下操作:

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

      【讨论】:

      • 在更改我的 Vagrantfile 并通过 vagrant destroy 完全拆除机器然后将其重新启动后,它仍将其称为默认值。
      • 我相信这会改变 VirtualBox GUI 中的名称。 “您可以自定义出现在 VirtualBox GUI 中的名称”参考:link
      • 啊,是我的错——我以为作者要更改VirtualBox名称,内部Vagrant名称当然只能在多VM环境中更改。
      • cmur2 不是太挑剔的意思,但可以通过只提供一个 config.vm.define 块在单机环境中设置内部 Vagrant 名称。
      【解决方案7】:

      如果有很多人使用您的 vagrant 文件 - 您可能需要动态设置名称。下面是如何使用 HOST 机器 中的用户名作为盒子名称和主机名的示例:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-18
        • 2020-11-06
        • 2020-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-01
        • 1970-01-01
        相关资源
        最近更新 更多