vagrantvagrantfile

Vagrant file: host name repeated?


I have following in vagrant config file:

config.vm.define "db01" do |db01|
    db01.vm.box = "geerlingguy/centos7"
    db01.vm.hostname = "db01"
    db01.vm.network "private_network", ip: "192.168.56.15"
    db01.vm.provision "shell", path: "mysql.sh"

What's the purpose of having "db01" 2 times, once for config.vm.define and once for db01.vm.hostname?


Solution

  • The config.vm.define is the name of the virtual machine, while the vm.hostname the hostname that will be assigned to the virtual machine. That hostname is the name that other machines on the network will use to refer to this particular machine while vm name is kinda label and is local. So you do not have them set twice really - you are setting two different things. The fact you actually used the same looking string value for both is a ability/feature. You set it that way just because you can and that's most likely convenient. Yet still, these are not the same things and not everyone follow the same approach. They set them to be completely different to match their needs and still everyone can be happy thanks to the fact both things are not merged in any way.