Vagrant is probably one of my favorite tools. The ability to quickly spin up one, or multiple, virtual machines for testing is super useful. Over the course of the last year or two really using it, I have developed what I call my “master” Vagrantfile. This Vagrantfile essentially has lots of the common things I do, with comments that allow me to easily select the things I want. For instance, options for all the different distributions that I need to test with, whether I want to provision with a shell script or with Ansible, and places for specifying multiple VMs. So I figured I would post my master file here for others to take a look at, use, or even just as a guide.

Some of these options are machine specific, and when running multiple VMs would need to be placed under the definition for each of the separate VMs. A good example of this is the Network settings. Other options, like provisioning, can be placed either outside the machine definitions (and apply to all machines) or inside and only apply to specific machines. This is especially useful when spinning up test clusters that may require different settings or software to be deployed for Master and Slave configurations.

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

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  # config.vm.box = "ubuntu/xenial64"
  # config.vm.box = "hashicorp/precise64"
  # config.vm.box = "debian/jessie64"
  # config.vm.box = "ubuntu/trusty64"

  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Sync a data folder to the machine
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Sizing options
  config.vm.provider "virtualbox" do |vb|
    vb.cpus = 1 
    vb.memory = 1024
  end 

  # Run commands via the Shell provisioner after spin-up
  # config.vm.provision "shell", inline: <<-SHELL
  #   yum -y install docker
  #   systemctl start docker
  # SHELL

  # Use the Ansible provisioner, setting the machine group type 
  # and the playbook to run
  # config.vm.provision "ansible" do |ansible|
  #   ansible.groups = { 
  #     "dockerhosts" => ["default"]
  #   }   

  #   ansible.playbook = "site.yml"
  #   ansible.sudo = true
  # end 

  # Use the following if you want to run multiple machines
  #  config.vm.define "master" do |master|
  #    master.vm.network "private_network", ip: "192.168.33.10"
  #  end
  #
  #  config.vm.define "slave1" do |slave1|
  #    slave1.vm.network "private_network", ip: "192.168.33.11"
  #  end
  #
  #  config.vm.define "slave2" do |slave2|
  #    slave2.vm.network "private_network", ip: "192.168.33.12"
  #  end
  #
  #  config.vm.define "slave3" do |slave3|
  #    slave3.vm.network "private_network", ip: "192.168.33.13"
  #  end

end