Mark Needham

Thoughts on Software Development

Vagrant: Multi (virtual) machine with Puppet roles

with 5 comments

I’ve been playing around with setting up a neo4j cluster using Vagrant and HAProxy and one thing I wanted to do was define two different roles for the HAProxy and neo4j machines.

When I was working at uSwitch Nathan had solved a similar problem, but with AWS VMs, by defining the role in an environment variable in the VM’s spin up script.

In retrospect I think I might have been able to do that by using the shell provisioner and calling that before the puppet provisioner but Nathan, Gareth Rushgrove and Gregor Russbuelt suggested that using facter might be better.

When I initially looked at the ‘Custom Facts’ section of the docs I thought it was only possible to set facts for the Vagrant file as a whole but you can actually do it on a per VM basis which is neat.

I added a method called ‘provision_as_role’ to the ‘Vagrant::Config::V2::Root’ class:

module Vagrant
  module Config
    module V2
      class Root
        def provision_as_role(role)
          vm.provision :puppet do |puppet|
            puppet.manifests_path = "puppet/manifests"
            puppet.module_path = "puppet/modules"
            puppet.manifest_file  = "base.pp"
            puppet.facter = { "role" => role.to_s }

and then passed in a role depending on the VM in my Vagrantfile:

require File.join(File.dirname(__FILE__), 'lib', 'root.rb')
Vagrant.configure("2") do |config| = "precise64"
  config.vm.box_url = ""
  config.vm.define :neo01 do |neo| 
    neo.vm.hostname = "neo01" :private_network, ip: ""    
    neo.provision_as_role :neo
  config.vm.define :lb01 do |lb|
    lb.vm.hostname = "lb01" :private_network, ip: ""    
    lb.provision_as_role :lb

We can now access the variable ‘$role’ in our puppet code which I used like so:


class all_the_things {
  exec { 'apt-get update': command => '/usr/bin/apt-get update'; }
  package { 'curl': ensure => '7.22.0-3ubuntu4', }
  class { 'apt': }
node default {
  class { 'all_the_things': }
  class { $role:
    require => Class['all_the_things']

The ‘neo’ and ‘lb’ classes look like this:

class neo {
  class { 'java': version => '7u25-0~webupd8~1', }
  class { 'neo4j': require     => Class['java'], }
class lb {
  class { 'haproxy':  }

The full code is on github but it’s behaving a bit weirdly in some scenarios so I’m still trying to get it properly working.

Be Sociable, Share!

Written by Mark Needham

June 30th, 2013 at 1:13 pm

Posted in Software Development

Tagged with

  • Dean Malmgren

    Thanks for sharing! I have a similar setup (albeit not with neo4j) but I’m actually running into a problem getting the VirtualBox machines to communicate. Do your `:private_network` directives make it possible to ping the neo01 server from the lb01 server?

    I’m a little new to this so any tips you might have would be really helpful; thanks!

  • Dean Malmgren

    I stand corrected. Apparently the network on the VMs went out after I put my host machine to sleep (a rather annoying problem with VirtualBox on OSX). Running `/etc/init.d/networking restart` fixed it.

    Anyway, thanks again for the post!

  • @dean_malmgren:disqus I ran into the same problem and got a ‘no route to host’ exception. The problem with the restart networking thing is that it seems to totally screw up the VM probably because the SSH sessions gets torn out from underneath as a result of the networking restart?

    Any ideas how to get around that?

  • Serget

    Super useful even with latest vagrant version! Thanks

  • @Serget glad it still works 🙂

    And no worries!