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.
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.
Vagrant boxes are packages that, similar to Vagrantfiles, can be shared and used to replicate virtual environments. Vagrant boxes can be easily downloaded by running the
vagrant box add command. The Vagrant cloud offers an easily searchable catalogue of boxes. The Vagrant cloud provides lots of information about a box, such as the creator, the version, how many times its been downloaded, and a brief description.
Vagrant supports three main types of networking when creating virtual environments: public networks, private networks, and port-forwarding. The simplest networking option is port-forwarding, which allows you to access a specific port through the guest operating system into the Vagrant machine. Public and private networking are more complex and offer more configuration, but we will cover that in future chapters.
Provisioning in Vagrant offers you a way to configure the Vagrant machine even more. You can install software and dependencies as the machine is being created. To provision a Vagrant machine, you can use shell scripting, Docker, Chef, Ansible, and other configuration-management software, such as Puppet.
Vagrant allows you to easily package up a virtual environment that can be shared among fellow developers. This packaged virtual environment is often referred to as a Vagrant box. A box can be configured to mirror the production environment where your web application or code will be running. This can help minimize any bugs or issues when your application/code is deployed to the production environment.
The beauty of Vagrant's configuration (known as a Vagrantfile) is often small and can be easily edited and tested. The syntax of a Vagrantfile is easy to understand and offers a simple way to build a complex environment.
Vagrant can be used by many different members of a team, including those on the development team, the operations team, and the design team.
For a developer, Vagrant can allow them to package up their code/application into an easily-sharable fully-fledged development environment. This can then be used by developers using different operating systems, such as macOS, Linux, or Windows.
The operations team can easily and quickly test deployment tools and scripts using Vagrant. Vagrant supports many popular deployment tools in the operations/DevOps world, such as Puppet, Docker, and Chef. Vagrant can be a cheaper and faster way to test deployment scripts and infrastructure topologies. Everything can be done locally with Vagrant or it can be used with a service such as Amazon Web Services.
Vagrant allows the development team and operations team to create virtual environments running code, and applications ready for a designer to easily run this environment on their machine and start making edits to the application. There is no configuration required and feedback can be instant, from when a developer makes a change or a developer has to update the Vagrantfile.
VirtualBox is one of the many providers that Vagrant supports. VirtualBox is a powerful virtualization tool that allows you to create virtual environments on your existing operating system. It allows you to fully customize a virtual machine's hardware, such as the RAM, CPU, hard drive, audio, and graphics.
VirtualBox was initially released in January 2007 by the company Innotek GmbH, which was later acquired by Sun Microsystems, which, in turn, was acquired by the Oracle Corporation. Oracle is actively maintaining and releasing new versions of VirtualBox.
VirtualBox is built in x86 Assembly, C++, and C. It can run and supports many different operating systems, such as Windows, Linux, Solaris, and OS X.
DevOps is a popular term in the IT world at the moment. There are many different opinions as to what DevOps actually is. In simple terms, DevOps is the mix of development and operations. It is essentially creating a sort of "hybrid programmer" who knows about operations and infrastructure, or a system admin who understands programming and can develop applications.
DevOps is a mixture of methodologies, practices, philosophies, and software. DevOps streamlines the whole project life cycle by creating a workflow that works for all departments. There are no rules or laws in DevOps, but generally it's the process of connecting the developers and the infrastructure team by enabling an easy way to develop and ship code.
The beauty of DevOps is that any company can start following its ideas, methodologies, and best practices. Large companies may have a whole DevOps department/team, whereas smaller companies may just need one or two dedicated DevOps employees. In a start-up scenario, where money must be carefully budgeted, one employee may take on the role of developer and also DevOps.
In this chapter, you will learn about the current state of development in DevOps, how Vagrant fits into DevOps, and how to use Vagrant as a day-to-day DevOps tool. By the end of this chapter, you will have a much better understanding of how Vagrant can be used for development as part of the DevOps process.
As mentioned previously, DevOps is a mixture of software development, operations/system administration, and testing/quality assurance. DevOps is not a new movement, but one that doesn't necessarily have a leader or a set of rules and standards to follow. Every company has their own idea of what DevOps is and how it should be implemented. Many follow similar paths or rough guidelines. Due to the lack of governance with DevOps, the current state of development is varied.
Traditionally, development has always been separate to the operations and server team, but in the last few years, we have seen many DevOps tools bridge that gap and make life easier for both sides.
In the past, when a web developer would build a web application, they would code it, built it locally on their machine, and then FTP (file transfer) the files onto a live (production) server to then run the code—if there were any issues or bugs, the developer would have to make changes to the server environment and debug the code. There are many developers who still use this workflow and it may be because of their environment or because they have no choice in the matter.
Today, a modern web developer's workflow may look like this:
- The developer writes their code locally but through a virtual environment/machine with a tool such as Vagrant. This allows the developer to set up an environment such as the production one.
- The developer edits to their code and uses version-control (such as Git or Subversion) to manage changes. The version-control is set up in a way that allows the developer to keep test/new code separate from the production code.
- A continuous integration (CI) tool (such as Jenkins or Travis CI) is used to create a pipeline that often has three separate stages—development, staging, and production. The CI tool can be used to run tests against the software, and run scripts such as performing assets by combining and minifying them. The version-control software can be linked into the CI tool, which often triggers these builds and tests. When the developer pushes some new code to the staging environment, tests can be run before it reaches the production environment.
- Often, if the tests run and there are no issues, the code may be pushed directly into the production branch in the version control. At this point, the CI tool may trigger a new build, which would essentially restart the service that the code applies to. This could be simple or complex, depending on the production environment and software architecture.
- At some stages during this process, there may be manual intervention by the QA (quality assurance/testing) team or more senior developers who wish to check the code before it goes to production.
Of course, this is just an example workflow and will differ between companies and development teams. The modern workflow may seem much more complicated and tedious, but this is for good measure. At each stage, you'll notice there are checks and tests run before the code can reach a live production environment where real users may be interacting with that code. This can be incredibly important when working with financial software and other business-critical software. This modern workflow greatly reduces the margin of error.
Modern development in the DevOps world is focused on speed and automation. The focus on speed is the ability to quickly build a feature or fix a bug and "push the code to production" (a phrase you might have heard!). This means that single developers or a team of developers have less of a barrier when working on the code. A developer shouldn't worry about configuring servers or environments.
Automation is a big part of DevOps, and that effects the development part too. You can imagine how slow a process it would be if a developer made changes to their code and then had to wait for a member of the operations team to manually run tests and scripts against their changes before letting them know the result.
I believe that Vagrant is a key tool in a developer's toolbox in today's DevOps-focused world. Vagrant is essentially a suite of tools that allows the developer to create code but also connect with configuration management tools, such as Puppet, Chef, and Ansible, that are used to automate workflows and environments on servers.
Vagrant's primary focus is on development and enables an easy way for every developer on the team to use the same environment. Within a Vagrant environment, you can run version-control that could link into the CI workflow, which allows you to run tests and move code into different stages.
Vagrant is a flexible tool that enhances day-to-day development by allowing you to easily test out DevOps workflow ideas. It allows you to separate your software code and infrastructure without having to know much about DevOps, infrastructure, servers, and configuration-management tools.
As a day-to-day DevOps tool, Vagrant can be used for many things, including the following:
- Testing software code in different environments and operating systems
- Testing different workflows using configuration-management tools, such as Chef and Puppet
- Working in the same environment as other developers in your team/company
- Easily make changes to Vagrant and see the results instantly
- Running multiple environments/virtual environments to test out networking, file-sharing, and other multi-server use cases
In this chapter, we learned that Vagrant is a very powerful and flexible tool for helping create virtual environments that can mimic staging and development environments used by your business or application. We looked at the current development state of DevOps, how Vagrant fits into that, and how to use Vagrant as a day-to-day development tool.
In Chapter 2, Installing VirtualBox and Vagrant, we will install Vagrant and its provider, VirtualBox. We will look at how to install these pieces of software on a Windows, Mac, and Linux machine. You will also learn how to find out your system's version and its CPU architecture.