Monday, December 26, 2016

Creating multiple VM using Vagrant

This is a sample configuration for vagrant to create custom stack of servers.

Steps1. create file  "Vagrantfile"


#--DDL
require 'yaml'
records = YAML.load_file('config.yaml')
$x = records['server_name'].size
puts "You have asked to install #{$x} of systems "
puts "#{records['server_name']}"
cluster  = {}
(0..($x-1)).each do |i|
cluster["hostname" => "#{records['server_name'][i]['hostname']}" , "ip" => "#{records['server_name'][i]['ip']}"]="hostname-#{i}"
end
Vagrant.configure(2) do |config|

  cluster.each_with_index do |(info, hostname), index|

    config.vm.define hostname do |cfg|
      cfg.vm.provider :virtualbox do |vb, override|
        config.vm.box = "centos/7"
override.vm.synced_folder '.', '/vagrant' , type: "virtualbox"
override.vm.network :private_network, ip: "#{info['ip']}"
        override.vm.hostname = "#{info['hostname']}"
        vb.name = "#{info['hostname']}"
override.vm.provision "ansible_local" do |ansible|
ansible.playbook = "playbook.yaml"
end
      end # end provider
    end # end config

  end # end cluster
end


step 2. Create file config.yaml ( --- also needs to include )


---
 server_name:
         - hostname: "webserver"
           ip: "10.0.15.2"
         - hostname: "database"
           ip: "10.0.15.3"
         - hostname: "appserver"
           ip: "10.0.15.4"




step3. place one sample "playbook.yaml" file


---
- hosts: all
  become: yes
  tasks:
    - name: ensure ntpd is at the latest version
      yum: pkg=ntp state=latest
      notify:
      - restart ntpd
  handlers:
    - name: restart ntpd
      service: name=ntpd state=restarted



step4. run "vagrant up"



Note:  I got some issues while  starting vagrant related to sharing the directory to guest vm. At that time I used following command to resolve it.    "vagrant plugin install vagrant-vbguest "




Please test it and send me if there is any issues. 

1 comment:

Which operating system you like most?