Vagrant Cheat sheet Part 1 – Introduction and working with Boxes

Vagrant Cheatsheet

Vagrant is a tool for creating and managing virtualized development and testing environments

The Tao of Vagrant
Developers – checkout from any repo, run vagrant up and have a fully working dev environment
DevOps – work on automation scripts, test them using a complete sandbox environment in vagrant

List of vagrant plugins

https://github.com/mitchellh/vagrant/wiki/Available-Vagrant-Plugins

# Install vmware_fusion plugin
$ vagrant plugin install vagrant-vmware-fusion
$ vagrant plugin license vagrant-vmware-fusion license.lic

# Get Version
$ vagrant –version

# Manage Boxes
# List boxes
$ vagrant box list

# Add a box
$ vagrant box add

# Remove a box
$ vagrant box remove

# Create and start up VM
$ vagrant init
$ vagrant up
$ vagrant ssh

# Determine state of Vagrant
$ vagrant status

# Shut down
$ vagrant halt

# Remove
$ vagrant destroy

Vagrantfiles
Vagrant is configured per project. Each project has its own environment. A project is denoted by a vagrantfile.
The vagrantfile is a text file read by vagrant in order to determine what needs to be done to create a working environment

Vagrant Boxes
Vagrant builds VM’s off base images that it uses as a template. Boxes are managed using the vagrant box command

.vagrant Folder
Vagrant creates a .vagrant folder in the project directory. This is used to maintain some state for Vagrant.
You should exclude the .vagrant directory from any commits.

Shared Filesystems
Vagrant uses shared filesystems to store files that are not removed when vagrant destroy is called. By default these are stored in the project directory and are mapped to /vagrant in the VM
This can be overridden by changing the config.vm.share_folder parameter

Basic Networking
You can forward traffic to the VM by changing the config.vm.forward_port parameter. For example if you have a web service running on port 80, you can set the forward_port parameter so that it is accessible on the host machine on port 8080 -> config.vm.forward_port 80, 8080

Teardown
Suspend will save the current running state of the machine
$ vagrant suspend
Halt will shut the machine down completely
$ vagrant halt
Destroy will halt the machine and then delete all trace of it, requires confirmation but can be overwritten using the –force flag
$ vagrant destroy

Vagrant Cheat Sheet – Provisioning with Ansible

Below is a sample Vagrantfile for provisioning a single VM and an ansible playbook for installing apache2


# playbook.yml
---
- hosts: default
  user: vagrant
  tasks:
  # Perform updates
  - name: Perform updates
  command: apt-get update
  # Install apache2
  - name: Install Apache2
  action: apt pkg=apache2 state=latest
  # Reset siteroot
  - name: Reset siteroot
  command: rm -rf /var/www/html
  # Link siteroot
  - name: Link siteroot
  command: ln -fs /vagrant /var/www/html

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

# Vagrantfile API/syntax version. Don’t touch unless you know what you’re doing!
VAGRANTFILE_API_VERSION = “2”

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = “chef/ubuntu-14.04″

config.vm.network “forwarded_port”, guest: 80, host: 8080

config.vm.provision “ansible” do |ansible|
ansible.playbook = “playbook.yml”
ansible.sudo = true
end

Vagrant Cheat Sheet – Provisioning with Chef Solo

Below is a sample Vagrantfile for provisioning a single VM and a chef recipe for installing apache2

# default.rb
execute “apt-get update”
package “apache2″
execute “rm -rf /var/www/html”
link “/var/www/html” do
to “/vagrant”
end

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

# Vagrantfile API/syntax version. Don’t touch unless you know what you’re doing!
VAGRANTFILE_API_VERSION = “2”

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = “chef/ubuntu-14.04″

config.vm.network “forwarded_port”, guest: 80, host: 8080

config.vm.provision “chef_solo”, run_list: ["vagrant_book"]
end

Vagrant Cheat Sheet – Provisioning with Shell scripts

Easiest form of provisioning is to use a shell script. Vagrant performs all provisioning as root so no need to use sudo

Below is a sample Vagrantfile for provisioning a single VM and a shell script for installing apache2

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

# Vagrantfile API/syntax version. Don’t touch unless you know what you’re doing!
VAGRANTFILE_API_VERSION = “2”

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = “chef/ubuntu-14.04″

# Forward port on guest 80 to host 8080
config.vm.network “forwarded_port”, guest: 80, host: 8080

# Run provisioner script
config.vm.provision “shell”, path: “provision.sh”
end

# provision.sh
#!/usr/bin/env bash

echo “Installing apache and setting it up…”
apt-get update >/dev/null 2>&1
apt-get install -y apache2 >/dev/null 2>&1
rm -rf /var/www/html
ln -fs /vagrant /var/www/html