Vagrant

Dec 30, 2017
3 minute read

When something becomes messy in the software industry, we have the habit of tucking the problems away behind abstractions. To me Vagrant is simply an abstraction layer for working with a range of hypervisors (libvirt, VirtualBox, VMWare, Hyper-V, Docker and more), in a reliable and consistent manner.

By articulating VM’s as a simple text based (Ruby) configuration file, a Vagrantfile, achieve greater simplicity, portability across systems, and consistency which could theoretically be used with any virtual environment running on top of any other system.

With Vagrant an environment can now be reliably defined in a text based format, making it easy to back up, modify, share, and manage with revision control. It also means that rather than sharing a whole virtual machine image, which could be many gigabytes, a simple text file can be shared instead.

Workflow

dnf install vagrant

Checkout available boxes on the HashiCorp Vagrant Cloud. I wanted a CentOS box, which is published as centos/7. Make sure the box you go for actually supports the underlying hypervisor technology you plan on running, libvirt in my case. A box is used as an initial image to clone from, the base image itself is never modified.

vagrant box add centos/7 to download the box locally (global to the user).

vagrant init centos/7 to boilerplate a Vagrantfile to get started with. Below is a basic Vagrantfile. Of course can be heavily customised to make the VM’s behave just how you need.

Create the bootstrap.sh script in the same working directory as the Vagrantfile. Vagrant takes care of automatically synchronising this directory on the host, with the guest.

vagrant up

Share the Vagrantfile with the dev team, by commiting it into the git repo.

A couple of highlights with this little Vagrantfile. First, how provider specific customisations can be made, in this case with the libvirt Vagrant provider. Second, the use of a bash script provisioner, which will invoke bootstrap.sh which will install, configure and start the httpd package (the Apache web server).