如何更改 Vagrant“默认”机器名称?

问题描述 投票:0回答:8

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

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

有办法设置吗?

configuration vagrant virtualbox vagrantfile
8个回答
395
投票

我发现多个选项令人困惑,所以我决定测试所有这些选项,看看它们到底能做什么。

我正在使用 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”

    注释:如果显式定义虚拟机,则使用的名称将替换标记“默认”。这是控制台上输出的名称 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 中显示的完整名称。

    组合示例:定义虚拟机并设置提供商名称

    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
    

    注释: 这设置了虚拟机内的主机名。这将是虚拟机中

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


67
投票

这是我为各个虚拟机分配名称的方式。将

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)

25
投票

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

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

17
投票

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

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

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

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


10
投票

您可以通过更改

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

4
投票

如果有很多人使用您的流浪文件 - 您可能需要动态设置名称。以下是如何使用 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

0
投票

使用变量

NAME
其工作原理如下:

NAME = "jabref-debian-12"

Vagrant.configure("2") do |config|

  config.vm.box = "alvistack/debian-12"

  config.vm.define NAME
  config.vm.hostname = NAME

  config.vm.provider "virtualbox" do |v|
    v.name = NAME
    v.gui = true
    v.customize ["modifyvm", :id, "--memory", "2048", "--cpus", "2"]
  end

  config.vm.provision "shell", inline: <<-SHELL
      sudo apt-get update
      sudo apt-get -y upgrade
  SHELL
end

请注意,我添加了一个最小的片段用于配置以防万一

基于https://stackoverflow.com/a/50603225/873282

© www.soinside.com 2019 - 2024. All rights reserved.