32,36 €
Use Vagrant to easily build complete development environments
Key Features
Book Description
Hands-On DevOps with Vagrant teaches you how to use Vagrant as a powerful DevOps tool and gives an overview of how it fits into the DevOps landscape. You will learn how to install VirtualBox and Vagrant in Windows, macOS, and Linux. You will then move on to understanding Vagrant commands, discovering its boxes and Vagrant Cloud.
After getting to grips with the basics, the next set of chapters helps you to understand how to configure Vagrant, along with networking. You will explore multimachine, followed by studying how to create multiple environments and the communication between them. In addition to this, you will cover concepts such as Vagrant plugins and file syncing.
The last set of chapters provides insights into provisioning shell scripts, also guiding you in how to use Vagrant with configuration management tools such as Chef, Ansible, Docker, Puppet, and Salt.
By the end of this book, you will have grasped Vagrant's features and how to use them for your benefit with the help of tips and tricks.
What you will learn
Who this book is for
Hands-On DevOps with Vagrant is for you if you are a system administrator, DevOps engineer, DevOps architect, or any stakeholder working with DevOps and wanting to explore Vagrant. Experience in system administration is needed to enjoy this book.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 235
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Gebin GeorgeAcquisition Editor: Rohit RajkumarContent Development Editor: Dattatraya MoreTechnical Editor: Sayali Thanekar, Cymon Pereira, Nirbhaya ShajiCopy Editor: Safis EditingProject Coordinator: Kinjal BariProofreader: Safis EditingIndexer: Priyanka DhadkeGraphics: Jisha ChirayilProduction Coordinator: Jyoti Chauhan
First published: October 2018
Production reference: 1151018
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78913-805-4
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Alex Braunton is a web developer focusing on the LAMP stack by day and a technical tinkerer by night. He is passionate about all technological things and enjoys trying to build robots and home automation systems with his Raspberry Pi collection. Currently, he is focusing on sharpening his DevOps knowledge and experimenting with a range of technologies, such as serverless, virtual reality, and GraphQL. He also has a growing bonsai collection and constantly bores his wife and family about the art and history of bonsai.
Michał Wołonkiewicz started by participating in the home meteo station network (involving an unbreakable DS1820 connected by a 1-Wire converter to an RS-232 interface in an Optiplex G1, operating under the control of OpenBSD) before he even got his driving license. He gained his first commercial experience as a systems engineer in both the public and private sectors, he improved the infrastructure at an investment bank and taught developers how to use it. He runs a consulting company with the goal of providing expertise on IT, Telco, and security technologies. He can be reached at [email protected].
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Hands-On DevOps with Vagrant
Dedication
Packt Upsell
Why subscribe?
Packt.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Introduction
Getting started with Vagrant and DevOps
Understanding Vagrant
Vagrant features
Vagrantfile
Boxes
Networking
Provisioning
Plugins
Advantages of Vagrant
Development team
Operations team
Design team
What is VirtualBox?
What is DevOps?
Vagrant for DevOps
Current state of development within DevOps
Vagrant and DevOps
Using Vagrant as a day-to-day DevOps tool
Summary
Installing VirtualBox and Vagrant
Installing VirtualBox and Vagrant on Windows
Prerequisites
System version
CPU architecture
Installing VirtualBox on Windows 10
Installing Vagrant on Windows 10
Installing VirtualBox and Vagrant on Linux
Prerequisites
System version
CPU architecture
Installing VirtualBox on Ubuntu 16.04
Installing Vagrant on Ubuntu 16.04
Installing VirtualBox and Vagrant on macOS
Prerequisites
System version
CPU architecture
Installing VirtualBox on Mac OS 10.11.3
Installing Vagrant on macOS 10.13.3
Summary
Command Line-Interface - Vagrant Commands
Vagrant command overview
Vagrant commands in depth
A brief note on formatting commands
General Vagrant commands and sub-commands
The list-commands command
Options/flags
The help command
The version command
Options/flags
The global-status command
Options/flags
Vagrant's configuration commands and sub-commands
The login command
Options/flags
The package command
Options/flags
The snapshot command
sub-commands
The provider command
Options/flags
The plugin command
sub-commands
The cap command
Options/flags
Day-to-day Vagrant commands and sub-commands
The box command
sub-commands
The destroy command
Options/flags
The halt command
Options/flags
The init command
Options/flags
The port command
Options/flags
The provision command
Options/flags
The push command
Options/flags
The reload command
Options/flags
The resume command
Options/flags
The status command
Options/flags
The suspend command
Options/flags
The up command
Options/flags
The validate command
Options/flags
Application-specific Vagrant commands and sub-commands
The docker-exec command
Options / flags
The docker-logs command
Options/flags
The docker-run command
Options/flags
The rdp command
Options/flags
The rsync command
Options/flags
The rsync-auto command
Options/flags
The ssh command
Options/flags
The ssh-config command
Options/flags
The powershell command
Options /flags
A typical Vagrant workflow using commands
Troubleshooting
Summary
Discovering Vagrant Boxes - Vagrant Cloud
Understanding Vagrant boxes
Vagrant box file anatomy
Box file
Box metadata
Box information
How to install a Vagrant box
Direct URL to box file
Shorthand/alias to box file
A file path or URL to a box in a specific catalog
How to delete a Vagrant box
Deleting a specific version of a box
Deleting all versions of a box
Box versioning
Vagrant Cloud
Understanding the Vagrant Cloud
Vagrant Cloud website
Installing a Vagrant box found on the Vagrant Cloud – Part 1, Search
Installing a Vagrant box found on the Vagrant Cloud – Part 2, Install
Uploading a Vagrant box to the Vagrant cloud
Creating a Vagrant box
Enterprise solutions for Vagrant boxes
Summary
Configuring Vagrant Using a Vagrantfile
Understanding Vagrantfiles
Creating a Vagrantfile
Vagrantfile syntax
Vagrantfile options
Vagrant machine configuration (config.vm)
Vagrant SSH configuration (config.ssh)
Vagrant settings (config.vagrant)
Other Vagrantfile settings
WinRM settings (config.winrm)
WinSSH settings (config.ssh and config.winssh)
Troubleshooting a Vagrantfile
Summary
Networking in Vagrant
Port-forwarding
Port-forwarding notes
Private networking
DHCP
Static IP
IPv6
Public networking
DHCP
Static IP
Network bridge
Summary
Multi-Machine
An introduction to Vagrant multi-machine
Load balancing with Vagrant multi-machine
lb.sh
web.sh
Vagrant multi-machine shell provisioning
multi-machine SSH
Web server and database setup with Vagrant multi-machine
web.sh
db.sh
Nginx and PHP configuration
MySQL configuration
Summary
Exploring Vagrant Plugins and Syncing Files
Understanding Vagrant plugins
The anatomy of a Vagrant plugin
Gem
bundler
Managing Vagrant plugins
Vagrant plugin installation methods
Installing a Vagrant plugin from a local file
Installing a Vagrant plugin from a known gem source
Vagrant plugin commands and subcommands
Finding, installing, and using a Vagrant plugin
Installing a Vagrant plugin
Uninstalling a Vagrant plugin
Vagrant file-syncing
Setting up synced folders
Synced folders with basic usage
Synced folders with RSync
Synced folders with NFS
Summary
Shell Scripts - Provisioning
Introduction to Vagrant provisioning
Understanding configuration management
Basic usage of Vagrant provisioning
Vagrant provisioning commands
Vagrant provisioning with a file
Single file
Directory
Vagrant Shell provisioner
Inline Scripts
External scripts
Script arguments
Script argument – string
Script argument – array
Summary
Ansible - Using Ansible to Provision a Vagrant Box
Understanding Ansible
Installing Ansible
Installing Ansible on macOS High Sierra (version 10.13)
Provisioning Vagrant using Ansible
Provisioning Vagrant using Ansible on the host machine
Provisioning Vagrant using Ansible on the guest machine
Additional Ansible options
Provisioner – Ansible
Provisioner – Ansible local
Ansible Playbooks
Summary
Chef - Using Chef to Provision a Vagrant Box
Understanding Chef
Chef Cookbook
Recipes
Templates
Attribute values
Extensions
File distributors
Chef Supermarket
Search
Provisioning Vagrant with Chef
Installing Chef on macOS
Using Chef Solo to provision a Vagrant machine
Using Chef Client to provision a Vagrant machine
Summary
Docker - Using Docker with Vagrant
Understanding Docker
Key components of Docker
Containers
Images
Registry
Service
Using the Docker Hub to find Docker images
Basic usage – running a container
pull
run
stop
start
search
Using Docker to provision a Vagrant machine
Docker-specific configuration in Vagrant
Images
build_image
args
pull_images
run
image
cmd
args
auto_assign_name
deamonize
restart
post_install_provisioner
Summary
Puppet - Using Puppet to Provision a Vagrant Box
Understanding Puppet
Resources
Manifest
Compile
Catalogs
Apply
Desired state
Puppet apply and Puppet agent
Puppet apply
Options
Puppet agent
Options
Puppet Manifest example and syntax
Syntax
Provisioning with Puppet
Provisioning with Puppet apply
Provisioning with Puppet agent
Summary
Salt - Using Salt to Provision a Vagrant Box
Understanding Salt
Salt Master
Salt Minion
Modules
Execution
State
Grains
Renderer
Returners
Runners
Salt states
Syntax and example
Provisioning Vagrant with Salt
Salt options available within Vagrant
Install options
Minion options
Master options
Execute states
Execute runners
Output control
Vagrant cheat sheet
Testing a Vagrantfile
Saving a snapshot
Status
Boxes
Hardware specification
Code deployment
Multi-machine
General
Summary
Other Book You May Enjoy
Leave a review - let other readers know what you think
Vagrant is an open source tool that allows you to programatically create and manage virtual environments. Vagrant's main focus is on creating development environments that can be shared between teams all over the world. It removes the "works fine on my machine" problem and allows anyone with the Vagrantfile configuration to create an exact copy of the original machine.
Vagrant was created and is maintained by Mitchell Hashimoto and HashiCorp with a continuous stream of support and updates. It's a piece of software that has been going from strength to strength since its creation in 2010.
In this book, we'll cover many aspects of Vagrant. The book can be used by beginners who have very little or no experience with Vagrant. We'll cover how to install Vagrant and all of the basic knowledge needed to get up and running.
This book can also be used by more advanced users who wish to better understand and utilize Vagrant. We'll cover the available commands, networking, multi-machine, and provisioning with configuration management tools such as Chef and Ansible.
Whatever level you are at, this book will teach you something new or help reinforce your knowledge and offer tips and tricks.
Chapter 1, Introduction, acts as a great introduction to the world of Vagrant. It will help create a foundation of knowledge to guide you through the book. You will learn what Vagrant is, the benefits of Vagrant, what VirtualBox is, and what DevOps is. You'll also learn how Vagrant fits into the DevOps landscape, how it can be used as a DevOps tool, and take a look at other pieces of software.
Chapter 2, Installing VirtualBox and Vagrant, Windows, macOS, and Linux, gets your hands dirty by teaching you how to install VirtualBox and Vagrant. We'll cover the three main operating systems: Windows, macOS, and Linux. You'll learn how to navigate both websites (https://www.virtualbox.org and https://www.vagrantup.com) to download, install, and verify the software, once it has been installed.
Chapter 3, Command Line-Interface - Vagrant Commands, teaches you about the range of useful commands that Vagrant provides. You'll learn about all of the available commands and sub-commands. You'll also learn about the structure of Vagrant commands, how to use the help command to get more information, and a brief description of what each one does. You will feel confident in managing Vagrant via the command line by the end of this chapter.
Chapter 4, Discovering Vagrant Boxes - Vagrant Cloud, covers all aspects of Vagrant boxes. We will look at how to manage them: installation, deletion, and versioning. We will also create a base box that has the minimum requirements for building a Vagrant environment. In this chapter, we will also cover Vagrant Cloud and what it offers you. Vagrant Cloud is a searchable index of Vagrant boxes that are ready to download. We'll cover a range of things, such as how to use the the Vagrant Cloud website, how to search for a specific box, and how to install that box.
Chapter 5, Configuring Vagrant Using a Vagrantfile, explores the Vagrantfile, which allows you to easily customize your Vagrant machine. The Vagrantfile offers many different configuration options, such as networking, folder syncing, the multi-machine option, provisioning, and provider-specific settings. You'll also learn the syntax and formatting of a Vagrantfile and how to validate it, once created.
Chapter 6, Networking in Vagrant, explains how networking in Vagrant is easily configurable and can be used to create some powerful setups. There are three key networking configuration options that you will learn about in this chapter: port forwarding, public networking, and private networking. You'll learn how to use each one through examples and view the benefits of each.
Chapter 7, Multi-Machine, looks at the multi-machine option, which allows you to create multiple Vagrant machines and manage/configure them using a single Vagrantfile. You will create a multi-machine environment that mimics a real-world scenario. You will create one machine that runs a web server and another that runs a database. These machines will communicate using a networking configuration. This will give you a solid foundation and help you to start creating powerful environments using the multi-machine option.
Chapter 8, Exploring Vagrant Plugins and Syncing Files, gets into how, although Vagrant offers many features, there may be a specific use case where you require some additional functionality. In this chapter, you will learn all about Vagrant plugins. You will see how easy it is to install and use Vagrant plugins. There are a range of commands and sub-commands to learn about, too. In this chapter, you will also learn about syncing files with Vagrant and the different configuration options available.
Chapter 9, Shell Scripts - Provisioning, deals with provisioning in Vagrant, which is another powerful Vagrant feature, giving you the ability to easily provision your Vagrant machines. This chapter acts as an introduction to provisioning and will teach you more about configuration management tools, shell provisioning, and file provisioning. There are multiple configuration options available when using these types of provisioning to learn about, too.
Chapter 10, Ansible - Using Ansible to Provision a Vagrant Box, teaches you how to provision a Vagrant environment using Ansible and Ansible playbooks. You will also briefly learn how to install Ansible on your Vagrant machine, before learning how to use Ansible on the host machine to provision the Vagrant box.
Chapter 11, Chef - Using Chef to Provision a Vagrant Box, teaches you how to provision a Vagrant environment using Chef and Chef cookbooks. You will look at provisioning the machine using the basic option, Chef Solo, and the advanced option, Chef Client.
Chapter 12, Docker - Using Docker with Vagrant, delves into how to provision a Vagrant environment using Docker. We'll look at searching and pulling images from the Docker Hub and then running them as containers. We'll also look at the different options Docker accepts when we're using it as a Vagrant provisioner.
Chapter 13, Puppet - Using Puppet to Provision a Vagrant Box, explores how to provision a Vagrant environment using Puppet. You will learn about the two main options available with Vagrant: Puppet Apply and Puppet Agent. Using Puppet Agent, you will see how to connect to a Puppet master and retrieve instructions from that.
Chapter 14, Salt - Using Salt to Provision a Vagrant Box, tackles how to provision a Vagrant environment using Salt. You will also learn about Salt states, which allow us to dictate which packages and services should be added into the provisioning.
This book is aimed at both beginners and advanced users. It will teach you how to install the required software. If you already have this software, please check the versions that you have as there may be differences between the version that you have and the version that we use in the book. You may need to upgrade your software. You will need:
VirtualBox version:
5.2.10
Vagrant version:
2.0.4
Ubuntu box (from Vagrant cloud) version:
ubuntu/xenial64 20180510.0.0
It's worth reading through each chapter a few times so you don't miss anything. If you need more information or clarification, the official Vagrant website documentation is fantastic.
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Hands-On-DevOps-with-Vagrant. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it https://www.packtpub.com/sites/default/files/downloads/9781789138054_ColorImages.pdf.
Feedback from our readers is always welcome.
General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.
You are about to embark on an exciting journey focused on Vagrant and its role within DevOps. Throughout these chapters, you will learn interesting and useful facts, as well as tips and tricks, about Vagrant. Initially, we will focus on the basics of Vagrant and getting it installed and running on your machine. We will then venture through its ins and outs, by focusing on the important parts of Vagrant, such as its commands, networking, multi-machine, Vagrantfiles, and using configuration management tools, such as Chef, Docker, and Ansible. By the end of this book, you will have solid foundational knowledge about Vagrant and the necessary skill set to start using it on a day-to-day basis as part of your DevOps workflow.
In this chapter, we will create a solid foundation that will help you understand what Vagrant is, what VirtualBox is, and how Vagrant ties into the DevOps landscape. We will learn about the current state of development tools in DevOps and focus on how Vagrant can be used by many different teams in an organisation—not just developers! By the end of this chapter, you will have a good understanding of the basics of Vagrant, VirtualBox, and DevOps.
In this section, you will be introduced to Vagrant and learn about its features, benefits, and its role in the development tools used in the DevOps world.
Vagrant is very simple on the surface, but is actually incredibly complex under the hood. It allows you to quickly and effortlessly create virtual environments (known as Vagrant boxes) and customize them. Vagrant easily integrates with multiple providers, such as VirtualBox, VMware, and Docker. These providers actually power the virtual environments, but Vagrant provides a customizable API to that virtual machine.
Vagrant has a large selection of commands, which can be used from the command line/Terminal to manage virtual environments. These commands can quickly download and set up an environment from the Vagrant cloud, which hosts many popular environments, such as Ubuntu or PHP's Laravel.
Vagrant is an important piece of software that can be found in many programmers' toolboxes. It is commonly used to tackle the well-known phrase, It works on my machine, by allowing everyone to have a copy of the same environment.
Vagrant was created by Mitchell Hashimoto and released in March 2010. Vagrant is now part of the HashiCorp company, which Mitchell Hashimoto cofounded in 2012 with Armon Dadgar. Vagrant is an open source piece of software that has been built in the Ruby language. It is currently being licensed under the MIT license. Vagrant can be run on macOS, Windows, FreeBSD, and Linux.
Vagrant is essentially another layer in the virtualization stack. It acts as an easily programmable interface to control virtual environments. Vagrant relies on a provider, such as VirtualBox, to power these environments, but it can also configure providers so they work in harmony – an example would be Vagrant controlling how much memory (RAM) an environment has.
Vagrant offers many features to help you build and configure virtual environments. Vagrant features can be split into a few key areas—Vagrantfile, boxes, networking, provisioning, and plugins. Vagrant can be managed in two key ways – the command line and a Vagrantfile. The command-line approach is often used for admin tasks, such as downloading/importing a new Vagrant box or deleting an old one.
A Vagrantfile is a configuration file that uses the Ruby programming language syntax. It is easy to understand and can be quickly tested by making a change and then running the vagrant up command to see whether the expected results happen. A Vagrantfile can easily be shared and added into version control. It's lightweight and contains everything needed for another user to replicate your virtual environment/application.
