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"

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}"
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| = "centos/7"
override.vm.synced_folder '.', '/vagrant' , type: "virtualbox" :private_network, ip: "#{info['ip']}"
        override.vm.hostname = "#{info['hostname']}" = "#{info['hostname']}"
override.vm.provision "ansible_local" do |ansible|
ansible.playbook = "playbook.yaml"
      end # end provider
    end # end config

  end # end cluster

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

         - hostname: "webserver"
           ip: ""
         - hostname: "database"
           ip: ""
         - hostname: "appserver"
           ip: ""

step3. place one sample "playbook.yaml" file

- hosts: all
  become: yes
    - name: ensure ntpd is at the latest version
      yum: pkg=ntp state=latest
      - restart ntpd
    - 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?