How to change Vagrant 'default' machine name?

前端 未结 7 579
有刺的猬
有刺的猬 2020-11-28 00:29

Where does the name \'default\' come from when launching a vagrant box?

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


        
相关标签:
7条回答
  • 2020-11-28 01:11

    You can change vagrant default machine name by changing value of config.vm.define.

    Here is the simple Vagrantfile which uses getopts and allows you to change the name dynamically:

    # -*- 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
    

    So to use different name, you can run for example:

    vagrant --vm-name=my_name up --no-provision
    

    Note: The --vm-name parameter needs to be specified before up command.

    or:

    VM_NAME=my_name vagrant up --no-provision
    
    0 讨论(0)
提交回复
热议问题