Multi-machine Vagrant Environments

Posted on ‐ Tagged vagrant

Vagrant is a really powerful tool. I use it frequently for testing various things related to my work at PayPerks. One of the most powerful features is multi-machine environments.

Multi-machine environments allow you to recreate any piece of infratstructure with little effort, while using existing provisioning code from your software-defined infrastructure. For example, the Vagrantfile below creates a single application server, two database servers, and a bastion host:

# -*- mode: ruby -*-
# vi: set ft=ruby :
# Multi-machine Vagrantfile inspired by

Vagrant.configure("2") do |config|

  boxes = [
    { :name => :app01,      :ip => "", :http_fwd => 8880 , :https_fwd => 4443 },
    { :name => :db01,       :ip => "" },
    { :name => :db02,       :ip => "" },
    { :name => :bastion01,  :ip => "" },

  vm_default = proc do |cnf| = "precise64"
    cnf.vm.box_url = ""

  puppet_default = proc do |puppet|
    puppet.manifest_file  = "site.pp"
    puppet.manifests_path = "manifests"
    puppet.module_path    = "modules"
    puppet.options        = "--verbose --debug"

  boxes.each do |opts|
    config.vm.define opts[:name] do |config|

      config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", "768"]
        vb.gui = true if opts[:gui]

      config.vm.host_name = "" % opts[:name].to_s   "forwarded_port", guest: 80, host: opts[:http_fwd] if opts[:http_fwd]   "forwarded_port", guest: 443, host: opts[:https_fwd] if opts[:https_fwd]   "private_network", ip: opts[:ip]
      config.vm.provision :shell, path: ""

      config.vm.provision :puppet do |puppet|



The real magic happens here because of Vagrant’s tight integration with various provisioners. With a simple “vagrant up”, I can start the process of creating the instances and let the provisioner take over to configure them. In this case, I’m able to utliize the same puppet manifests that I’m using in production to provision this local environment. The result: a copy of this particular piece of infrastructure on my local machine. I Vagrant :)

comments powered by Disqus