Article Image
read

#myOCD

Warning: Make sure you apply these additions to a new Vagrant instance or one that isn’t running. (If your instance isn’t new you will need to read the section called “Changing existing Vagrant instances”)

It’s always bugged me that Vagrant insists on calling a single instance default1 no matter what you’ve set the name to in your Vagrantfile configuration of the instance provider:

      # Set the instance name
      config.vm.provider :virtualbox do |v|
        v.name = "VaporApps"
      end

On top of that when you have several independent instances with their own Vagrantfile’s it can be a little daunting when everything you ssh into looks the same2:

    Last login: Sun Jan 13 23:58:20 2019 from 10.0.2.2
    vagrant@ubuntu-xenial:~$ whoami
    vagrant

So, if you’re like me and don’t want to see a listing of all your Vagrant boxes called default… like this:

    dev:~ $ vagrant global-status
    id       name    provider   state    directory                                          
    ----------------------------------------------------------------------------------------
    96e7803  default virtualbox poweroff /Users/dev/Development/Vapor                       
    6f4dbab  default virtualbox poweroff /Users/dev/Development/Clients/XXX/intranet-portal 
    d393ba0  default virtualbox poweroff /Users/dev/Development/Clients/XXX/D365Integration 
    45243f3  default virtualbox running  /Users/dev/Development/VaporWebsites               
    fd57a2f  default virtualbox poweroff /Users/dev/Development/OS Projects/Research        
    8182484  default virtualbox running  /Users/dev/Projects/Ansible                        

and you want to know at a glance which instance you’re currently ssh’d into… may I suggest these simple additions to your Vagrantfile before you vagrant up for the first time.

1. Wrap Your Instance in a define

Vagrant is able to define and control multiple instances in a single Vagrantfile, typically you would use this when developing a system that has a multi-machine setup, like a front-end reverse cache, an app server and backing it all possibly a database or some other data store (like an image store etc) or all of those things.

For my purposes, I’m going to use it to set the instance names for my various setups. The process is relatively simple, first off we can take our previous block where we set the instance name and wrap it in a define block.

NB. It’s important to do this before you vagrant up for the first time. If you have already created your instance and want to make these changes you should read the next section 

# Use `define` to create our instance
config.vm.define "VaporApps" do |instance|
  # Set the instance name
  instance.vm.provider :virtualbox do |vm|
    vm.name = "VaporApps"
  end
end

1.1 Set a hostname

This is good, but we need to do one more thing so that when we ssh into an instance we can clearly see which one we’re in. We can set the hostname, by adding this line to our define block:

# So we know which instance we're in easily in termninal
va.vm.hostname = "VaporApps"

So, the whole define block looks like this:

# Use `define` to create our instance
config.vm.define "VaporApps" do |instance|
  # So we know which instance we're in easily in termninal
  instance.vm.hostname = "VaporApps"

  # Set the instance name
  instance.vm.provider :virtualbox do |vm|
    vm.name = "VaporApps"
  end
end

With these changes in place we can immediately see the differences after issuing a vagrant up command:

dev $ vagrant status
Current machine states:

VaporApps                 running (virtualbox)

dev $ vagrant ssh
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-141-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

Last login: Mon Jan 14 00:14:55 2019 from 10.0.2.2
vagrant@Websites:~$ 

Now at a glance we can see in our instance name and in an ssh session the name of the instance in the prompt, and the vagrant global-status output now shows meaningful names for each of our instances.

Craigs-MacBook-Pro:Research $ vagrant global-status
id       name           provider   state    directory                                             
--------------------------------------------------------------------------------------------------
96e7803  VaporApps virtualbox poweroff /Users/dev/Development/Vapor                                    
6f4dbab  XXXPortal virtualbox poweroff /Users/dev/Development/Clients/XXX/intranet-portal      
d393ba0  XXX365Int virtualbox poweroff /Users/dev/Development/Clients/XXX/D365Integration 
45243f3  Websites virtualbox running /Users/dev/Development/VaporWebsites                                  
fd57a2f  Research virtualbox poweroff /Users/dev/Development/OS Projects/Research                      
8182484  AnsibleTesting virtualbox running /Users/dev/Projects/Ansible                                      

2. Changing existing Vagrant instances

If you’ve already got some Vagrant instances around the place making those

  1. Make sure the instance you want to update isn’t running by issuing the vagrant status to see if its running:
dev $ vagrant status
Current machine states:

default                   running (virtualbox)
  1. and then a vagrant halt to gracefully stop it.
  2. Update your Vagrantfile with the define block and set your hostname as described above.
  3. Run vagrant status again and you should see the status has changed to show the name you specified in the define block, however, now it’s saying that your instance is not created
dev $ vagrant status
Current machine states:

VaporApps                 not created (virtualbox)

The environment has not yet been created. Run 'vagrant up' to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.

Don’t panic! This will be fixed in the next step.

  1. Open the .vagrant/machines directory and you should see another directory called default, rename it to the name you set in the define statement. In my example that means the directory default is renamed VaporApps
  2. Run vagrant status again and this time you should the standard poweroff status for your renamed instance.
dev $ vagrant status
Current machine states:

VaporApps                 poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

Your instance now has a user friendly name… and is ready to vagrant up

Photo by freestocks.org from Pexels


  1. If you’re using a multi-instance Vagrantfile you don’t get this issue. 

  2. I can’t even remember which instance I ssh’d into to copy that terminal output 😞 

Blog Logo

CPPL

The sum of it.


Published

Image

The Sum Of It

A place of for me to write things others may be interested in or find useful.

Back to Overview