Search This Blog

Saturday 31 December 1988

The 5 things to know before you run Vagrant

Vagrant is a simple and elegant tool for creating and managing virtual machines. Think of it as a wrapper around your virtualization provider like VMware or VirtualBox.

For me it was quite interesting because with such a tool, I could focus on working in the “environment” rather than spend time creating and managing virtual machines. For example, in my IDS lab I have a sensor machine, server and analyst workstation. With Vagrant, I use a configuration file to define these 3 machines, bring them up and manage them as a group. When my work is done, I bring down my IDS setup, destroy the machines (with the relief that Vagrant will allow me to recreate them when necessary) and then maybe bring up my malware analysis lab which would have Windows 7 workstation and REMnux. All this can be managed using Vagrant through just a handful of commands.

That said, I thought I would write an article providing a brief overview of Vagrant. To begin with, here’s a simplified visualization of what Vagrant actually does:

vagrant complete

#1 - The VagrantFile

The VagrantFile is an ASCII based text file where you specify the configuration of the virtual machine you require. If you need 10 machines for your lab then you need to specify them sequentially in the VagrantFile.
For every VM you can specify:

  1. Physical properties of the machine (CPU, RAM etc)
  2. Network Interfaces
  3. Shared Folders (Folders shared between host and the VM)
  4. Hostname/IP Address
  5. Details of software to be installed on the VM – In Vagrant terms this is called provisioning.

Here is a snippet from the VagrantFile, to create a simple Ubuntu virtual machine. The syntax is based on Ruby. But even if you don’t know Ruby at all it is fairly straight to understand and implement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#- This line indicates the beginning of configuration. 
Vagrant.configure(2) do |config|
  
#- Begin configuration for my-ubuntu-machine
config.vm.define "My Ubuntu Machine" do |my-ubuntu-machine|
  
#- Specifying the “box” required for this machine and the URL to get it from. 
 my-ubuntu-machine.vm.box="precise64"
 my-ubuntu-machine.vm.box_url ="http://files.vagrantup.com/precise64.box"
  
#- Setting the hostname, IP and SSH port forwarding 
 my-ubuntu-machine.vm.hostname="ubuntu-machine"
 my-ubuntu-machine.vm.network "private_network", ip: "10.1.1.10"
 my-ubuntu-machine.vm.network "forwarded_port", guest: 22, host: 2210 
  
#- Provisioning or software/config update – I am just running apt-get update to get the latest versions of packages 
 my-ubuntu-machine.vm.provision "shell", inline: -SHELL
 #!/usr/bin/env bash
 apt-get update /dev/null 21
 SHELL
end 
  
#- Add more VMs as required…
  
#- Below line indicates end of configuration.
end

 

#2 – Vagrant Boxes

A box is file that represents a virtual machine. Think of it as an appliance that you can plug in to Vagrant and start running. Boxes are available for download from vagrantbox.es and the Hashicorp site. In addition to downloading boxes you can also create your own boxes and use them.

#3 – Virtualization Provider

This would typically be any software that provides an emulated environment for running different operating systems. Popular options are Vmware Player/Workstation/Fusion, and VirtualBox. Vagrant works free and fine with VirtualBox. However if you want to use it with VMware, you need to purchase additional licenses.

#4 – Vagrant Software

The Vagrant software:

  1. Reads the VagrantFile,
  2. Downloads the boxes (if not already available)
  3. Installs the required applications – For this it takes the help of
  4. Creates VMs in the Virtualization provider
  5. Provisions the machines as required - For simple software installations you can use shell scripts. Where shell scripts get complicated, Vagrant also provides support for Configuration management tools such as Chef, Puppet, Salt and Ansible.
  6. Provides capabilities for managing the VMs using the “vagrant” command.

#5 – Plug-ins

Vagrant provides extensibility with the use of Plug-ins (not shown in above diagram). As a developer you might be interested to add new vagrant commands, provide easier options to configure a host, add support for provisioners etc. In fact, Vagrant itself ships with a set of core plug-ins that work behind the scenes to configure and bring up your VM. You can find the list of core plug-ins here. Plug-ins are developed using Ruby and are implemented as self-contained packages (Rubygems).


To know more about Vagrant refer the Vagrant documentation or the very readable book by Mitchell Hashimoto – Vagrant Up and Running.