Stathers.net

Dirty Vagrant

17 May 2017

Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the “works on my machine” excuse a relic of the past.

As it says on tin, Vagrant is a way to spin up VMs quickly in an automated manner. To get started simply:

vagrant init centos/7
vagrant up

This will download the base box from HashiCorp’s public vagrant box server and start the box. The centos/7 box specifically refers to https://atlas.hashicorp.com/centos/boxes/7.

The newly started box can then be accessed with:

vagrant ssh

Vagrantfile

The meat and potatoes of the Vagrant experience will be manipulated via the Vagrantfile (created upon running vagrant init).

Here is an example Vagrantfile which will setup two machines and network them together via private interfaces.

Vagrant.configure("2") do |config|
    config.vm.define "dns" do |dns|
        dns.vm.box = "centos/7"
        dns.vm.network "private_network", ip: "172.16.0.254"
    end
    config.vm.define "master" do |master|
        master.vm.box = "centos/7"
        master.vm.network "private_network", ip: "172.16.0.1"
    end
    config.vm.provision :shell, path: "bootstrap.sh"
end

This names two machines, instructs Vagrant to base them on the centos/7 box and assigns them static IP addresses on private interfaces.

It also triggers a script to run upon provisioning the box for the first time. In this case the bootstrap.sh script resides in the same directory as the Vagrantfile and contains some simple bash commands:

cat << EOF > /etc/resolv.conf
search puppet.local
nameserver 172.16.0.254
EOF

echo "PEERDNS=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0

A provisioning script is definitely not required though!

Read More

https://www.vagrantup.com/intro/getting-started/index.html