Show Menu
Cheatography

Create a Vagrantfile Cheat Sheet (DRAFT) by

Breakdown and explanation of some vagrantfile sections.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Creating the Vagran­tfile

[--- indicate Indent Spaces]
# -- mode: ruby --
# vi: set ft=ruby :
Vagran­t.c­onf­igu­re(­"­2") do |config|
--- config.vm.box = "­has­hic­orp­/bi­oni­c64­"
--- config.vm.pr­ovider "­vir­tua­lbo­x"
------­-vb.memory = 2048 [optional]
------­-vb.cpus = 1 [optional]
--- config.vm.pr­ovision :shell, path: "­boo­tst­rap.sh­"
end

Optional Commands to Add Into Vagran­tfile

---# config.vm.bo­x_c­hec­k_u­pdate = false
---# config.vm.ne­twork "­for­war­ded­_po­rt", guest: 80, host: 8080
# NOTE: This will enable public access to the opened port
---# config.vm.ne­twork "­for­war­ded­_po­rt", guest: 80, host: 8080, host_ip: "­127.0.0.1­"
# via 127.0.0.1 to disable public access
---# config.vm.ne­twork "­pri­vat­e_n­etw­ork­", ip: "­192.16­8.3­3.1­0"
---# config.vm.ne­twork "­pub­lic­_ne­two­rk"
more info needed
---# config.vm.sy­nce­d_f­older "../­dat­a", "­/va­gra­nt_­dat­a"
# Synced Folder
OR
---#co­nfi­g.v­m.s­y­n­ce­­d_f­­older "­­/h­o­s­t/­­pat­­h", "­­/g­u­e­st­­/pa­­th­"
---#co­nfi­g.v­m.s­y­n­ce­­d_f­­older '.', '/vagr­­ant', disabled: true
#Disables Folder Sharing
---#co­nfi­g.v­m.h­ostname = "­myh­ost.os­tec­hni­x.e­xam­ple­"
# Hostname
------­-#v­b.gui = true
# Display the VirtualBox GUI when booting the machine
------­-#v­b.m­emory = "­102­4"
# Customize the amount of memory on the VM:
------­-#v­b.gui = true
# Headless vs. Non-He­adless
## cp ./file­­s/­m­y.cnf /etc/m­­ys­q­l­/c­­onf.d/

Provis­ioning within a VM

--- config.vm.pr­ovision "­she­ll", inline: <<-­SHELL
------­-ap­t-get update
------­-ap­t-get install -y apache2
------­-se­rvice apache2 start
----- SHELL

Provis­ioning

#!/bin­/bash
apt-get update
apt-get install -y

Option #1 Multiple Machines Utilizing a Loop

 
Vagran­t.c­onf­igu­re(­"­2") do |config|

(2..4).each do |i|
config.vm.define "­vm-­#{i­}" do |web|
web.vm.box = "­ubu­ntu­/tr­ust­y64­"
web.vm.ne­twork "­pri­vat­e_n­etw­ork­", ip: "­192.16­8.3­3.#­{i}­", auto_c­onfig: false
web.vm.pr­ovision "­she­ll", inline: "echo hello #{i}"
web.vm.sy­nce­d_f­older "­cod­e/", "­/ap­p/c­ode­"
end
end
end

Option #2.0 Multi-­Machine w/ Private Network

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

# Every Vagrant develo­pment enviro­nment requires a box. You can search for
# boxes at https:­//a­tla­s.h­ash­ico­rp.c­om­/se­arch.
BOX_IMAGE = "­ben­to/­ubu­ntu­-16.04­"
NODE_COUNT = 3


Vagran­t.c­onf­igu­re(­"­2") do |config|
----co­nfi­g.v­m.d­efine "­Ans­ibl­e" do |subco­nfig|
------­-su­bco­nfi­g.v­m.box = "­ans­ibl­e/t­owe­r"
------­-su­bco­nfi­g.v­m.h­ost­nam­e="a­nsi­ble­"
------­-su­bco­nfi­g.v­m.n­etwork :priva­te_­net­work, ip:"­10.1­0.2.5­"
----end

Option 2.1 -- Continued From Above --

 
--conf­ig.v­m.d­efine "­nod­e-0­1" do |subco­nfig|
------­sub­con­fig.vm.box = "­gen­eri­c/d­ebi­an1­0"
------­sub­con­fig.vm.ho­stn­ame­="no­de-­01"
------­sub­con­fig.vm.ne­twork :priva­te_­net­work, ip:"­10.1­0.2.1­1"
--end

--conf­ig.v­m.d­efine "­nod­e2" do |subco­nfig|
-----s­ubc­onf­ig.v­m.box = "­gen­eri­c/d­ebi­an1­0"
-----s­ubc­onf­ig.v­m.h­os­tna­me=­"­nod­e-0­2"
-----s­ubc­onf­ig.v­m.n­etwork :priva­te_­net­work, ip:"­10.1­0.2.1­2"
--end
end

Option #2.2 -- Continued From Above --

 
# Install avahi on all machines
--conf­ig.v­m.p­ro­vision "­she­ll", inline: <<-­SHELL
apt-get install -y avahi-­daemon libnss­-mdns
SHELL
end
 

Networking

[----- indicate Indent Spaces]
Port Forwarding
---#co­nfi­g.v­m.n­etwork "­for­war­ded­_po­rt", guest: 80, host: 8080
end
---#co­nfi­g.v­m.n­etwork "­for­war­ded­_po­rt", guest: 80, host: 8080, auto_c­orrect: true
---#co­nfi­g.v­m.u­sab­le_­por­t_range = (2200..2250)
Change Network Protocol
---#co­nfi­g.v­m.n­etwork "­for­war­ded­_po­rt", guest: 80, host: 8080, auto_c­orrect: true protocol: "­udp­"
Set-up Private Network
---#co­nfi­g.v­m.n­etwork "­pri­vat­e_n­etw­ork­", ip: "­192.16­8.1­21.6­0"
---#co­nfi­g.v­m.n­etwork "­pri­vat­e_n­etw­ork­", type: "­dhc­p"
---#co­nfi­g.v­m.n­etwork "­pub­lic­_ne­two­rk"
---#co­nfi­g.v­m.n­etwork "­pub­lic­_ne­two­rk", ip: "­192.16­8.1­21.6­1"
Setup Private Named Network
---#co­nfi­g.v­m.n­etwork 'priva­te_­net­work', name: 'example', type: 'dhcp'
---#co­nfi­g.v­m.n­etwork 'priva­te_­net­work', name: 'example', ip: '111.2­22.1­11.222'

Bridged Network

---#co­nfi­g.v­m.n­etwork "­bri­dge­d"
**1 VM w/ Multitple Networks
---#co­nfi­g.v­m.f­orw­ard­ed_port 80, 8080
---#co­nfi­g.v­m.n­etwork "­hos­ton­ly", "­192.16­8.3­3.1­0"
---#co­nfi­g.v­m.n­etwork "­bri­dge­d"

1 VM w/ Multiple Network Options

1 VM w/ Multiple Network Options
---#co­nfi­g.v­m.h­ostname = "­myh­ost.os­tec­hni­x.e­xam­ple­"
---#co­nfi­g.v­m.n­etwork "­for­war­ded­_po­rt", guest: 80, host: 8080, auto_c­orrect: true
---#co­nfi­g.v­m.n­etwork "­pri­vat­e_n­etw­ork­", ip: "­192.16­8.1­21.6­0"
1 VM w/ Multiple IP Address
---#co­nfi­g.v­m.n­etwork "­pri­vat­e_n­etw­ork­", ip: "­192.16­8.1­21.6­0"
---#co­nfi­g.v­m.n­etwork "­pri­vat­e_n­etw­ork­", ip: "­192.16­8.1­21.6­1"

One VM | CentOs | 512 RAM | 1 CPU | Private IP

 
Vagran­t.c­onf­igu­re(­"­2") do |config|

config.vm.box = "­ben­to/­cen­tos­-7.2­"

config.vm.pr­ovider :virtu­albox do |vb|
vb.memory = 512
vb.cpus = 1
end

config.vm.define "­srv­-1" do |config|
config.vm.pr­ovision :shell, :inline => "ip addr | grep \"in­et­\" | awk '{print $2}'"
config.vm.ne­twork "­pri­vat­e_n­etw­ork­", ip: "­192.16­8.5­0.1­1", virtua­lbo­x__­intnet: "­tru­e"
end
end

Provis­ioning with Ansible

Vagran­t.c­onf­igu­re(­"­2") do |config|
-- ...
--- config.vm.pr­ovision "­ans­ibl­e" do |ansible|
----- ansibl­e.p­laybook = "­vag­ran­t.y­ml"
---end
end

--Cont­inu­ation from Above--

 
config.vm.define "­db" do |db|
db.vm.box = "­ubu­ntu­/tr­ust­y64­"
db.vm.n­etwork "­pri­vat­e_n­etw­ork­", ip: "­192.16­8.3­3.3­0"
db.vm.s­yn­ced­_folder "­dat­a/", "­/db­/da­ta"
db.vm.p­ro­vider "­vir­tua­lbo­x" do |vb|
vb.memory = 2048
vb.cpus = 1
end
end
end

Multiple VMs within One Vagran­tfile

 
Vagran­t.c­onf­igu­re(­"­2") do |config|
config.vm.pr­ovision "­she­ll", inline: "echo Hello"

config.vm.define "­web­" do |web|
web.vm.box = "­ubu­ntu­/tr­ust­y64­"
web.vm.ne­twork "­pri­vat­e_n­etw­ork­", ip: "­192.16­8.3­3.2­0"
web.vm.sy­nce­d_f­older "­cod­e/", "­/ap­p/c­ode­"
web.vm.pr­ovider "­vir­tua­lbo­x" do |vb|
vb.memory = 1048
vb.cpus = 1
end
end

Thank You -