Vagrant: get host operating system name

We have a Vagrant setup running under Ubuntu 12.04 as a guest OS in our team, where the host OS is Windows 7 or 8. What I would like to do is get the Windows hostname of the host computer and add it to the name roaming host during setup, for example

config.vm.hostname = <windows hostname>-web 

This is because we have several developers connecting to external services from their local development machines, and if we all have the same host name (since the Vagrant file is controlled by the source and the same for everyone), then in the magazines for these external we can not distinguish those who made a request for a service. I thought that if we could dynamically obtain the host name from the host OS, this would be a good way to identify the individual guest OSs running on each development machine.

Is this possible, and if so, what is the best way to achieve it?

+7
vagrant virtualbox vagrantfile puppet host
source share
2 answers

On Windows, the COMPUTERNAME environment variable contains the host name. Since Vagrantfile is actually a Ruby script, you can set the host name as follows:

 config.vm.hostname = "#{ENV['COMPUTERNAME']}-web" 

OSX ENV['COMPUTERNAME'] will evaluate to nil so you can use this to set the hostname on any Windows / * nix system:

 config.vm.hostname = "#{ENV['COMPUTERNAME'] || `hostname`[0..-2]}-web" 

Update: I never realized that Windows has a hostname command. At least Windows 7. I don't know how far it goes. Thus, you can simply use the following on all systems:

 config.vm.hostname = "#{`hostname`[0..-2]}-web" 
+10
source share

I used this technique, but I had to change the expression a bit. If the host name returns the fully qualified domain name, as is done on the Mac, the original does not quite work.

The following is a working solution for Mac and Windows.

 config.vm.hostname = "#{`hostname`[0..-2]}".sub(/\..*$/,'')+"-web" 
+3
source share

All Articles