Sunday, July 22, 2018

Useful Vagrant Files



Most of the examples below adds a Host-Only NIC so the VMs get an IP from
192.168.50.X range. This makes it convenient when running ansible playbooks or
roles against them because you no longer need to configure port forwarding in
the inventory file.

Basic single node
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
  hostname = "elk"
  config.vm.provision :shell, path: "post-install.sh"
  config.vm.define "#{hostname}" do |machine|
    machine.vm.network "private_network", ip: "192.168.50.101"
    machine.vm.box = "centos/7"
    machine.vm.hostname = "#{hostname}"
    machine.vm.provider "virtualbox" do |vb|
      vb.name = "#{hostname}"
    end   
  end
end
Single node w/ ansible provisioner
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
  # custom settings
  hostname = "elk"
  ENV['ANSIBLE_ROLES_PATH'] = "../../roles"

  # main code
  config.vm.provision :shell, path: "post-install.sh"
  config.vm.define "#{hostname}" do |machine|
    machine.vm.network "private_network", ip: "192.168.50.101"
    machine.vm.box = "centos/7"
    machine.vm.hostname = "#{hostname}"
    machine.vm.provider "virtualbox" do |vb|
      vb.name = "#{hostname}"
    end   
    machine.vm.provision :ansible do |ansible|
      ansible.limit = "all"
      ansible.playbook = "../../playbooks/apply-role.yml"
      ansible.extra_vars = {role_name: "base"}
    end
  end
end
Multi-machine (Bridge NIC)
Vagrant.configure("2") do |config|
  config.vm.provision :shell, path: "post-install.sh"
  config.vm.define "node1" do |node1|
    node1.vm.network "public_network", ip: "192.168.1.115", bridge: "enp2s0"
    node1.vm.box = "centos/7"
    node1.vm.hostname = "node1" # --> sets hostname as seen inside OS
    node1.vm.provider "virtualbox" do |vb|
      vb.name = "node1"  # --> sets virtualbox machine name as seen in VM manager
    end   
  end
  config.vm.define "node2" do |node2|
    node2.vm.network "public_network", ip: "192.168.1.116", bridge: "enp2s0"
    node2.vm.box = "centos/7"
    node2.vm.hostname = "node2"
    node2.vm.provider "virtualbox" do |vb|
      vb.name = "node2"
    end   
  end
  config.vm.define "node3" do |node3|
    node3.vm.network "public_network", ip: "192.168.1.117", bridge: "enp2s0"
    node3.vm.box = "centos/7"
    node3.vm.hostname = "node3"
    node3.vm.provider "virtualbox" do |vb|
      vb.name = "node3"
    end   
  end
end
Multi-machine
Vagrant.configure("2") do |config|
  config.vm.provision :shell, path: "post-install.sh"
  config.vm.define "node1" do |node1|
    node1.vm.network "private_network", ip: "192.168.50.101"
    node1.vm.box = "centos/7"
    node1.vm.hostname = "node1"
    node1.vm.provider "virtualbox" do |vb|
      vb.name = "node1"
    end   
  end
  config.vm.define "node2" do |node2|
    node2.vm.network "private_network", ip: "192.168.50.102"
    node2.vm.box = "centos/7"
    node2.vm.hostname = "node2"
    node2.vm.provider "virtualbox" do |vb|
      vb.name = "node2"
    end   
  end
  config.vm.define "node3" do |node3|
    node3.vm.network "private_network", ip: "192.168.50.103"
    node3.vm.box = "centos/7"
    node3.vm.hostname = "node3"
    node3.vm.provider "virtualbox" do |vb|
      vb.name = "node3"
    end   
  end
end
Short version of the
above files
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
  N = 3
  (1..N).each do |machine_id|
    config.vm.define "redis#{machine_id}" do |machine|
      machine.vm.hostname = "redis#{machine_id}"
      machine.vm.box = "centos/7"
      machine.vm.network "private_network", ip: "192.168.50.#{107+machine_id}"
    end
  end
end
Hostname w/ dash
Vagrant.configure("2") do |config|
  config.vm.provision :shell, path: "post-install.sh"

  config.vm.define "ha-node1" do |config|
    config.vm.network "private_network", ip: "192.168.50.105"
    config.vm.box = "centos/7"
    config.vm.hostname = "ha-node1"
    config.vm.provider "virtualbox" do |vb|
      vb.name = "ha-node1"
    end   
  end
end
Using ansilble provisioner
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
  num_nodes = 3
  identifier = "redis"
  vlan = "192.168.50."
  config.vm.provision "shell", path: "post-install.sh"
  (1..num_nodes).each do |machine_id|
    config.vm.define "#{identifier}#{machine_id}" do |machine|
      machine.vm.hostname = "#{identifier}#{machine_id}"
      machine.vm.box = "centos/7"
      machine.vm.network "private_network", ip: "#{vlan}#{107+machine_id}"
      machine.vm.provider "virtualbox" do |vb|
        vb.name = "#{identifier}#{machine_id}"
      end
      if machine_id == num_nodes
        machine.vm.provision :ansible do |ansible|
          # Disable default limit to connect to all the machines
          ansible.limit = "all"
          ansible.playbook = "sample.yml" # --> here is your playbook and will
        end                               #     and will run after all VMs are up
      end
    end
  end
end

You can set role path and add extra variables.

[...] 
  ENV['ANSIBLE_ROLES_PATH'] = "../../roles"
  [...]
          ansible.playbook = "../../playbooks/apply-role.yml"
          ansible.extra_vars = {role_name: "redis"}
        end
  [...]
end

No comments:

Post a Comment