DevOps

Vagrant

A deterministic, repeatable way of managing virtual machines on cloud and locally

  • #vm
  • #iac
  • #infra

Vagrant is a tool to automate the process of provisioning virtual machines (VMs). It was initially developed by Mitchell Hashimoto, the founder of Hashicorp as a personal side project back in 2010.

Providers

Providers are the virtualization technology that Vagrant can to deploy and configure VM natively.

Here is a list of supported Vagrant providers.

Box

Box are essentially images of the VM that can be deployed. Vagrant have its own repository that contains a wide array of boxes created by officials or the community that can be used for deployment.

Boxes can be discovered/browsed through the Hashicorp Portal.

Some of the example boxes are

  • centos/7
  • generic/gentoo
  • ubuntu/xenial64
  • hashicorp/precise64
  • archlinux/archlinux

Vagrantfile

Vagrantfile is the file that contains all configurations for a VM that Vagrant uses for deployment. The file is written in Ruby syntax.

This is the bare minimum Vagrantfile that specifies centos/7 to be used as the image for creating the VM.

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end

Using vagrant init centos/7 will scaffold a heavily commented Vagrantfile for CentOS that it helpful for beginners to go through. Here are more examples for how a Vagrantfile can be configured.

To validate a Vagrantfile, run the validate command.

vagrant validate

Provisioner

Provisioner is the medium that sets up the VM after they have been created. It wouldn't be that useful to create an empty VM without the necessary tools and software installed.

  • Ansible
  • Puppet
  • Chef
  • Salt
  • Raw scripts (e.g Bash)

Shell Scripts

In the shell provisioning block, we can provide inline shell commands that will be executed upon creation/edit.

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "shell", inline: <<-SHELL
    echo hello from shell!
    sudo yum update -y
    sudo yum install -y httpd
  SHELL
end

Alternatively, we can have a separate shell script that can be referenced in the Vagrantfile to achieve the same thing.

provision.sh
#!/bin/bash

echo hello from shell!
sudo yum update -y
sudo yum install -y httpd

The provision.sh script can be referenced as such:

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provision "shell", path: "provision.sh"
end

More details about using shell as the provisioner can be found on the official documentation.

Next Steps

After having the Vagrantfile generated and configured, run vagrant up to start the VM. For first time execution, it will download the box specified (image) from the official repository first before creating and running the VM.

When the VM have been created and running, run vagrant ssh to ssh into the VM. To quit the VM from the SSH, type exit in the command line. To stop the VM, run vagrant halt to gracefully shutdown the VM.

Basic Commands

This section shows a few common commands of the Vagrant CLI.

Vagrant Commands

CommandsDescriptionExample
vagrant initCreates a new Vagrantfilevagrant init centos/7
vagrant upDownloads and spinup the machines
vagrant sshSSH into the guest machine
vagrant ssh-configOutputs OpenSSH valid configuration to connect to the VMs via SSH
vagrant haltStops the VMs
vagrant suspendSuspends the guest machines
vagrant resumeResumes the suspended machines
vagrant reloadRestarts the VMs
vagrant destroyStop and removes all the VMs
vagrant statusShows the status of the current Vagrant environment
vagrant packagePackages a running virtual environment into a reusable box
vagrant provisionRuns any configured provisioners against the running VM
vagrant plugin installInstalls a vagrant pluginvagrant plugin install vagrant-vbguest
vagrant plugin listLists all installed plugins
vagrant plugin uninstallUninstalls a pluginvagrant plugin uninstall vagrant-vbguest

Vagrant Box Commands

CommandsDescriptionExample
vagrant box addAdd a box to local box repositoryvagrant box add centos/7
vagrant box listLists all boxes in your local box repository
vagrant box outdatedChecks if any boxes in your local box repository are outdated
vagrant box updateUpdates a box to a new versionvagrant box update centos/7
vagrant box repackageRepackages a box with a new name and metadatavagrant box repackage centos/7 --name custom-centos
vagrant box pruneRemoves outdated boxes from your local box repository
vagrant box removeRemoves a box from your local box repositoryvagrant box remove centos/7

VirtualBox as Provider

When using VirtualBox as a provider for Vagrant, this are some useful commands for inspecting the VMs.

  • List all the VMs
vboxmanage list vms
  • List all the running VMs
vboxmanage list runningvms

References

Pal, R. Vagrant Beginner Tutorial.https://www.youtube.com/playlist?list=PLhW3qG5bs-L9S272lwi9encQOL9nMOnRa
Hashicorp. Introduction to Vagrant. Retrieved 2024, November 14 from https://developer.hashicorp.com/vagrant/intro
Vagrant (software).ย Wikipedia. Retrieved 2024, November 14 from https://en.wikipedia.org/wiki/Vagrant_(software)
Hashicorp. Shell Provisioner. Retrieved 2024, November 14 from https://developer.hashicorp.com/vagrant/docs/provisioning/shell