Professional Documents
Culture Documents
Learning Vagrant
Elle Krout
elle@linuxacademy.com
Oct 15, 2019
Study Guide | Learning Vagrant
Contents
What Is Virtualization? 9
Overview 9
Why Virtualize? 9
What Is a Hypervisor 11
Overview 11
Vagrant Support 11
What Is Vagrant? 13
Overview 13
Vagrant Components 13
Our Environments 15
2
Study Guide | Learning Vagrant
Linux 16
Vagrant 16
VirtualBox 16
Mac 17
Vagrant 17
VirtualBox 17
Windows 18
Git 18
Vagrant 18
VirtualBox 19
Plugin Overview 20
Updating: 20
Provider Plugins 22
3
Study Guide | Learning Vagrant
Additional Plugins 23
Boxes 24
Discover Boxes 24
The Vagrantfile 25
Vagrant Up 27
Accessing the VM 29
VM Management 30
Snapshots 31
4
Study Guide | Learning Vagrant
Basic Syncing 33
Options 33
Example Vagrantfile 34
NFS 35
RSync 36
SMB 38
Port Management 39
Public Networking 43
Provisioning Overview 44
Shell 45
5
Study Guide | Learning Vagrant
File 47
Ansible 48
Ansible Provisioner 48
Ansible Local 49
Ansible Common 49
Chef 51
Chef Solo 51
Chef Zero 52
Chef Client 53
Chef Apply 53
Puppet 55
Puppet Apply 55
Puppet Agent 56
6
Study Guide | Learning Vagrant
Salt 57
Docker 59
Box Overview 60
Share Overview 65
HTTP Sharing 66
SSH Sharing 67
Vagrant Connect 68
Windows Considerations 69
7
Study Guide | Learning Vagrant
8
Study Guide | Learning Vagrant
What Is Virtualization?
Overview
• The act of creating a "virtual" (non-real) version of something
• Hardware virtualization:
• Creates a "virtual" computer ("virtual machine") on top of an existing computer
• Uses the existing machine's hardware:
CPU, RAM, disk space
• Uses its own operating system
• Virtual machines created/managed by a control program, called a hypervisor
• Hypervisor examples:
• KVM
• XEN
• VMware
• VirtualBox
• Parallels
Why Virtualize?
• Datacenter hardware is often too large for most needs:
• Divides the host into smaller virtual machines
9
Study Guide | Learning Vagrant
10
Study Guide | Learning Vagrant
What Is a Hypervisor
Overview
• The process, software, firmware, or hardware that creates a virtual machine
• Exists only to let virtual machines share resources with the host
• Abstracts resources from the guest machine
• Two types of hypervisors for two circumstances:
• Type I hypervisors work on bare metal
• Type II hypervisors work over an operating system
• For some hypervisors, this distinction is less clear
KVM, which is a Linux kernel module, can be classified as both a type I and II hypervisor
Vagrant Support
• Native hypervisor support:
• VirtualBox
• Hyper-V
• Docker*
• Additional hypervisors can be added through the use of provider plugins:
• A provider is the underlying platform that creates our virtual machines:
Most often a hypervisor, but can also be containers or the cloud
11
Study Guide | Learning Vagrant
Docker is not a hypervisor but a containers platform; support for Docker was added in Vagrant 1.6. Note that not all Vagrant
features can be used with the Docker provider.
12
Study Guide | Learning Vagrant
What Is Vagrant?
Overview
• Vagrant is not a hypervisor.
• Vagrant is not a virtualization platform.
• Vagrant is a wrapper tool that allows us to automate our virtual machine workflow to create easy-to-use and easy-
to-replicate environments.
• Vagrant leverages an existing hypervisor (or container/cloud platform) to create virtual machines.
Vagrant Components
• The Vagrantfile:
• The file that describes the environment we wish to provision
• Written in Ruby
• Boxes:
• The base image used to provision the guest server(s)
• Can be taken from app.vagrantup.com or self-made
• The CLI:
• The command line interface used to manage Vagrant environments
• Plugins:
• Any assisting feature or functionality added to Vagrant:
Providers are a plugin
13
Study Guide | Learning Vagrant
14
Study Guide | Learning Vagrant
Our Environments
• For videos:
• Personal workstation (not Cloud Playground)
• VirtualBox
• Vagrant
• Ubuntu 18.04 guest
• For hands-on labs:
• EC2 instance
• LXC or Docker
• Vagrant
• Various guests
15
Study Guide | Learning Vagrant
Linux
Vagrant
• Download from the Download Vagrant page:
• Previous versions found here
• This course uses Vagrant 2.2.5
• Verify the checksum before download:
• Signature file is found on downloads page, as well as included in the directory for any previous releases
• Extract using appropriate system package manager:
• .zip file for non Debian/CentOS-based hosts, requires manual install
VirtualBox
• Download the appropriate package for your distro at the Linux Downloads page:
• This course uses version 6.0.10
• Import Oracle's public key to validate the download
• Extract using either rpm , dpkg , or by following the instructions on the download page
16
Study Guide | Learning Vagrant
Mac
Vagrant
• Download from the Download Vagrant page:
• Previous versions found here
• This course uses Vagrant 2.2.5
• Double click the .dmg file to open
• Double click vagrant.pkg to start the installer
• Follow the installer:
• Select the installation location
• Click Install
VirtualBox
• Download the "OS X hosts" option from the VirtualBox Downloads page:
• This course uses version 6.0.10
• Double click the .dmg file to open
• Double click the VirtualBox.pkg icon
• Follow the installer:
• Select the installation location
• Click Install
17
Study Guide | Learning Vagrant
Windows
Git
• Download Git for Windows from the Git download page:
• This course uses version 2.22.0
• Open the downloaded .exe file
• Follow the prompts to install:
• Select Git from the command line and also from 3rd-party software
• Select Use the OpenSSL library
• Select Checkout as-is, commit Unix-style line endings
• Select Use MinTTY
Vagrant
• Download Vagrant for Windows from the Vagrant Downloads page:
• This course uses version 2.2.5
• Open the downloaded .msi file
• Follow the prompts to install:
• Wait to restart until after VirtualBox is installed
18
Study Guide | Learning Vagrant
VirtualBox
Note: that if you are already using Hyper-V, it must be disabled before you can use VirtualBox
• Download VirtualBox for Windows Hosts from the Download VirtualBox page:
• This course uses version 6.0.10
• Open the downloaded .exe file
• Follow the prompts to install:
• Note that you will temporarily be disconnected from the Internet
• To finish, restart your Windows machine
• To test Vagrant, open the Command Prompt ( cmd.exe ):
• Run vagrant version to confirm
19
Study Guide | Learning Vagrant
Plugin Overview
Updating:
• All plugins:
• vagrant plugin update
• A single plugin:
• vagrant plugin update PLUGIN
• Repair failing plugins:
• vagrant plugin repair
• Install license for proprietary plugin: vagrant plugin license LICENSE-FILE
• Uninstall:
• vagrant plugin uninstall PLUGIN
20
Study Guide | Learning Vagrant
21
Study Guide | Learning Vagrant
Provider Plugins
22
Study Guide | Learning Vagrant
Additional Plugins
23
Study Guide | Learning Vagrant
Boxes
Discover Boxes
• Boxes can be user-created or downloaded from other parties
• The official box repository can be found here
• Hashicorp only offers a few Ubuntu-based boxes
• Namespaces do not mean the box is official
• HashiCorp also recommends Bento, which provides open source boxes that work across multiple hypervisors
• Add a box:
• vagrant box add USER/BOX
24
Study Guide | Learning Vagrant
The Vagrantfile
25
Study Guide | Learning Vagrant
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
end
26
Study Guide | Learning Vagrant
Vagrant Up
27
Study Guide | Learning Vagrant
28
Study Guide | Learning Vagrant
Accessing the VM
• Access host:
• vagrant ssh <hostname>
• Access from outside of the environment directory:
• vagrant ssh-config
• --host to define a specific host
• Default user:
• vagrant
• Has superuser privileges
• Logs in with a private key
Stored in hidden .vagrant directory
29
Study Guide | Learning Vagrant
VM Management
30
Study Guide | Learning Vagrant
Snapshots
31
Study Guide | Learning Vagrant
32
Study Guide | Learning Vagrant
Basic Syncing
Options
• create (boolean): Creates the host path if it does not exist
• group (string): Set group owner of the guest directory
• owner (string): Set owner of the guest directory
• mount_options (array): Any additional mount options we wish to pass
• id (string): The name of the mount point (what shows up when running mount on the guest)
• disabled (boolean): Added to the end of the initial config.vm.synced_folder line; prevents file syncing
33
Study Guide | Learning Vagrant
Example Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.synced_folder "app/", "/var/www/wanderer",
create: true, group: "vagrant", owner: "vagrant", id: "wanderer-app"
end
34
Study Guide | Learning Vagrant
NFS
• Declare the file syncing solution by adding the type option to the config.vm.synced_folder configuration:
• config.vm.synced_folder "app/", "/var/www/app", type: "nfs"
• Host must have NFS installed:
• Host:
Debian: nfs-kernel-server
RedHat: nfs-utils
Mac: Preinstalled
Windows: Add vagrant-winnfsd plugin
• Vagrant can install NFS on some guests, but it may need to be installed
• Cannot use owner / group options
• A static network must be added:
• config.vm.network "private_network", type: "dhcp"
• Additional options:
• nfs_export (boolean): Will assume /etc/export has been updated manually
• nfs_udp (boolean): Determine whether or not to use UPD as the transfer method
• nfs_version (string or integer): The desired NFS protocol
35
Study Guide | Learning Vagrant
RSync
36
Study Guide | Learning Vagrant
37
Study Guide | Learning Vagrant
SMB
38
Study Guide | Learning Vagrant
Port Management
• config.vm.network option
• "forwarded_port" setting
• Options:
• auto_correct (boolean): Automatically change port if clashing with an existing port
• guest (int): The port on the guest to expose on the host
• guest_ip (string): The guest IP to which to bind
• host (int): The host IP to use; must be greater than 1024
• host_ip (string): The host IP to which to bind
• protocol (string): UDP or TCP
A rule must be added for each protocol
• id (string): Name to call this networking rules
• View mapped ports:
• vagrant port
39
Study Guide | Learning Vagrant
• To add multiple machines, create a " Vagrantfile within a Vagrantfile using config.vm.define " method block:
• The name defined is the name Vagrant will use for the machine:
• Used for vagrant commands; i.e., vagrant halt app will halt only the app machine
40
Study Guide | Learning Vagrant
• Place configuration blocks in the method for each machine, replacing config with the name assigned to the
machine:
• Anything set with config outside of the block is applied to all machines:
• Code outside of the block is configured first
41
Study Guide | Learning Vagrant
Private Networking
• Access guest machine at IP address is available only from within the network
• Vagrant configures this automatically based on the Vagrantfile for most systems:
• Exceptionally old/new systems may not have support
• Configure DHCP:
• config.vm.network "private_network", type: "dhcp"
• IP address automatically assigned:
Use auto_config: false to disable
Use ifconfig /related command to discover private IP on guest
• Configure a static IP:
• config.vm.network "private_network", ip: "192.168.50.4"
• User must ensure IP is not already used
• No limitations, but should be in reserved private address space:
10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255
IPv6 supported, but not over DHCP:
Uses same ip option
Use auto_config: false to disablelso set the netmask with netmask (default: 64)
42
Study Guide | Learning Vagrant
Public Networking
43
Study Guide | Learning Vagrant
Provisioning Overview
44
Study Guide | Learning Vagrant
Shell
• Embedded:
$script = <<-SCRIPT
apt-get install nodejs npm -y
useradd --no-create-home --shell /bin/false node_exporter
...
SCRIPT
...
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: $script
end
• Path:
• config.vm.provision "shell", path: "script.sh"
• Useful options:
• privileged (boolean): Run script with elevated privileges; defaults to true
45
Study Guide | Learning Vagrant
46
Study Guide | Learning Vagrant
File
47
Study Guide | Learning Vagrant
Ansible
• Two options:
• Ansible
• Ansible Local
Ansible Provisioner
• Requires Ansible to be downloaded on the workstation
• Essentially, Vagrant uses Ansible as you would normally use Ansible
• Up and running:
• Options:
• ask_become_pass (boolean): Require Ansible to ask for a password prompt when becoming another user or
using sudo
• ask_vault_pass (boolean): Require Ansible to ask for a vault password
• force_remote_user (boolean): When true (default), Vagrant uses the ansible_ssh_user in the inventory as
the Ansible remote user; when set to false, the vagrant user is used
• host_key_checking (boolean): Require SSH host key checking
• raw_ssh_arg (array of strings): Pass in OpenSSH client options
48
Study Guide | Learning Vagrant
Ansible Local
• Does not require Ansible on the host system
• Up and running:
• Options:
• install (boolean): When set to true (default), attempt to install Ansible on guest system
• install_mode ( default , pip , or pip_args_only ): Determine the installation method; default is the
package manager for the guest
• pip_args (string): Additional arguments if install_mode is set to use pip
• provisioning_path (string): The absolute path on the guest where Ansible files are stored
• tmp_path : The absolute path on the guest where temporary files generated by Ansible Local are stored
Ansible Common
• Additional options for both provisioners, changes the options passed to the ansible-playbook command
• Selected options:
• become (boolean): Run commands as the become_user
• become_user (string): The user to run commands as when become is set to true
• config_file (string): Set Ansible configuration file location
• extra_vars (string or hash): Pass in additional variables (path to JSON/YAML file or hash)
• groups : Set inventory groups for inventory file
49
Study Guide | Learning Vagrant
50
Study Guide | Learning Vagrant
Chef
Chef Solo
• Uses chef-solo , which executes the Chef client without a Chef server
• Ideal for Chef new people and experts alike
• Up and running:
• Options:
• cookbooks_path (string or array): The location of cookbooks
• data_bags_path (string or array): The location of data bags
• environments_path (string): The location of environment definitions
• nodes_path (string or array): The location of JSON node objects
• recipe_url (string): The URL of a cookbook archive
51
Study Guide | Learning Vagrant
Chef Zero
• Uses chef-zero to run in "local mode"
• chef-zero itself acts as a lightweight version of the Chef Infra Server
• Middle ground between Chef Solo and full Chef server
• Up and running:
• Options:
• cookbooks_path (string or array): The location of cookbooks
• data_bags_path (string or array): The location of data bags
• environments_path (string): The location of environment definitions
• nodes_path (string or array): The location of JSON node objects
• recipe_url (string): The URL of a cookbook archive
• roles_path (string or array): The location of role definitions
• synced_folder_type (string): The type of synced folder protocol to use to share provisioner data:
Uses the default synced folder type
52
Study Guide | Learning Vagrant
Chef Client
• Use Chef with a Chef Infra Server
• Up and running:
• Options:
• client_key_path : Specify the path to the client key
• validation_client_name : The name of the chef-validator key
Chef Apply
• Use chef-apply , which runs a single recipe from the command line
• Up and running:
• Options:
• recipe (string): The raw recipe contents
• upload_path (string): The location on the guest in which the generated recipe file is stored; defaults to /tmp/
vagrant-chef-apply-*
53
Study Guide | Learning Vagrant
54
Study Guide | Learning Vagrant
Puppet
Puppet Apply
• Use Puppet without a Puppet Master
• Automatically looks for default manifest as manifests/default.rb
• Up and running:
config.vm.provision "puppet"
• Set modules:
• Selected options:
• facter (hash): Set Facter facts
• hiera_config_path (string): Location of the Hiera configuration of the host
• manifest_file (string): The default manifest file
• manifests_path (string): Location of the manifests directory on the host
55
Study Guide | Learning Vagrant
Puppet Agent
• Connect to a Puppet master
• Up and running:
• Options:
• binary_path (string): Location of Puppet binary on guest
• client_cert_path (string): Location of client certification on host
• client_private_key_path (string): Location of client private key on host
• facter (hash): Additional Facter facts
• options (string or array): Additional command line options to pass to the puppet agent command
• puppet_node (string): Set the node name (Defaults to the hostname)
• puppet_server (string): Hostname of the Puppet server (Defaults to puppet )
56
Study Guide | Learning Vagrant
Salt
• Selected options:
• Installation:
install_master (boolean): Install master on guest (not supported on Windows guests)
no_minion (boolean): Do not install minion (not supported on Windows guests)
install_syndic (boolean): Install salt-syndic (not supported on Windows guests)
version (string): Version of Salt to install
• Minion options:
minion_config (string): Path to minion configuration (defaults to salt/minion on the host)
minion_key (string): Path to minion key (defaults to salt/key/minion.key )
57
Study Guide | Learning Vagrant
58
Study Guide | Learning Vagrant
Docker
• Unlike the Docker provider, the Docker provisioner installs Docker on a guest VM and runs Docker images on that
guest
• Can be run with no options (this will install Docker)
• Up and running:
• config.vm.provision "docker"
• Options:
• images (array): Images to pull via docker pull
• pull_images (array): Images to pull via docker pull
• post_install_provisioner (method): An additional provisioner block to run after Docker installation
• run : Run a container and configure it to start on boot (can only be specified once):
image (string: Image to run
cmd (string: Command to start within the container
args (string): Additional arguments to pass in the docker run command
daemonize (boolean): Run Docker as a daemon (defaults to true )
auto_assign_name (boolean): Automatically assign the name of the container based on the first argument
of the run
restart : Container restart policy (; defaults to always )
• build_image (string): Location on guest of Dockerfile to build image based on
args (string): Additional arguments to call to build_image
59
Study Guide | Learning Vagrant
Box Overview
60
Study Guide | Learning Vagrant
61
Study Guide | Learning Vagrant
• Take an existing Vagrant box that has been changed and repackage it as a new box
• Package existing guest:
• vagrant package
• Options:
Name of file to output:
--output name.box
Include metadata files:
--include files
Include Vagrantfile:
--vagrantfile file
62
Study Guide | Learning Vagrant
63
Study Guide | Learning Vagrant
64
Study Guide | Learning Vagrant
Share Overview
65
Study Guide | Learning Vagrant
HTTP Sharing
66
Study Guide | Learning Vagrant
SSH Sharing
67
Study Guide | Learning Vagrant
Vagrant Connect
68
Study Guide | Learning Vagrant
Windows Considerations
69
Study Guide | Learning Vagrant
70