Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Vagrant multi-machine cluster using a loop
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
cluster = {
"master" => { :ip => "192.168.33.10", :cpus => 1, :mem => 1024 },
"slave" => { :ip => "192.168.33.11", :cpus => 1, :mem => 1024 }
}
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
cluster.each_with_index do |(hostname, info), index|
config.vm.define hostname do |cfg|
cfg.vm.provider :virtualbox do |vb, override|
config.vm.box = "ubuntu/trusty64"
override.vm.network :private_network, ip: "#{info[:ip]}"
override.vm.hostname = hostname
vb.name = hostname
vb.customize ["modifyvm", :id, "--memory", info[:mem], "--cpus", info[:cpus], "--hwvirtex", "on"]
end # end provider
end # end config
end # end cluster
end
@atrakic

This comment has been minimized.

Copy link

atrakic commented Sep 29, 2016

thx! :)

@sudhakarau1

This comment has been minimized.

Copy link

sudhakarau1 commented Jun 9, 2017

Nice one. Thanks!

@OliPelz

This comment has been minimized.

Copy link

OliPelz commented Oct 25, 2017

really helped me today!

@realei

This comment has been minimized.

Copy link

realei commented Mar 9, 2018

This helped a lot, thanks

@akshaygupta16

This comment has been minimized.

Copy link

akshaygupta16 commented Aug 6, 2018

I need to insert a piece of code after the loop terminates here.
I even tried putting it after the last 'end',
still that external piece of code is getting executed just after the first iteration.
can someone help me with this ?

Code :

-- mode: ruby --

vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

cluster = {
"box5" => { :ip => "192.168.56.25", :cpus => 1, :mem => 512 },
"box6" => { :ip => "192.168.56.26", :cpus => 1, :mem => 512 },
"box7" => { :ip => "192.168.56.27", :cpus => 1, :mem => 512 },
"box8" => { :ip => "192.168.56.28", :cpus => 1, :mem => 512 }
}

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

#cluster.each_with_index do |(hostname, info), index|
for var in cluster
config.vm.define hostname do |cfg|
cfg.vm.provider :virtualbox do |vb, override|
config.vm.box = "mybox.box"
override.vm.network :private_network, ip: "#{info[:ip]}"
override.vm.hostname = hostname
vb.name = hostname
vb.customize ["modifyvm", :id, "--memory", info[:mem], "--cpus", info[:cpus], "--hwvirtex", "on"]
end # end provider
end # end config

end # end cluster

end

Vagrant.configure("2") do |config|
config.vm.provision "shell", path: "script.sh"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.