{"id":1683,"date":"2018-04-05T22:42:20","date_gmt":"2018-04-05T20:42:20","guid":{"rendered":"http:\/\/www.ludovicocaldara.net\/dba\/?p=1683"},"modified":"2020-08-18T16:10:46","modified_gmt":"2020-08-18T14:10:46","slug":"multi-vm-groups-vagrantfile","status":"publish","type":"post","link":"https:\/\/www.ludovicocaldara.net\/dba\/multi-vm-groups-vagrantfile\/","title":{"rendered":"Basic Vagrantfile for multiple groups of VMs"},"content":{"rendered":"<p>In case you want to prepare multiple sets of machines quickly using <a href=\"https:\/\/www.vagrantup.com\/intro\/index.html\">Vagrant<\/a>, ready for different setups, this might be something for you:<\/p>\n<pre class=\"lang:ruby decode:true \">## -*- mode: ruby -*-\r\n## vi: set ft=ruby :\r\n\r\nrequire 'ipaddr'\r\n\r\n###############################\r\n# CUSTOM CONFIGURATION START\r\n###############################\r\n\r\n# lab_name is the name of the lab where all the files will be organized.\r\nlab_name = \"lab_bigdata\"\r\n\r\n# here is where you download your software, so it will be available to the VMs.\r\nsw_path  = \"C:\\\\Users\\\\ludov\\\\Downloads\\\\Software\"\r\n\r\n# cluster(s) definition\r\nclusters = [\r\n  {\r\n  :prefix  =&gt; \"hadoop\", \t\t\t\t# prefix: VMs will be named prefix01, prefix02, etc\r\n  :domain  =&gt; \"ludovicocaldara.net\",\t# domain name\r\n  :box     =&gt; \"ludodba\/ol7.3-base\",\t\t# base box, either \"ludodba\/ol7.3-base\" or \"ludodba\/ubu1604\"\r\n  :nodes   =&gt; 3,\t\t\t\t\t\t# number of nodes for this cluster\r\n  :cpu     =&gt; 1,\r\n  :mem     =&gt; 2048,\r\n  :publan  =&gt; IPAddr.new(\"192.168.56.0\/24\"), \t# public lan for the cluster\r\n  :publan_start =&gt; 121\t\t\t\t\t\t\t# starting IP, each VM will increment it by one\r\n  },\r\n  {\r\n  :prefix  =&gt; \"kafka\",\t\t\t\t\t\t\t# eventually, continue with another cluster!\r\n  :domain  =&gt; \"ludovicocaldara.net\",\r\n  :box     =&gt; \"ludodba\/ol7.3-base\",\r\n  :nodes   =&gt; 1,\r\n  :cpu     =&gt; 1,\r\n  :mem     =&gt; 2048,\r\n  :publan  =&gt; IPAddr.new(\"192.168.56.0\/24\"),\r\n  :publan_start =&gt; 131\r\n  },\r\n  {\r\n  :prefix  =&gt; \"postgres\",\r\n  :domain  =&gt; \"ludovicocaldara.net\",\r\n  :box     =&gt; \"ludodba\/ubu1604\",\r\n  :nodes   =&gt; 1,\r\n  :cpu     =&gt; 1,\r\n  :mem     =&gt; 2048,\r\n  :publan  =&gt; IPAddr.new(\"192.168.56.0\/24\"),\r\n  :publan_start =&gt; 141\r\n  }\r\n]\r\n\r\n###############################\r\n# CUSTOM CONFIGURATION END\r\n###############################\r\n\r\n######################################################\r\n# Extending Class IPAddr to add the CIDR to the lan\r\nclass IPAddr\r\n  def to_cidr_s\r\n    if @addr\r\n      mask = @mask_addr.to_s(2).count('1')\r\n      \"#{to_s}\/#{mask}\"\r\n    else\r\n      nil\r\n    end\r\n  end\r\nend # extend class IPAddr\r\n\r\n########\r\n# MAIN #\r\n########\r\n\r\nVagrant.configure(2) do |config|\r\n  config.ssh.username = \"root\"  \t# my boxes are password based for simplicity\r\n  config.ssh.password = \"vagrant\"\r\n  config.vm.graceful_halt_timeout = 360\t# in case you install grid infra... do not force shutdown after a few seconds\r\n\r\n  if File.directory?(sw_path)\r\n    # our shared folder for oracle 12c installation files (uid 54320 is grid, uid 54321 is oracle)\r\n    config.vm.synced_folder sw_path, \"\/media\/sw\", :mount_options =&gt; [\"dmode=775\",\"fmode=775\",\"uid=54322\",\"gid=54328\"]\r\n  end\r\n\r\n  # looping through each cluster\r\n  (0..(clusters.length-1)).each do |cluid|\r\n\r\n    # assign variable clu to current cluster, for convenience\r\n    clu = clusters[cluid]\r\n      \r\n    # looping through each node in the cluster\r\n    (1..(clu[:nodes])).each do |nid|\r\n\r\n      # let's start from the last node (see RAC Attack automation for the reason) :-)\r\n      nid = clu[:nodes]+1-nid\r\n      config.vm.define vm_name = \"#{clu[:prefix]}%02d\" % nid do |cnf|\r\n\t  \r\n\t\t# set the right box for the VM\r\n\t\tcnf.vm.box = clu[:box]\r\n\t\tif (clu[:box_version]) then\r\n\t\t\tcnf.vm.box_version = clu[:box_version]\r\n\t\tend #if\r\n\t\t\r\n\t\t# the new vm name\r\n        vm_name = \"#{clu[:prefix]}%02d\" % nid\r\n        fqdn = \"#{vm_name}.#{clu[:domain]}\"\r\n        cnf.vm.hostname = \"#{fqdn}\"\r\n\r\n\t\t# incrementing public ip for the cluster\r\n        pubip = clu[:publan].|(clu[:publan_start]+nid-1).to_s\r\n\r\n        cnf.vm.provider :virtualbox do |vb|\r\n          #vb.linked_clone = true  # in case you want thin provisioning. read the vagrant doc before setting it\r\n          vb.name = vm_name\r\n          vb.gui = false\r\n          vb.customize [\"modifyvm\", :id, \"--memory\", clu[:mem]]\r\n          vb.customize [\"modifyvm\", :id, \"--cpus\",   clu[:cpu]]\r\n          vb.customize [\"modifyvm\", :id, \"--groups\", \"\/#{lab_name}\/#{clu[:prefix]}\"]\r\n        end #config.vm.provider\r\n\t\t\r\n        # Configuring virtualbox network for #{pubip}\r\n        cnf.vm.network :private_network, ip: pubip\r\n\r\n      end #config.vm.define\r\n    end #loop nodes\r\n  end  #loop clusters\r\nend #Vagrant.configure<\/pre>\n<p>The nice thing, (beside speeding up the creation and basic configuration) is the organization of the directories. The configuration at the beginning of the script will result in 5 virtual machines:<\/p>\n<pre class=\"lang:plsql highlight:0 decode:true \">your VM directory\r\n        |- lab_bigdata \r\n                |- hadoop\r\n                        |- hadoop01  (ol7)\r\n                        |- hadoop02  (ol7)\r\n                        |- hadoop03  (ol7)\r\n                |- kafka\r\n                        |- kafka01   (ol7)\r\n                |- postgres\r\n                        |- postgres01  (ubuntu 16.04)\r\n\r\n<\/pre>\n<p>It is based, in part (but modified and simplified a lot), from\u00a0 the RAC Attack automation scripts by Alvaro Miranda.<\/p>\n<p>I have a more complex version that automates all the\u00a0tasks for a full multi-cluster RAC environment, but if this is your requirement, I would rather check oravirt scripts on github (<a href=\"https:\/\/github.com\/oravirt\">https:\/\/github.com\/oravirt<\/a>) . They are much more powerful and complete (and complex&#8230;) than my Vagrantfile. \ud83d\ude42<\/p>\n<p>Cheers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In case you want to prepare multiple sets of machines quickly using Vagrant, ready for different setups, this might be something for you: ## -*- mode: ruby -*- ## vi: set ft=ruby : require &#8216;ipaddr&#8217; ############################### # CUSTOM CONFIGURATION START &hellip; <a href=\"https:\/\/www.ludovicocaldara.net\/dba\/multi-vm-groups-vagrantfile\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[336,5,132],"tags":[],"class_list":["post-1683","post","type-post","status-publish","format-standard","hentry","category-devops","category-linux","category-triblog"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/1683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/comments?post=1683"}],"version-history":[{"count":5,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/1683\/revisions"}],"predecessor-version":[{"id":1689,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/1683\/revisions\/1689"}],"wp:attachment":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/media?parent=1683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/categories?post=1683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/tags?post=1683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}