{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner
architects,devops,tool,tools & methods

Dynamic Vagrant Nodes

I use vagrant a lot and at Zapier I use a setup where one Vagrantfile can be used to launch any instance in our infrastructure locally for testing. This is really quite useful as I can boot up a two node rabbitmq cluster with haproxy by simply typing vagrant up rabbitproxy01 rabbit01 rabbit02.

In the beginning this was a little messy as I just kept adding new node definitions and incrementing the ip address. But today I’ll share my refactored vagrant file for your own dynamic virtualizing goodness.

# -*- mode: ruby -*-
# vi: set ft=ruby :
domain = 'example.com'
# add your nodes here
nodes = ['puppet', 'rabbit', 'redis', 'http', 'jenkins', 'rabbitproxy']
puppet_nodes = []
nodes.each { |node_name|
  (1..10).each {|n|
    nodenum = "#{n}".rjust(2,'0')
    subnet += 1
    puppet_nodes << {:hostname => "#{node_name}#{nodenum}",  :ip => "172.16.32.#{subnet}", :box => 'precise64'} 
# uncomment the below to see the ips for each host
# puppet_nodes.each { |n| puts "#{n[:hostname]} : #{n[:ip]}" }
Vagrant::Config.run do |config|
  puppet_nodes.each do |node|
    config.vm.define node[:hostname] do |node_config|
      node_config.vm.box = node[:box]
      node_config.vm.box_url = 'http://files.vagrantup.com/precise64.box'
      node_config.vm.host_name = "#{node[:hostname]}.#{domain}"
      node_config.vm.network :hostonly, node[:ip]
      if node[:fwdhost]
        node_config.vm.forward_port(node[:fwdguest], node[:fwdhost])
      memory = node[:ram] ? node[:ram] : 256;
      node_config.vm.customize [
        'modifyvm', :id,
        '--name', node[:hostname],
        '--memory', memory.to_s
      node_config.vm.provision :puppet do |puppet|
          puppet.manifests_path = 'manifests'
          puppet.module_path = 'modules'
          puppet.manifest_file = 'site.pp'

Essentially you just add your node names to nodes and when vagrant runs it will calculate a range of 10 host only ip addresses and node definitions for that node. So if you add “redis” to the list you can boot up redis01, redis02 … redis10.

Caveat: don’t just type vagrant up by itself or you’re going to have a bad time. This will essentially start bootstrapping many many vagrant boxes until your computer screams and falls over. Enjoy!

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks