DevOps 101 - Vagrant

Why Vagrant?

Vagrant helps solve some of the following basic needs within the DevOps and I.T. infrastructure management world:

  • Automate an unattended start of virtual machines
  • Creating reliable and consistent virtual machines
  • Creating ephemural virtual machines to provide a sandbox for development and testing purposes
  • Leveraging pre-built virtual machines
  • Starting a virtual machine within a minute
  • Starting a set of virtual machines
  • Defining a network and configuring hostnames and IP addresses
  • Configuring storage, memory, and CPU dynamically
  • Easily switch/use different virtualization providers to start virtual machines

Vagrant additionally creates a level of abstraction by providing a templating engine where JSON files describe your desired state for the virtual machines.

Commands

Below are the general commands you will need to know and will use most often.

  • vagrant init <box> to initialize a Vagrantfile configuration
  • vagrant up starts the machines.
  • vagrant halt stops the machines.
  • vagrant reload stops the machines, reloads configuration of the machines, and then starts them again; very useful when you want to increase CPU or Memory and reload the machines with the new settings.
  • vagrant ssh [name|id] [-- extra_ssh_args] to login using SSH using default user (vagrant)
  • vagrant status show the status of all virtual machines defined in the Vagrantfile.
  • vagrant global-status lists all known vagrant machines from any folder on your current system.
  • vagrant cloud search --provider <provider-name> <expression> to search the vagrant cloud for vagrant boxes using command line instead of their website.

Boxes

Vagrant requires a box or image of an operating system in order to start your virtual machines. You can find great example boxes from the following places, however my recommendation is to create your own boxes using Packer. This lets you control what is inside the box. I find many boxes online have a lot of unnecessary junk installed that we all do not need. I prefer a minimal box that I can build upon.

Packer

You can also create your own Vagrant boxes using the powerful yet simple Packer tool. I highly recommend getting familiar with this tool as it makes it really easy to build a new Vagrant box using multiple virtualization providers and a simple JSON configuration file.

My favorite box creation templates are from boxcutter. Create fresh Vagrant boxes using these Packer templates. BoxCutter has community-driven templates and tools for creating cloud, virtual machines, containers and metal operating system environments

Plugins

Vagrant allows you to install plugins that bring extra functionality and integration to ease your daily life. Here are some of the most useful plugins. This website maintains a bigger list of Vagrant plugins if you wish to explore other ones. However, these are the ones I believe are the best from my experience with Vagrant.

  • vagrant-vbguest to keep your VirtualBox Guest Additions up to date.
  • vagrant-scp to copy files to a Vagrant VM via SCP.
  • vagrant-libvirt to add support for the libvirt provider.
  • vagrant-rhn-satellite that allows registration with a Red Hat Network Satellite server as part of the provisioning process.
  • vagrant-registration that registers your machines for updates on systems with a subscription model (like Red Hat Enterprise Linux, RHEL).
  • vagrant-list that adds a list command to the vagrant command line interface to list known Virtual Machines - the full list extracted from Virtualbox (not just the short list based on your Vagrantfile). Very useful to see the big picture of what is running on your laptop.
  • vagrant-hostmanager that manages hosts files within a multi-machine environment on both the guest and host machines.

Providers

Vagrant has a variety of virtualization providers. You can find the full list on their website but here is more detailed information you might be interested in reading.

Oracle VirtualBox

Parallels

Parallels is a virtualization software that requires a purchased license. The Vagrant Parallels Provider is a plugin for Vagrant, allowing to manage Parallels Desktop virtual machines on macOS hosts.

vagrant plugin install vagrant-parallels

VMware Fusion

VMware Fusion is a virtualization software that requires a purchased license except with the Fusion Player edition that offers the personal license.

brew install --cask vagrant-vmware-utility
vagrant plugin install vagrant-vmware-desktop
vagrant up --provider vmware_desktop

UTM

UTM is an open source virtualization software that is available for Mac. The backend for UTM is QEMU. QEMU supports both virtualization and emulation (running code designed for different system architectures).

Unfortunately at the moment there is no vagrant provider available to allow using vagrant with UTM:

  • https://github.com/utmapp/UTM/issues/3718
  • https://github.com/hashicorp/vagrant/issues/12518
brew install utm

For the open source repository: https://github.com/utmapp/UTM

QEMU

This is a Vagrant plugin that adds a simple QEMU provider to Vagrant, allowing Vagrant to control and provision machines using QEMU.

brew install qemu
vagrant plugin install vagrant-qemu
vagrant init ppggff/centos-7-aarch64-2009-4K
vagrant up --provider qemu

Tips and Tricks

How to debug issues with vagrant?

For debugging the virtual machines, view the logs. Use the --debug option to generate detailed information for any vagrant command. If you do not pipe the logs to a file, you’ll find the logs in the .vagrant/machines/default/<provider>/<machine>/<provider.log> file from the working directory where Vagrantfile is located.

vagrant up --debug &> vagrant.log

Vagrant debug logs include information about your system including environment variables and user information. If you store sensitive information in the environment or in your user account, please scan or scrub the debug log of this information before uploading the contents to the public Internet.

If you plan on submitting a bug report or issue that includes debug-level logs, please use a service like GitHub Gist. Do not paste the raw debug logs into an issue as it makes it very difficult to scroll and parse the information.

Note that .vagrant folder always exists in the current folder where Vagrantfile exists and where you run your vagrant commands. If you have issues with the machines, you can also delete this folder and start fresh.

For debugging issues with vagrant or its plugin, consider deleting the ~/.vagrant.d/ folder. This folder always exists in your user home directoy and contains things like installed plugins and configuration for vagrant.

For more information reference Vagrant Debugging

How to scan for all vagrant machines?

Use the global-status command to list and prune existing vagrant machines that are found in any folders on your system.

# List all vagrant machines
vagrant global-status
# Prune vagrant machines
vagrant global-status --prune

How to enable X-Forwarding from Vagrant box?

If you want to run graphical applications from your Vagrant box and display them on your host machine (instead of running a GUI Vagrant box which uses more resources) then following the instructions here.

How to create a Vagrant Box?

Below are the basic steps to create a new Vagrant Box:

Resources

  • ARM based boxes, Vagrant Base Boxes for ARM Hosts (e.g., Apple Silicon / M1)
  • This project contains a preconfigured Vagrant environment for running various systems in a single command: Vagrant Sandbox
  • A curated list of awesome Vagrant resources, plugins, tutorials and other nice things. Awesome Vagrant
  • Vagrant Listings - plugins, boxes, box configs, etc. Website
  • Good examples of using Vagrant to startup various software.

Operating Systems: