Vagrantfile: In a more ruby way to customize RAM and CPU core.

Hello Everyone,

    Here is my new Vagrantfile which coded in more ruby way. This code helps you with customize RAM, CPU and OS based provision script.

Most of the code is self explanatory if you have basic understanding of Vagrant.



# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.

$hostnames = <<-script echo="" master.mypuppetsetup.com="" puppet="" puppetmaster="">> /etc/hosts
echo "192.168.66.20 k8sworker1 k8sworker1.mypuppetsetup.com" >> /etc/hosts
echo "192.168.66.30 k8sworker2 k8sworker2.mypuppetsetup.com" >> /etc/hosts
SCRIPT

$centos_setup = <<-script -y="" :hostname="" apt-get="" epel-release="" install="" script="" servers="[" ubuntu_setup="<<-SCRIPT" update="" yum=""> "puppetmaster",
    :ip => "192.168.66.10",
    :box => "centos/7",
    :ram => 4096,
    :cpu => 2
  },
  {
    :hostname => "k8sworker1",
    :ip => "192.168.66.20",
    :box => "centos/7",
    :ram => 2048,
    :cpu => 1
  },
   {
    :hostname => "k8sworker2",
    :ip => "192.168.66.30",
    :box => "ubuntu/trusty64",
    :ram => 2048,
    :cpu => 1
  }
]
Vagrant.configure("2") do |config|

  servers.each do |machine|
    config.vm.define machine[:hostname] do |node|
      node.vm.box = machine[:box]
      node.vm.hostname = machine[:hostname]
      node.vm.network "private_network", ip:machine[:ip]
      node.vm.provision "shell", inline: $hostnames
      node.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", machine[:ram]]
        vb.customize ["modifyvm", :id, "--cpus", machine[:cpu]]
      end
    node.vm.provision "shell" ,inline: $hostnames
    
    if machine[:box] == "centos/7"
      node.vm.provision "shell" ,inline: $centos_setup
    end

    if machine[:box] == "ubuntu/trusty64"
      node.vm.provision "shell" ,inline: $ubuntu_setup
    end

    end
  end
end
 
 
Hope it helps
Thank you.

0 comments:

Post a Comment