Archive for the ‘Ruby’ Category


We can use the facilities of the ruby based Vagranfile DSL to simplify the simulation of a wide topology park.

The typical configuration snippet for a private networked machine follows:


config.vm.define "nginx-1" do |nginx|

nginx.vm.network "forwarded_port", guest: 80, host: 8081
nginx.vm.network "private_network", ip: "192.168.33.10"

end

Once that we’re talking about a actual programming language we could just use a regular iteration logic in order to produce several nodes of same type:


(1..NGINX_INSTANCES).each do |i|
 config.vm.define "nginx-#{i}" do |nginx|

nginx.vm.network "forwarded_port", guest: 80, host: 8080 + i
 nginx.vm.network "private_network", ip: "192.168.33.1#{i}"

nginx.vm.provider "virtualbox" do |vb|
 vb.gui = false
 vb.memory = "256"
 end
 end
 end

Besides the counter generated machine name that we used, it would be necessary to restrict the amount of memory used by our number of VMs hold in NGINX_INSTANCES. So we could limit the amount of memory used by each instance by using  provider specific parameters.

In the same Vagrantfile we can provide another sort of machines the same way:


(1..NODE_INSTANCES).each do |i|
 config.vm.define "node-#{i}" do |nginx|

nginx.vm.network "forwarded_port", guest: 80, host: 8090 + i
 nginx.vm.network "private_network", ip: "192.168.33.2#{i}"

nginx.vm.provider "virtualbox" do |vb|
 vb.gui = false
 vb.memory = "256"
 end
 end
 end

Finally, to perform the provisioning of this machines we can use ansible (as shown here) with the Vagrant DSL to dynamically assemble the inventory groups based on our DSL.


config.vm.provision "ansible" do |ansible|
ansible.playbook = "provisioning/entry-playbook.yml"
ansible.groups = {
"static_web_servers" => ["nginx-[1:#{NGINX_INSTANCES}]"],
"application_servers" => ["node-[1:#{NODE_INSTANCES}]"]
}

ansible.sudo = true
ansible.raw_arguments = ["-vvvv"]

end

 

Full project here.

 

 

 

 

 

 

 

 

Advertisements