Reputation: 17401
TL;DR; Can someone help me understand the difference between these "names" defined in a Vagrantfile?
config.vm.define "worker"
box.vm.box = "ots-box"
box.vm.host_name = "worker"
vb.name = "barhost"
While trying to change the hostname of the vagrant instance I had, I was lost in the Vagrantfile syntax. Now I have successfully changed the hostname but some other things (network) isn't working. I suspect I've changed something that has lead vagrant to configure the box is a particular way, which caused this issue.
Vagrant.configure("2") do |config|
config.vm.define "worker".to_sym do |box|
box.vm.box = "ots-box"
box.vm.box_url = "http://testing.xxx.com.s3.amazonaws.com/ots-fe.box"
box.vm.host_name = "worker"
end
I suppose it's irrelevant but I'm trying to create an ubuntu box on Mac El-capitan with Oracle VirtualBox and vagrant version 1.7.4.
Documentation says: config.vm.box - This configures what box the machine will be brought up against. The value here should be the name of an installed box or a shorthand name of a box in HashiCorp's Atlas.
Example here introduces another term vb.name
:
Vagrant.configure('2') do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.define "foohost" do |foohost|
end
config.vm.provider :virtualbox do |vb|
vb.name = "barhost"
end
end
Upvotes: 4
Views: 473
Reputation: 53703
I'll try to answer points by points:
First the following syntax
Vagrant.configure("2") do |config|
config.vm.define "worker".to_sym do |box|
box.vm.box = "ots-box"
box.vm.box_url = "http://testing.xxx.com.s3.amazonaws.com/ots-fe.box"
box.vm.host_name = "worker"
end
is mainly used if you have multiple VMs running (https://docs.vagrantup.com/v2/multi-machine/) so you need to define a name for the VM, in your case worker
so within the next block all the parameters will be set for the worker
VM and to make ruby syntax happy you need to define a variable name (box
)
so if you have a single VM within your Vagrantfile, you do not need this syntax, you can just have
Vagrant.configure("2") do |config|
config.vm.box = "ots-box"
config.vm.box_url = "http://testing.xxx.com.s3.amazonaws.com/ots-fe.box"
config.vm.hostname = "worker"
end
Second, the config/box.vm.box
points to a vagrant box This is a very important concept of Vagrant as each VM you will spin must be created against an existing box. You can download box from internet (vagrant atlas, http://www.vagrantbox.es/) or make the box yourself. You can review the available boxes installed on your machine by running vagrant box list
. If the box set in your Vagrantfile is not available in the available box, vagrant will try to download (so if you point something like ubuntu/trusty64
, even if you did not install it, vagrant will download it from https://atlas.hashicorp.com/ubuntu/boxes/trusty64, assuming it is available for your provider)
Third, the config/box.vm.hostname
will be the variable you're interested (not hostname vs host_name), from the doc
config.vm.hostname - The hostname the machine should have. Defaults to nil. If nil, Vagrant won't manage the hostname. If set to a string, the hostname will be set on boot.
so if you set this variable in your Vagrantfile and boot the machine, the hostname
variable in ubuntu will be resolved to this same value. As an example, from the following Vagrantfile
Vagrant.configure(2) do |config|
...
config.vm.box = "ubuntu/trusty64"
config.vm.hostname = "ubuntu"
...
from the vm, I get
fhenri@machine:~/project/examples/vagrant/ubuntu$ vagrant ssh
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-29-virtual x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Tue Jan 5 10:21:54 2016 from 172.16.42.1
vagrant@ubuntu:~$ hostname
ubuntu
Finally the name topic, config/box.vm.name
will be used for the name of the VM from the provider (in your case VirtualBox), it is quite well explained from the following answer
Upvotes: 4