The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Saturday, June 1, 2013

Vagrantfile for setting up multiple machines with chef configuration

Example Vagrantfile below for setting up multiple virtual machines and using chef to provision them


Vagrant.configure("2") do |config|
# assumes your chef.json files are in cookbooks or site-cookbooks directory
servers = [
{
id: :db1,
ip: "172.16.2.111",
node_json: "master-db.json"
},
{
id: :db2,
ip: "172.16.2.222",
node_json: "standby-db.json"
},
{
id: :web1,
ip: "172.16.2.333",
node_json: "web.json"
}
]
# for testing remove some servers from provision
db_servers.reject! { |item| item[:id] == :db2 }
db_servers.each do |server_settings|
config.vm.define server_settings[:id] do |db|
db.vm.box = "precise32"
db.vm.network :private_network, ip: server_settings[:ip]
db.vm.provision :chef_solo do |chef|
chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"]
# http://jbbarth.com/posts/2011-03-20-vagrant-provisioning-with-chefsolo.html
["./nodes/#{server_settings[:node_json]}"].each do |node_file|
json = JSON.parse(File.read(node_file))
json["run_list"].each { |run| chef.add_recipe(run) }
chef.json.merge!(json)
end
end
end
end
end

Resources

No comments: