Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Dynamic Vagrant Nodes

DZone's Guide to

Dynamic Vagrant Nodes

· DevOps Zone ·
Free Resource

Download the blueprint that can take a company of any maturity level all the way up to enterprise-scale continuous delivery using a combination of Automic Release Automation, Automic’s 20+ years of business automation experience, and the proven tools and practices the company is already leveraging.

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 = []
subnet=10
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])
      end
 
      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'
      end
    end
  end
end

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!

Download the ‘Practical Blueprint to Continuous Delivery’ to learn how Automic Release Automation can help you begin or continue your company’s digital transformation.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}