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
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
# 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
- Make sure the instance you want to update isn’t running by issuing the
vagrant statusto see if its running:
dev $ vagrant status Current machine states: default running (virtualbox)
- and then a
vagrant haltto gracefully stop it.
- Update your
defineblock and set your hostname as described above.
vagrant statusagain and you should see the status has changed to show the name you specified in the
defineblock, however, now it’s saying that your instance is
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.
- Open the
.vagrant/machinesdirectory and you should see another directory called
default, rename it to the name you set in the
definestatement. In my example that means the directory
vagrant statusagain and this time you should the standard
poweroffstatus 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
Photo by freestocks.org from Pexels