SlideShare a Scribd company logo
Create Development and Production
Environments with Vagrant
Brian P. Hogan
Twitter: @bphogan
Hi. I'm Brian.
— Programmer (http://github.com/napcs)
— Author (http://bphogan.com/publications)
— Engineering Technical Editor @ DigitalOcean
— Musician (http://soundcloud.com/bphogan)
Let's chat today! I want to hear what you're working on.
Roadmap
— Learn about Vagrant
— Create a headless Ubuntu
Server
— Explore provisioning
— Create a Windows 10 box
— Create boxes on
DigitalOcean
Disclaimers and Rules
— This is a talk for people new
to Vagrant.
— This is based on my personal
experience.
— If I go too fast, or I made a
mistake, speak up.
— Ask questions any time.
— If you want to argue, buy me
a beer later.
Vagrant is a tool to provision development environments in virtual
machines. It works on Windows, Linux systems, and macOS.
Vagrant
A tool to provision development environments
— Automates so!ware virtualization
— Uses VirtualBox, VMWare, or libvirt for
virtualization layer
— Runs on Mac, Windows, *nix
Vagrant needs a virtualization layer, so we'll use VirtualBox, from Oracle, cos it's free.
Vagrant uses SSH and if you're on Windows, you'll need PuTTY to log in, and PuTTYgen
to generate keys if you don't have public and private keys yet.
Simplest Setup - Vagrant and VirtualBox
— Vagrant: https://www.vagrantup.com
— VirtualBox: https://virtualbox.org
— On Windows
— PuTTY to log in to SSH
— PuTTYgen for SSH Keygeneration
Using Vagrant
$ vagrant init ubuntu/xenial64
Creates a config file that will tell Vagrant to:
— Download Ubuntu 16.04 Server base image.
— Create VirtualBox virtual machine
— Create an ubuntu user
— Start up SSH on the server
— Mounts current folder to /vagrant
Downloads image, brings up machine.
Firing it up
$ vagrant up
Log in to the machine with the ubuntu user. Let's demo
it.
Using the machine
$ vagrant ssh
ubuntu@ubuntu-xenial:~$
While the machine is provisioning let's look at the
configuration file in detail.
Configuration with Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
# ...
end
Vagrant automatically shares the current working directory.
Sharing a folder
config.vm.synced_folder "./data", "/data"
Forward ports from the guest to the host, for servers and
more.
Forwarding ports
config.vm.network "forwarded_port", guest: 3000, host: 3000
Creating a private network is easy with Vagrant. This
network is private between the guest and the host.
Private network
config.vm.network "private_network", ip: "192.168.33.10"
This creates a public network which lets your guest machine
interact with the rest of your network, just as if it was a real machine.
Bridged networking
config.vm.network "public_network"
The vm.privder block lets you pass configuration options for the provider. For
Virtualbox that means you can specify if you want a GUI, specify the amount of
memory you want to use, and you can also specify CPU and other options.
VirtualBox options
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true
# Customize the amount of memory on the VM:
vb.memory = "1024"
end
We can set the name of the machine, the display name in Virtualbox, and the
hostname of the server. We just have to wrap the machine definition in a
vm.define block.
Naming things
Vagrant.configure("2") do |config|
config.vm.define "devbox" do |devbox|
devbox.vm.box = "ubuntu/xenial64"
devbox.vm.hostname = "devbox"
config.vm.network "forwarded_port", guest: 3000, host: 3000
devbox.vm.provider "virtualbox" do |vb|
vb.name = "Devbox"
vb.gui = false
vb.memory = "1024"
end
end
end
Provisioning
— Installing so!ware
— Creating files and folders
— Editing configuration files
— Adding users
Vagrant Provisioner
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL
Demo: Apache Web Server
— Map local folder to server's /
var/www/html folder
— Forward local port 8080 to
port 80 on the server
— Install Apache on the server
The Vagrantfile
Vagrant.configure("2") do |config|
config.vm.define "devbox" do |devbox|
devbox.vm.box = "ubuntu/xenial64"
devbox.vm.hostname = "devbox"
devbox.vm.network "forwarded_port", guest: 80, host: 8080
devbox.vm.synced_folder ".", "/var/www/html"
devbox.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL
end
end
Configuration Management with
Ansible
— Define idempotent tasks
— Use declarations instead of
writing scripts
— Define roles (webserver,
database, firewall)
— Use playbooks to provision
machines
An Ansible playbook contains all the stuff we want to do to
our box. This one just installs some things on our server.
Playbook
---
- hosts: all
become: true
tasks:
- name: update apt cache
apt: update_cache=yes
- name: install Apache2
apt: name=apache2 state=present
- name: "Add nano to vim alias"
lineinfile:
path: /home/ubuntu/.bashrc
line: 'alias nano="vim"'
When we run the playbook, it'll go through all of the steps and
apply them to the server. It'll skip anything that's already in place.
Playbook results
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [devbox]
TASK [update apt cache] ********************************************************
changed: [devbox]
TASK [install Apache2] *********************************************************
changed: [devbox]
TASK [Add nano to vim alias] ***************************************************
changed: [devbox]
PLAY RECAP *********************************************************************
devbox : ok=4 changed=3 unreachable=0 failed=0
Vagrant has support for playbooks with the ansible
provisioner.
Provisioning with Ansible
Vagrant.configure("2") do |config|
config.vm.define "devbox" do |devbox|
# ,,,
devbox.vm.provision :ansible do |ansible|
ansible.playbook = "playbook.yml"
end
end
end
Demo: Dev box
— Ubuntu 16.04 LTS
— Apache/MySQL/PHP
— Git/vim
The Vagrantfile has the same setup we had last time, but we'll use the ansible provisioner this
time. Ansible uses Python 2 but Ubuntu 16.04 doesn't include Python2. We can tell Ansible
to use Python 3 using the ansible_python_interpreter option for host_vars.
The Vagrantfile
Vagrant.configure("2") do |config|
config.vm.define "devbox" do |devbox|
devbox.vm.box = "ubuntu/xenial64"
devbox.vm.hostname = "devbox"
devbox.vm.network "forwarded_port", guest: 80, host: 8080
devbox.vm.synced_folder ".", "/var/www/html"
devbox.vm.provision :ansible do |ansible|
ansible.playbook = "playbook.yml"
ansible.host_vars = {
"devbox" => {"ansible_python_interpreter" => "/usr/bin/python3"}
}
end
end
end
Our playbook installs a few pieces of software including
Ruby, MySQL, Git, and Vim.
Playbook
---
- hosts: all
become: true
tasks:
- name: update apt cache
apt: update_cache=yes
- name: install required packages
apt: name={{ item }} state=present
with_items:
- apache2
- mysql-server
- ruby
- git-core
- vim
Vagrant Boxes
Get boxes from https://app.vagrantup.com
Useful boxes:
— scotch/box - Ubuntu 16.04 LAMP server ready to go
— Microso!/EdgeOnWindows10 - Windows 10 with
Edge browser
You can get more Vagrant boxes from the Vagrant Cloud web site, including boxes
that have a bunch of tooling already installed. You can even get a Windows box.
Demo: Windows Dev box
— Test out web sites on Edge
— Run so!ware that only works on Windows
— Run with a GUI
— Uses 120 day evaluation license, FREE and direct
from MS!
Here's how we'd configure a Windows box. We tell Vagrant we're using Windows instead
of Linux, and we specify that we're using WinRM instead of SSH. Then we provide
credentials.
The Vagrantfile
$ vagrant init
Vagrant.configure("2") do |config|
config.vm.define "windowstestbox" do |winbox|
winbox.vm.box = "Microsoft/EdgeOnWindows10"
winbox.vm.guest = :windows
winbox.vm.communicator = :winrm
winbox.winrm.username = "IEUser"
winbox.winrm.password = "Passw0rd!"
winbox.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "2048"
end
end
end
Vagrant and the Cloud
— Provides
— AWS
— Google
— DigitalOcean
— Build and Provision
Creating virtual machines is great, but you can use Vagrant to
build machines in the cloud using various Vagrant providers.
First, we install the Vagrant plugin. This will let us use a new
provider. We need an API key from DigitalOcean.
Demo: Web Server on DigitalOcean
Install the plugin
$ vagrant plugin install vagrant-digitalocean
Get a DigitalOcean API token from your account and
place it in your environment:
$ export DO_API_KEY=your_api_key
We'll place that API key in an environment variable
and reference that environment variable from our
config. That way we keep it out of configurations we
The config has a lot more info than before. We have to specify our SSH key for
DigitalOcean. We also have to specify information about the kind of server we want to set
up. We configure these through the provider, just like we configured Virtualbox.
Demo: Ubuntu on DigitalOcean
Vagrant.configure('2') do |config|
config.ssh.private_key_path = '~/.ssh/id_rsa'
config.vm.box = 'digital_ocean'
config.vm.box_url = "https://github.com/devopsgroup-
io/vagrant-digitalocean/raw/master/box/digital_ocean.box"
config.vm.define "webserver" do |box|
box.vm.hostname = "webserver"
box.vm.provider :digital_ocean do |provider|
provider.token = ENV["DO_API_KEY"]
provider.image = 'ubuntu-16-04-x64'
provider.region = 'nyc3'
provider.size = '512mb'
provider.private_networking = true
end # provider
end # box
end # config
We can also use provisioning to install stuff on the server.
Provisioning works too!
Vagrant.configure('2') do |config|
# ...
config.vm.define "webserver" do |box|
#...
provider.private_networking = true
box.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
ansible.host_vars = {
"webserver" => {"ansible_python_interpreter" => "/usr/bin/python3"}
}
end # ansible
end # box
end # config
Immutable infrastructure
— "Treat servers like cattle, not
pets"
— Don't change an existing
server. Rebuild it
— No "Snowflake" servers
— No "configuration dri#" over
time
— Use images and tools to
rebuild servers and redeploy
apps quickly
Spin up an infrastructure
— Vagrantfile is Ruby code
— Vagrant supports multiple
machines in a single file
— Vagrant commands support
targeting specific machine
— Provisioning runs on all
machines
We define a Ruby array with a hash for each server. The
hash contains the server info we need.
The Vagrantfile
Vagrant.configure('2') do |config|
machines = [
{name: "web1", size: "1GB", image: "ubuntu-16-04-x64", region: "nyc3"},
{name: "web2", size: "1GB", image: "ubuntu-16-04-x64", region: "sfo1"}
]
config.ssh.private_key_path = '~/.ssh/id_rsa'
config.vm.box = 'digital_ocean'
config.vm.box_url = "https://github.com/devopsgroup-io/vagrant-
digitalocean/raw/master/box/digital_ocean.box"
We then iterate over each machine and create a vm.define block. We assign the
name, the region, the image, and the size. We could even use this to assign a
playbook for each machine.
The Vagrantfile
Vagrant.configure('2') do |config|
# ...
machines.each do |machine|
config.vm.define machine[:name] do |box|
box.vm.hostname = machine[:name]
box.vm.provider :digital_ocean do |provider|
provider.token = ENV["DO_API_KEY"]
provider.image = machine[:image]
provider.size = machine[:size]
end
box.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
ansible.host_vars = {
machine[:name] => {"ansible_python_interpreter" => "/usr/bin/python3"}
}
end # ansible
end # box
end # loop
end # config
Usage
— vagrant up brings all machines online and provisions
them
— vagrant ssh web1 logs into the webserver
— vagrant provision would re-run the Ansible
playbooks.
— vagrant ssh web1 logs into the db server
— vagrant rebuild rebuilds machines from scratch and
reprovisions
— vagrant destroy removes all machines.
Puphet is a GUI for generating Vagrant configs with provisioning. Pick your
OS, servers you need,. programming languages, and download your bundle.
Puphet
https://puphpet.com/
Terraform, by Hashicorp, is the production solution for building out cloud environments and provisioning them. Docker
Machine can also spin up cloud environments and then install Docker on them, which you can use to host apps
hosted in containers. And Ansible, with a little bit of code and plugins, can create and provision your infrastructure.
Alternatives to Vagrant
— Terraform
— Docker Machine
— Ansible
Summary
— Vagrant controls virtual
machines
— Use Vagrant's provisioner to
set up your machine
— Use existing box images to
save time
— Use Vagrant to build out an
infrastructure in the cloud
Questions
— Slides: https://bphogan.com/presentations/
vagrant2017
— Twitter: @bphogan
— Say hi!
```

More Related Content

PDF
Hcx intro preso v2
Parashar Singh
 
PDF
Quest 2 and the future of metaverse v2.0 210908
Michael Lesniak
 
PPTX
App Modernization
PT Datacomm Diangraha
 
PPTX
Cloud Migration - Cloud Computing Benefits & Issues
Artizen, Inc.
 
PDF
[오픈소스컨설팅] 쿠버네티스와 쿠버네티스 on 오픈스택 비교 및 구축 방법
Open Source Consulting
 
PPTX
Ansible
Afroz Hussain
 
PDF
Shift-left SRE: Self-healing on OpenShift with Ansible
Jürgen Etzlstorfer
 
PDF
Openstack 101
Kamesh Pemmaraju
 
Hcx intro preso v2
Parashar Singh
 
Quest 2 and the future of metaverse v2.0 210908
Michael Lesniak
 
App Modernization
PT Datacomm Diangraha
 
Cloud Migration - Cloud Computing Benefits & Issues
Artizen, Inc.
 
[오픈소스컨설팅] 쿠버네티스와 쿠버네티스 on 오픈스택 비교 및 구축 방법
Open Source Consulting
 
Ansible
Afroz Hussain
 
Shift-left SRE: Self-healing on OpenShift with Ansible
Jürgen Etzlstorfer
 
Openstack 101
Kamesh Pemmaraju
 

What's hot (20)

PDF
Choosing the Right Cloud Provider
Rutter Networking Technologies
 
PDF
Deploying your first application with Kubernetes
OVHcloud
 
PDF
[OpenStack] 공개 소프트웨어 오픈스택 입문 & 파헤치기
Ian Choi
 
PDF
From Spring Framework 5.3 to 6.0
VMware Tanzu
 
PPTX
IaaS and PaaS
Thanakrit Lersmethasakul
 
PPTX
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...
Simplilearn
 
PDF
Horizon 8 + Instant Clones
MarketingArrowECS_CZ
 
PPTX
What is Docker
Pavel Klimiankou
 
PDF
AWS Finance Symposium_국내 메이저 증권사의 클라우드 글로벌 로드밸런서 활용 사례 (gslb)
Amazon Web Services Korea
 
PDF
Microservices Design Patterns | Edureka
Edureka!
 
PPTX
Apigee Edge Product Demo
Apigee | Google Cloud
 
PDF
AWS IAM Tutorial | Identity And Access Management (IAM) | AWS Training Videos...
Edureka!
 
PDF
OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...
NETWAYS
 
PDF
Containerd + buildkit breakout
Docker, Inc.
 
PDF
9 steps to awesome with kubernetes
BaraniBuuny
 
PDF
Cloud Native In-Depth
Siva Rama Krishna Chunduru
 
PDF
Introduction to Docker
Luong Vo
 
PPT
Masakari project onboarding
Sampath Priyankara
 
PDF
Best Practices for Middleware and Integration Architecture Modernization with...
Claus Ibsen
 
PDF
Microsoft Azure Overview | Cloud Computing Tutorial with Azure | Azure Traini...
Edureka!
 
Choosing the Right Cloud Provider
Rutter Networking Technologies
 
Deploying your first application with Kubernetes
OVHcloud
 
[OpenStack] 공개 소프트웨어 오픈스택 입문 & 파헤치기
Ian Choi
 
From Spring Framework 5.3 to 6.0
VMware Tanzu
 
What Is A Docker Container? | Docker Container Tutorial For Beginners| Docker...
Simplilearn
 
Horizon 8 + Instant Clones
MarketingArrowECS_CZ
 
What is Docker
Pavel Klimiankou
 
AWS Finance Symposium_국내 메이저 증권사의 클라우드 글로벌 로드밸런서 활용 사례 (gslb)
Amazon Web Services Korea
 
Microservices Design Patterns | Edureka
Edureka!
 
Apigee Edge Product Demo
Apigee | Google Cloud
 
AWS IAM Tutorial | Identity And Access Management (IAM) | AWS Training Videos...
Edureka!
 
OSMC 2022 | Ignite: Observability with Grafana & Prometheus for Kafka on Kube...
NETWAYS
 
Containerd + buildkit breakout
Docker, Inc.
 
9 steps to awesome with kubernetes
BaraniBuuny
 
Cloud Native In-Depth
Siva Rama Krishna Chunduru
 
Introduction to Docker
Luong Vo
 
Masakari project onboarding
Sampath Priyankara
 
Best Practices for Middleware and Integration Architecture Modernization with...
Claus Ibsen
 
Microsoft Azure Overview | Cloud Computing Tutorial with Azure | Azure Traini...
Edureka!
 
Ad

Similar to Create Development and Production Environments with Vagrant (20)

PDF
DevOps Series: Defining and Sharing Testable Machine Configurations with vagrant
Felipe
 
PDF
Vagrant For DevOps
Lalatendu Mohanty
 
PDF
Vagrant - Version control your dev environment
bocribbz
 
PDF
Making Developers Productive with Vagrant, VirtualBox, and Docker
John Rofrano
 
PDF
Automating with ansible (Part B)
iman darabi
 
PDF
Keep calm and vagrant up
Daniel Carvalhinho
 
KEY
Vagrant
Michael Peacock
 
PPTX
Vagrant
Akshay Siwal
 
PDF
Intro to vagrant
Mantas Klasavicius
 
PPTX
Vagrant 101 Workshop
Liora Milbaum
 
PPTX
Development with Vagrant
John Coggeshall
 
PDF
Virtualization with Vagrant (ua.pycon 2011)
Dmitry Guyvoronsky
 
PDF
Vagrant are you still develop in a non-virtual environment-
Anatoly Bubenkov
 
PDF
Introduction to Vagrant
Marcelo Pinheiro
 
PDF
Vagrant workshop 2015
Haifa Ftirich
 
PPTX
Vagrant + Docker
David Giordano
 
PDF
Vagrant up-and-running
Joe Ferguson
 
ODP
It Works On My Machine: Vagrant for Software Development
Carlos Perez
 
PPTX
How To Set a Vagrant Development System
Paul Bearne
 
PDF
Take Home Your Very Own Free Vagrant CFML Dev Environment
ColdFusionConference
 
DevOps Series: Defining and Sharing Testable Machine Configurations with vagrant
Felipe
 
Vagrant For DevOps
Lalatendu Mohanty
 
Vagrant - Version control your dev environment
bocribbz
 
Making Developers Productive with Vagrant, VirtualBox, and Docker
John Rofrano
 
Automating with ansible (Part B)
iman darabi
 
Keep calm and vagrant up
Daniel Carvalhinho
 
Vagrant
Akshay Siwal
 
Intro to vagrant
Mantas Klasavicius
 
Vagrant 101 Workshop
Liora Milbaum
 
Development with Vagrant
John Coggeshall
 
Virtualization with Vagrant (ua.pycon 2011)
Dmitry Guyvoronsky
 
Vagrant are you still develop in a non-virtual environment-
Anatoly Bubenkov
 
Introduction to Vagrant
Marcelo Pinheiro
 
Vagrant workshop 2015
Haifa Ftirich
 
Vagrant + Docker
David Giordano
 
Vagrant up-and-running
Joe Ferguson
 
It Works On My Machine: Vagrant for Software Development
Carlos Perez
 
How To Set a Vagrant Development System
Paul Bearne
 
Take Home Your Very Own Free Vagrant CFML Dev Environment
ColdFusionConference
 
Ad

More from Brian Hogan (20)

PDF
Creating and Deploying Static Sites with Hugo
Brian Hogan
 
PDF
Automating the Cloud with Terraform, and Ansible
Brian Hogan
 
PDF
Docker
Brian Hogan
 
PDF
Getting Started Contributing To Open Source
Brian Hogan
 
PDF
Rethink Frontend Development With Elm
Brian Hogan
 
KEY
Testing Client-side Code with Jasmine and CoffeeScript
Brian Hogan
 
KEY
FUD-Free Accessibility for Web Developers - Also, Cake.
Brian Hogan
 
KEY
Responsive Web Design
Brian Hogan
 
KEY
Web Development with CoffeeScript and Sass
Brian Hogan
 
KEY
Building A Gem From Scratch
Brian Hogan
 
KEY
Intro To Advanced Ruby
Brian Hogan
 
KEY
Turning Passion Into Words
Brian Hogan
 
PDF
HTML5 and CSS3 Today
Brian Hogan
 
PDF
Web Development With Ruby - From Simple To Complex
Brian Hogan
 
KEY
Stop Reinventing The Wheel - The Ruby Standard Library
Brian Hogan
 
KEY
Intro to Ruby
Brian Hogan
 
KEY
Intro to Ruby - Twin Cities Code Camp 7
Brian Hogan
 
KEY
Make GUI Apps with Shoes
Brian Hogan
 
KEY
The Why Of Ruby
Brian Hogan
 
KEY
Story-driven Testing
Brian Hogan
 
Creating and Deploying Static Sites with Hugo
Brian Hogan
 
Automating the Cloud with Terraform, and Ansible
Brian Hogan
 
Docker
Brian Hogan
 
Getting Started Contributing To Open Source
Brian Hogan
 
Rethink Frontend Development With Elm
Brian Hogan
 
Testing Client-side Code with Jasmine and CoffeeScript
Brian Hogan
 
FUD-Free Accessibility for Web Developers - Also, Cake.
Brian Hogan
 
Responsive Web Design
Brian Hogan
 
Web Development with CoffeeScript and Sass
Brian Hogan
 
Building A Gem From Scratch
Brian Hogan
 
Intro To Advanced Ruby
Brian Hogan
 
Turning Passion Into Words
Brian Hogan
 
HTML5 and CSS3 Today
Brian Hogan
 
Web Development With Ruby - From Simple To Complex
Brian Hogan
 
Stop Reinventing The Wheel - The Ruby Standard Library
Brian Hogan
 
Intro to Ruby
Brian Hogan
 
Intro to Ruby - Twin Cities Code Camp 7
Brian Hogan
 
Make GUI Apps with Shoes
Brian Hogan
 
The Why Of Ruby
Brian Hogan
 
Story-driven Testing
Brian Hogan
 

Recently uploaded (20)

PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Doc9.....................................
SofiaCollazos
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 

Create Development and Production Environments with Vagrant

  • 1. Create Development and Production Environments with Vagrant Brian P. Hogan Twitter: @bphogan
  • 2. Hi. I'm Brian. — Programmer (http://github.com/napcs) — Author (http://bphogan.com/publications) — Engineering Technical Editor @ DigitalOcean — Musician (http://soundcloud.com/bphogan) Let's chat today! I want to hear what you're working on.
  • 3. Roadmap — Learn about Vagrant — Create a headless Ubuntu Server — Explore provisioning — Create a Windows 10 box — Create boxes on DigitalOcean
  • 4. Disclaimers and Rules — This is a talk for people new to Vagrant. — This is based on my personal experience. — If I go too fast, or I made a mistake, speak up. — Ask questions any time. — If you want to argue, buy me a beer later.
  • 5. Vagrant is a tool to provision development environments in virtual machines. It works on Windows, Linux systems, and macOS. Vagrant A tool to provision development environments — Automates so!ware virtualization — Uses VirtualBox, VMWare, or libvirt for virtualization layer — Runs on Mac, Windows, *nix
  • 6. Vagrant needs a virtualization layer, so we'll use VirtualBox, from Oracle, cos it's free. Vagrant uses SSH and if you're on Windows, you'll need PuTTY to log in, and PuTTYgen to generate keys if you don't have public and private keys yet. Simplest Setup - Vagrant and VirtualBox — Vagrant: https://www.vagrantup.com — VirtualBox: https://virtualbox.org — On Windows — PuTTY to log in to SSH — PuTTYgen for SSH Keygeneration
  • 7. Using Vagrant $ vagrant init ubuntu/xenial64 Creates a config file that will tell Vagrant to: — Download Ubuntu 16.04 Server base image. — Create VirtualBox virtual machine — Create an ubuntu user — Start up SSH on the server — Mounts current folder to /vagrant
  • 8. Downloads image, brings up machine. Firing it up $ vagrant up
  • 9. Log in to the machine with the ubuntu user. Let's demo it. Using the machine $ vagrant ssh ubuntu@ubuntu-xenial:~$
  • 10. While the machine is provisioning let's look at the configuration file in detail. Configuration with Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "ubuntu/xenial64" # ... end
  • 11. Vagrant automatically shares the current working directory. Sharing a folder config.vm.synced_folder "./data", "/data"
  • 12. Forward ports from the guest to the host, for servers and more. Forwarding ports config.vm.network "forwarded_port", guest: 3000, host: 3000
  • 13. Creating a private network is easy with Vagrant. This network is private between the guest and the host. Private network config.vm.network "private_network", ip: "192.168.33.10"
  • 14. This creates a public network which lets your guest machine interact with the rest of your network, just as if it was a real machine. Bridged networking config.vm.network "public_network"
  • 15. The vm.privder block lets you pass configuration options for the provider. For Virtualbox that means you can specify if you want a GUI, specify the amount of memory you want to use, and you can also specify CPU and other options. VirtualBox options config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine vb.gui = true # Customize the amount of memory on the VM: vb.memory = "1024" end
  • 16. We can set the name of the machine, the display name in Virtualbox, and the hostname of the server. We just have to wrap the machine definition in a vm.define block. Naming things Vagrant.configure("2") do |config| config.vm.define "devbox" do |devbox| devbox.vm.box = "ubuntu/xenial64" devbox.vm.hostname = "devbox" config.vm.network "forwarded_port", guest: 3000, host: 3000 devbox.vm.provider "virtualbox" do |vb| vb.name = "Devbox" vb.gui = false vb.memory = "1024" end end end
  • 17. Provisioning — Installing so!ware — Creating files and folders — Editing configuration files — Adding users
  • 18. Vagrant Provisioner config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install -y apache2 SHELL
  • 19. Demo: Apache Web Server — Map local folder to server's / var/www/html folder — Forward local port 8080 to port 80 on the server — Install Apache on the server
  • 20. The Vagrantfile Vagrant.configure("2") do |config| config.vm.define "devbox" do |devbox| devbox.vm.box = "ubuntu/xenial64" devbox.vm.hostname = "devbox" devbox.vm.network "forwarded_port", guest: 80, host: 8080 devbox.vm.synced_folder ".", "/var/www/html" devbox.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install -y apache2 SHELL end end
  • 21. Configuration Management with Ansible — Define idempotent tasks — Use declarations instead of writing scripts — Define roles (webserver, database, firewall) — Use playbooks to provision machines
  • 22. An Ansible playbook contains all the stuff we want to do to our box. This one just installs some things on our server. Playbook --- - hosts: all become: true tasks: - name: update apt cache apt: update_cache=yes - name: install Apache2 apt: name=apache2 state=present - name: "Add nano to vim alias" lineinfile: path: /home/ubuntu/.bashrc line: 'alias nano="vim"'
  • 23. When we run the playbook, it'll go through all of the steps and apply them to the server. It'll skip anything that's already in place. Playbook results PLAY [all] ********************************************************************* TASK [Gathering Facts] ********************************************************* ok: [devbox] TASK [update apt cache] ******************************************************** changed: [devbox] TASK [install Apache2] ********************************************************* changed: [devbox] TASK [Add nano to vim alias] *************************************************** changed: [devbox] PLAY RECAP ********************************************************************* devbox : ok=4 changed=3 unreachable=0 failed=0
  • 24. Vagrant has support for playbooks with the ansible provisioner. Provisioning with Ansible Vagrant.configure("2") do |config| config.vm.define "devbox" do |devbox| # ,,, devbox.vm.provision :ansible do |ansible| ansible.playbook = "playbook.yml" end end end
  • 25. Demo: Dev box — Ubuntu 16.04 LTS — Apache/MySQL/PHP — Git/vim
  • 26. The Vagrantfile has the same setup we had last time, but we'll use the ansible provisioner this time. Ansible uses Python 2 but Ubuntu 16.04 doesn't include Python2. We can tell Ansible to use Python 3 using the ansible_python_interpreter option for host_vars. The Vagrantfile Vagrant.configure("2") do |config| config.vm.define "devbox" do |devbox| devbox.vm.box = "ubuntu/xenial64" devbox.vm.hostname = "devbox" devbox.vm.network "forwarded_port", guest: 80, host: 8080 devbox.vm.synced_folder ".", "/var/www/html" devbox.vm.provision :ansible do |ansible| ansible.playbook = "playbook.yml" ansible.host_vars = { "devbox" => {"ansible_python_interpreter" => "/usr/bin/python3"} } end end end
  • 27. Our playbook installs a few pieces of software including Ruby, MySQL, Git, and Vim. Playbook --- - hosts: all become: true tasks: - name: update apt cache apt: update_cache=yes - name: install required packages apt: name={{ item }} state=present with_items: - apache2 - mysql-server - ruby - git-core - vim
  • 28. Vagrant Boxes Get boxes from https://app.vagrantup.com Useful boxes: — scotch/box - Ubuntu 16.04 LAMP server ready to go — Microso!/EdgeOnWindows10 - Windows 10 with Edge browser
  • 29. You can get more Vagrant boxes from the Vagrant Cloud web site, including boxes that have a bunch of tooling already installed. You can even get a Windows box. Demo: Windows Dev box — Test out web sites on Edge — Run so!ware that only works on Windows — Run with a GUI — Uses 120 day evaluation license, FREE and direct from MS!
  • 30. Here's how we'd configure a Windows box. We tell Vagrant we're using Windows instead of Linux, and we specify that we're using WinRM instead of SSH. Then we provide credentials. The Vagrantfile $ vagrant init Vagrant.configure("2") do |config| config.vm.define "windowstestbox" do |winbox| winbox.vm.box = "Microsoft/EdgeOnWindows10" winbox.vm.guest = :windows winbox.vm.communicator = :winrm winbox.winrm.username = "IEUser" winbox.winrm.password = "Passw0rd!" winbox.vm.provider "virtualbox" do |vb| vb.gui = true vb.memory = "2048" end end end
  • 31. Vagrant and the Cloud — Provides — AWS — Google — DigitalOcean — Build and Provision
  • 32. Creating virtual machines is great, but you can use Vagrant to build machines in the cloud using various Vagrant providers. First, we install the Vagrant plugin. This will let us use a new provider. We need an API key from DigitalOcean. Demo: Web Server on DigitalOcean Install the plugin $ vagrant plugin install vagrant-digitalocean Get a DigitalOcean API token from your account and place it in your environment: $ export DO_API_KEY=your_api_key We'll place that API key in an environment variable and reference that environment variable from our config. That way we keep it out of configurations we
  • 33. The config has a lot more info than before. We have to specify our SSH key for DigitalOcean. We also have to specify information about the kind of server we want to set up. We configure these through the provider, just like we configured Virtualbox. Demo: Ubuntu on DigitalOcean Vagrant.configure('2') do |config| config.ssh.private_key_path = '~/.ssh/id_rsa' config.vm.box = 'digital_ocean' config.vm.box_url = "https://github.com/devopsgroup- io/vagrant-digitalocean/raw/master/box/digital_ocean.box" config.vm.define "webserver" do |box| box.vm.hostname = "webserver" box.vm.provider :digital_ocean do |provider| provider.token = ENV["DO_API_KEY"] provider.image = 'ubuntu-16-04-x64' provider.region = 'nyc3' provider.size = '512mb' provider.private_networking = true end # provider end # box end # config
  • 34. We can also use provisioning to install stuff on the server. Provisioning works too! Vagrant.configure('2') do |config| # ... config.vm.define "webserver" do |box| #... provider.private_networking = true box.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" ansible.host_vars = { "webserver" => {"ansible_python_interpreter" => "/usr/bin/python3"} } end # ansible end # box end # config
  • 35. Immutable infrastructure — "Treat servers like cattle, not pets" — Don't change an existing server. Rebuild it — No "Snowflake" servers — No "configuration dri#" over time — Use images and tools to rebuild servers and redeploy apps quickly
  • 36. Spin up an infrastructure — Vagrantfile is Ruby code — Vagrant supports multiple machines in a single file — Vagrant commands support targeting specific machine — Provisioning runs on all machines
  • 37. We define a Ruby array with a hash for each server. The hash contains the server info we need. The Vagrantfile Vagrant.configure('2') do |config| machines = [ {name: "web1", size: "1GB", image: "ubuntu-16-04-x64", region: "nyc3"}, {name: "web2", size: "1GB", image: "ubuntu-16-04-x64", region: "sfo1"} ] config.ssh.private_key_path = '~/.ssh/id_rsa' config.vm.box = 'digital_ocean' config.vm.box_url = "https://github.com/devopsgroup-io/vagrant- digitalocean/raw/master/box/digital_ocean.box"
  • 38. We then iterate over each machine and create a vm.define block. We assign the name, the region, the image, and the size. We could even use this to assign a playbook for each machine. The Vagrantfile Vagrant.configure('2') do |config| # ... machines.each do |machine| config.vm.define machine[:name] do |box| box.vm.hostname = machine[:name] box.vm.provider :digital_ocean do |provider| provider.token = ENV["DO_API_KEY"] provider.image = machine[:image] provider.size = machine[:size] end box.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" ansible.host_vars = { machine[:name] => {"ansible_python_interpreter" => "/usr/bin/python3"} } end # ansible end # box end # loop end # config
  • 39. Usage — vagrant up brings all machines online and provisions them — vagrant ssh web1 logs into the webserver — vagrant provision would re-run the Ansible playbooks. — vagrant ssh web1 logs into the db server — vagrant rebuild rebuilds machines from scratch and reprovisions — vagrant destroy removes all machines.
  • 40. Puphet is a GUI for generating Vagrant configs with provisioning. Pick your OS, servers you need,. programming languages, and download your bundle. Puphet https://puphpet.com/
  • 41. Terraform, by Hashicorp, is the production solution for building out cloud environments and provisioning them. Docker Machine can also spin up cloud environments and then install Docker on them, which you can use to host apps hosted in containers. And Ansible, with a little bit of code and plugins, can create and provision your infrastructure. Alternatives to Vagrant — Terraform — Docker Machine — Ansible
  • 42. Summary — Vagrant controls virtual machines — Use Vagrant's provisioner to set up your machine — Use existing box images to save time — Use Vagrant to build out an infrastructure in the cloud