Vagrant: Virtual machine provisioning made easy

During Devops Days Paris 2013 earlier this year (18/19 April 2013) I was surprised to discover that very few people had heard of Vagrant. Luckily; there was an open space dedicated to Vagrant (and its usage) where 15-20 people attended but only 5 among them knew about Vagrant (including my colleague, François Armand and myself). This tools deserves to be better known, so the aim of this post is to introduce you to Vagrant and share one of several ways we regularly use Vagrant at Normation.

We began using this tool extensively during July 2012. At that time Rudder 2.4.0~beta3 was about to be released. We needed to test installation as much as possible, thus we needed to start from a fresh, clean environment for each test. This required the creation of several VMs (a server and some nodes) followed by installing and configuring Rudder (server or agent) on each of them.. Doing this manually took a lot of time, was laborious and could be prone to error. All the time spent on installation was wasted every time we destroyed those VMs after a test. Now all of this is automated with Vagrant and we no longer lose time configuring virtual machines.

What is Vagrant?

Vagrant is an open source virtual environment building tool created in January 2010 by Mitchell Hashimoto, and developed on his spare time. As Vagrant became more and lore popular worldwide, in November 2012 Mitchell founded Hashicorp, a company dedicated to Vagrant. With 3 years of development, the huge community and the support of Hashicorp, Vagrant has become a proven and reliable software.

Vagrant’s approach is very simple:

  • Create the VM, based on a base box a VM provider.
  • Customize  it, still using the VM provider
  • Configure it, on startup, using a provisioning tool..
  • Control it directly with vagrant commands, or ssh in (vagrant ssh)

Once you have configured your template (Vagrantfile) you can then re-use it and thus automate the entire installation process!

Vagrant uses a single configuration file: the Vagrantfile. In this file you describe how your virtual machines will be created and configured. Everything you need to know will be centralized in that file. How to configure a Vagrantfile is clearly documented on the website http://docs.vagrantup.com/v2/.

Vagrant will use three things to create the VM:

  • A base box: These are VMs prepared for vagrant usage (vagrant user, access defined). You can create your own boxes however many are already available at vagrantbox.es ,
  • A VM provider: It will create the VM using the base box and customize it. Numerous providers are supported (Virtualbox, AWS, …),
  • A provisioning tool: It will be run at the VM boot, it can run a shell script or use a configuration management tool (CFEngine, Chef, Puppet, …).Vagrant

So there are a huge variety of possibilities and it can be adapted to all of your needs and abilities.

Vagrant is very simple to install and use, you just need to choose your provider (virtualbox is the easiest one, other providers are also available).

Creating your first VM is very easy,  just run the following in a dedicated directory:

$ vagrant box add base http://files.vagrantup.com/lucid32.box
$ vagrant init
$ vagrant up

Vagrant offers you several commands to manage your environment :

  • init: initiate a vagrant environment with a bare Vagrant file
  • up: start  VM(s)
  • ssh: ssh into the VM passed as parameter
  • status: show status of all VMs
  • destroy: destroy VM(s)
  • halt: stop VM(s)
  • box: manage base boxes

… and a lot more!

So, to use vagrant the only things you need to know are how to declare a VM (very easy and straight forward with the Vagrantfile), and how to configure it – depending on your needs!

Vagrant gives you automation and I do think that automation is one of the best things ever invented. Automation helps you get back the time spent doing things manually so that you can make even better use of your time on other tasks. With Vagrant you do the preparation only once, then (almost, if we take into account Murphy’s Law) everything works as expected. At Normation, we use it in three areas: our development environment, testing environment and embedded demo. I’ll explain in details how we manage to do this with vagrant, and all advantages we gain by using it in a follow-up blog post.

Vagrant is not the only tool for virtual environment automation and provisioning, there is Veewee (creation of base boxes, developped with love by Patrick Debois) and Cobbler (my colleague, Mathieu Cerda, gave a talk about Cobbler at LOADays 2013, here is a link to his slides). They are very good and I encourage you to take a look at them! Easy automation is now a reality, it would be a shame not to take advantage of this!