Image for post
Image for post

Vagrant: Dynamic Multi-Machines

Spinning up multiple guests dynamically

Hopefully by now everyone has heard about Vagrant?

This wonderful tool to bring up disposable virtual machines on your system for a variety of operating systems.

Vagrant can provision the systems using a variety of change configuration solutions: shell script, puppet, chef, cfengine, ansible, salt-stack, or use docker.

Vagrant supports a variety of backend providers, can be cloud solutions, like AWS or GCP, or locally with Virtualbox, VMWare, Hyper-V, KVM, Xen, or Docker.

The community supports a variety of operating systems that you can download and try out, and Microsoft provides some downloadable Windows boxes with evaluation licenses of course.

In most cases, a single system is all that is needed for local development, but sometimes you may need to bring a up few systems at the same time, such as a client and server or a cluster of systems, in order to simulate a more realistic environment.

Vagrant does support this, called multi-machine. Vagrant documentation shows how to set this up statically, but what if you wanted to easily do this dynamically?

This article demonstrates how to do this, using a simple hosts files…

First we can create hosts configuration file, and put it in a config/ directory:

We put the key word primary to signal that this is the default system for when we use vagrant ssh without a system name. Otherwise, we would have to use vagrant ssh server.local for example.

As the Vagrant’s configuration, Vagrantfile, is just a ruby script we can do something interesting. We read in the configuration file, and then use this to dynamically spin up the systems:

This script has features I found useful over time:

  • Configure each system with a hostname specified in the config/hosts configuration file.
  • Give the same name with a timestamp for Virtualbox name, so they have a meaningful name if you use the Virtualbox graphical application.
  • Configure an NIC with a IP addressed specified in the config/hosts file. This allows the virtual guest systems to talk to each other, as well as access them from the host system. There’s no need to configure NAT port mapping anymore.
  • Designate one system as the primary default system when you type vagrant ssh, otherwise, use vagrant ssh $system_name that is specified in the config/hosts file.

If you would like to use a provisioning, just added it within the config.vm.define block. For example, if you wanted to download and install ChefDK on every system, you would add this line:

remote_script = ""
node.vm.provision "shell",
inline: "curl #{remote_script} | bash -s -- -P chefdk -c stable -v 2.0.28"

Though I selected one of the Chef’s Bento boxes, you can select from a variety of public domain vagrant boxes, provided that the box was built and configured properly. You can find Vagrant boxes from:

I hope this helps you with your vagrant adventures.

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store