Where does the name 'default' come from when launching a vagrant box?
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider...
Is there a way to set this?
In fact, this is quite common. To make this experience better, Vagrant allows specifying the default provider to use by setting the VAGRANT_DEFAULT_PROVIDER environmental variable. Just set VAGRANT_DEFAULT_PROVIDER to the provider you wish to be the default.
You just need to change the config. vm. box directive to reflect the changes.
As mentioned in the docs, boxes are stored at: Mac OS X and Linux: ~/. vagrant. d/boxes.
I found the multiple options confusing, so I decided to test all of them to see exactly what they do.
I'm using VirtualBox 4.2.16-r86992 and Vagrant 1.3.3.
I created a directory called nametest
and ran
vagrant init precise64 http://files.vagrantup.com/precise64.box
to generate a default Vagrantfile. Then I opened the VirtualBox GUI so I could see what names the boxes I create would show up as.
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
So there it is. You now know 3 different options you can set and the effects they have. I guess it's a matter of preference at this point? (I'm new to Vagrant, so I can't speak to best practices yet.)
This is the way I've assigned names to individual VMs. Change YOURNAMEHERE
to your desired name.
Contents of 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 output:
$ vagrant status Current machine states: YOURNAMEHERE not created (virtualbox)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With