Single VM with multiple `config.vm.hostname` values?

Hi everyone,

I have a VM who’s hostname should be something like app.example.test and api.example.test.

It looks like config.vm.hostname will only allow the definition of a single entry (forbids spaces). Is there any automated way to accomplish this?

I know that I could also go multi-vm here to solve my problem but I’d rather not for various reasons.

Thanks in advance!

Ehm…

Just like domain names on the Internet, hostnames within a network are unique as well. A hostname is a Fully Qualified Domain Name that uniquely and absolutely names a computer and it is used to distinguish between different computers on the same network.

How would you configure a second hostname in a “normal”/ manual way?

Thanks for the response!

$ grep 10.10.10.4 /etc/hosts
10.10.10.4 admin.example.test api.example.test app.example.test

for example. This is useful if you’re running many services on a single VM that would ordinarily be deployed to different infrastructure. Or if you have apache virtualhosts or nginx servers etc. It’s a pretty common use case, actually.

Does that make any more sense?

Sure, but this is not the hostname set by hostname. It’s more like using aliases.

config.vm.hostname is the value you would see using hostname. And that is unique. You could just add additional entries to /etc/hosts using a provisioner.

If set, Vagrant will update /etc/hosts on the guest with the configured hostname.
config.vm - Vagrantfile | Vagrant by HashiCorp

This is why I’m thinking this mechanism could be used.

OK, that’s a point. Now we should wait for the comment of an expert. :grin:

1 Like

Oh and for what it’s worth editing the /etc/hosts file with a provisioner is exactly what I do here. I was just reading the docs and thought that maybe I could do

config.vm.hostname = 'app.example.test api.example.test'

kind of thing only to realize that it fails validation.

Vagrant only accepts one hostname for the config.vm.hostname option. Maybe a more useful way to think of that option is "what should be returned from the guest when I run a hostname"
But, it looks like maybe this vagrant-hostmanager plugin might do what you’re after?
Cheers

2 Likes

Great one. Bookmarked.