A sandbox lab using Vagrant

There are many tools out there to create and generate virtual environments, but for a quick and easy sandbox environment in my opinion one of the best options is Vagrant.

The first thing we need to clarify is that Vagrant is not a tool for production, it is intended for development environments. Vagrant leverages a declarative configuration file (Vagrantfile) that describes all your software requirements, packages, operating system configuration, users, and many more. Additionally Vagrant also integrates with your existing automation and configuration tools like Ansible, Chef, Puppet, or Salt. Finally Vagrant works on Mac, Linux, Windows, and ships out of the box with support for VirtualBox, Hyper-V, and Docker as the main providers.

In a nutshell, Vagrant is a command-line utility for managing the lifecycle of virtual machines that you will startup and destroy in your sandbox. To start let’s review Vagrant simple installations requirements. Vagrant itself is very lightweight so all supported operating systems are capable of running Vagrant, the actual requirements are more related to the provider (hypervisor) of your choice. For a matter of simplicity and for the example in this post, I will use VirtualBox as the provider for Vagrant so the following requirements will apply, but you can choose any of the supported providers.

If you need to install a hypervisor and your choice is VirtualBox, the install is very easy. You have the option to download the latest version from the VirtualBox website but I prefer via APT package manager to get automatic updates with system updates.

$ sudo apt update && sudo apt install virtualbox

Next to install Vagrant on Ubuntu Linux you also have two choices, download the compress zip file from the Vagrant download page, extract and copy it to the /usr/bin folder. The second option is similar to the previous example via the APT package manager.

$ sudo apt install vagrant

All set to start provisioning virtual machines on your sandbox!

First, lest create a working directory for your sandboxes under your home directory.

$ mkdir $HOME/projects/test
$ cd $HOME/projects/test

Now to create your first declarative configuration file just type the following command.

$ vagrant init -m generic/ubuntu2004

The init command initializes a new Vagrant environment by creating a Vagrantfile and the argument specifies the name of the box I want to use for this virtual environment. There are many boxes already configured available to use on Vagrant Cloud that will automatically be downloaded by just specifying their name in the Vagrantfile. Here is the content of the minimal Vagrantfile just created.

 Vagrant.configure("2") do |config|
 config.vm.box = "generic/ubuntu2004"
 end

Now you are ready to launch your first virtual environment under Vagrant. From your test directory just type the following command.

$ vagrant up

The first time it could take between 10-15 minutes to download your selected box, but the deployment of your second environment will just take seconds to provision, configure and launch your virtual machine. Your boxes will be saved under $HOME/.vagrant/boxes and you can manage them using the box command with the available subcommands.

$ vagrant box list

To connect to the virtual machine just use the following command.

$ vagrant ssh

Now inside the virtual machine, you can work on it like any regular machine installing, testing, and removing software. If by any chance you made a mistake just exit from the virtual machine and type the following command to stop and destroy all resources that were created during the machine creation process.

$ vagrant destroy

There are many options available you can explore just by reviewing the list of available commands and their brief description with the following command.

$ vagrant list-commands

Finally, to review all the options available on the Vagranfile you can delete the previous file, run again the ‘init’ command without the ‘-m’ option to generate a Vagrantfile with the most common options included in the file and ‘vagrant up’ again.

$ vagrant init generic/ubuntu2004
$ vagrant up

For more detailed information you can check the Vagrant documentation here.

Next I will post more detail instructions on how to use Vagrant with Ansible to automate the installation and configuration of additional software packages under a sandbox virtual environment.

Leave a comment