Vagrant for VM Automation

Vagrant for VM Automation

Get you copy of Vagrant and VirtualBox

If you don’t already have Vagrant and VirtualBox, grab those.

Download Vagrant installer for your operating system.

Download VirtualBox installer for your operating system.

Building a Vagrant Box from Start to Finish

Vagrant makes it so simple to create a VM or entire environment that one would never think of using any other tool for automating the server / desktop virtualization.

Using few commands one can quickly build and run a VMs. Below are the very first commands that we use to get started.

$ vagrant init ubuntu/trusty64 … this command will create a new Vagrantfile and place the name ‘ubuntu/trusty64’ into the box statement.

The file would look like as below.

$ vagrant up … this will read thru the Vagrantfile and create a VM using the ‘ubuntu/trusty64’ box. Vagrant will look for the box availability in the folder where from we running the command. If the box file is not available, vagrant will download it from the Hashicorp’s Atlas repository.

We can also download the box and use it to create the VM. This can be done by first downloading the box file from any third party repository like, and then create vagrant box using the command,

$ vagrant box add <boxname> file:///c:\<>

This command will add a vagrant box to the vagrant database of available boxes. We can then run the command,

$ vagrant init <boxname>

Once the VM is up and running, we can login to the VM using the command,

$ vagrant ssh.

This will drop you into the VM.


Depending on the Host operating system on which you will be running Vagrant and VirtualBox, some of the services running on the guest OS (VM) and configurations might vary. For example, if the guest Linux OS id Ubuntu, some of the services and configuration would work and in case of RedHat those would not start by default.

On the host machine from where you are running the vagrant,

You can run below commands to get information about running VMs, modify configuration for existing boxes and more.

$ vagrant box list … this will list all the configured VMs on the host machine.

There are few other sub commands for Vagrant Box command, such as.,

Let’s look at the Vagrantfile

Vagrantfile is the main configuration file for automating VM creation. Vagrant uses pure Ruby syntax. A Vagrantfile can be written to create multiple VMs as well. Here is the sample code, for creating multiple VMs. Anserver, web and db are the variables that have been initialized and used to define VM variables and attributes, like hostname, network, box file name, and memory definition.

Every method started with the ‘do’ statement must end with the ‘end’ statement.


Why Build A Box?

There are many preconfigured / ready-to-use boxes available on sites like and so why would you want to build your own box?

As a member of an operations team if I received a request for provisioning development environment, have to make sure all servers have uniform configuration, I would use a vagrant box and install required applications, services, framework and so on. IF this would be an ongoing request, instead of running the configuration exercise every time, I would prefer to create a box from the existing configured vagrant VM and use the box file for any such VM provisioning request.

Maybe you want your box to have more ram or you need your boxes to more closely mirror production and you are building a ram enriched, multiple server cluster with multiple provisioners.

This can be done using the command,

Now the file can be used to add a vagrantbox to the vagrant database of available boxes.

The ‘newbox’ then can be used in the Vagrantfile to spin up a new VM.

What is a file? When using the VirtualBox provider, it’s a tarred, gzip file containing the following:





The Vagrantfile has some information that will be merged into your Vagrantfile that is created when you run vagrant init boxname in a folder.

The box-disk.vmdk is the virtual hard disk drive.

The box.ovf defines the virtual hardware for the box.

The metadata.json tells vagrant what provider the box works with.

NOTE: These contents would be different for the VMWare provider, etc.


Some of the frequently used Vagrant commands:

$ vagrant –version …. Provides version information.

Vagrant 1.8.5

$ vagrant destroy … a VM can be deleted , including the files for the VM.

$ vagrant halt …. Stops a running VM.

$ vagrant reload — restarts the running VM. Read and apply the Vagrantfile again. This way we can make changes to the Vagrantfile and reapply the config to the existing VM.

$ vagrant status … provides the status of the running VMs

Below is a listing of all available Vagrant commands and a brief description of what they do.


manages boxes: installation, removal, etc.


checks and executes capability


connect to a remotely shared Vagrant environment


stops and deletes all traces of the vagrant machine


attach to an already-running docker container


outputs the logs from the Docker container


run a one-off command in the context of a container


outputs status Vagrant environments for this user


stops the vagrant machine


shows the help for a subcommand


initializes a new Vagrant environment by creating a Vagrantfile


outputs all available Vagrant subcommands, even non-primary ones


log in to HashiCorp’s Atlas


packages a running vagrant environment into a box


manages plugins: install, uninstall, update, etc.


displays information about guest port mappings


connects to machine via powershell remoting


show provider for this environment


provisions the vagrant machine


deploys code in this environment to a configured destination


connects to machine via RDP


restarts vagrant machine, loads new Vagrantfile configuration


resume a suspended vagrant machine


syncs rsync synced folders to remote machine


syncs rsync synced folders automatically when files change


copies data into a box via SCP


share your Vagrant environment with anyone in the world


manages snapshots: saving, restoring, etc.


connects to machine via SSH


outputs OpenSSH valid configuration to connect to the machine


outputs status of the vagrant machine


suspends the machine


starts and provisions the vagrant environment


prints current and latest Vagrant version