Intro To Vagrant Software
Virtualization has become very important for every developer because virtualization makes it possible to create a complex virtual environment in one's computer. Vagrant, by Hashicorp, is a free software used to create these virtual environments.
Configuring environments by learning Vagrant is quite easy. Vagrant provides a configuration language called HCL (Hashicorp Configuration Language), which is used for defining the virtual environment we intend to create. Vagrant provides the configuration to a hypervisor. By default, Vagrant uses Oracle VirtualBox, but it is possible to configure it to use another provider, like VMWare, Docker, or Hyper-V.
Why choose Vagrant
Vagrant offers a reliable and easy way to configure and share work environments created with Vagrant. To better understand how to Vagrant can fit in our daily jobs:
- Developer: If you are a developer, you know how time-consuming it is to create a new work environment and isolate the dependencies across the different environments. With Vagrant, one can easily create a different environment and spin it up and down when needed. All we need is a Vagrantfile for bringing up and down the environment.
- Architect: From an Architect's point of view, it is easy to create and prototype new architecture and the related environment. All you need is to create the Vagrantfile. When you are comfortable with the environment, just share it with the Developer team.
- Site Reliability Engineer/DevOps Engineer: If you are an SRE (Site Reliability Engineer) or DevOps Engineer, with Vagrant, you can build a Pipeline for testing and faster release of the code into production. This is because you can easily test the configuration and release it in production.
The Vagrantfile is the file used to configure the environment one intends to create. With Vagrant, it is possible to create a basic configuration file using the option init. Into the Vagrantfile, we input all the definitions and information we need to create the environment. A basic Vagrantfile look like this:
Vagrant.configure("2") do |config| config.vm.box = "hashicorp/bionic64" end
To write a simple Vagrantfile, using Ruby, the configuration starts with the code lines::
Vagrant.configure(“2”) do | config | …. End
These lines are used to configure the object config, the number "2" indicating the version of the object we want to use. The configuration is made in the block do...end. In this case, we configure the VM and the image we want to install. For this example, we'll use a Hashicorp/bionic64 image. The Vagrantfile can be generated with the command:
When we use this command, we generate a Vagrantfile in the same folder where we execute the command. With the vagrant file generated, we can execute the VM using the command:
The command vagrant up runs the Virtual Machine in the Hypervisor, configured to allow us to interact with the Virtual Machine.
In Vagrant, a "Box" is essentially a package that can be used to create a Vagrant environment. The best way to check and search for the box is to use the public image build by Hashicorp. The public image is available at this link:
At this link, it is possible to check the public image and choose the image based on the provider installed on the local machine. The boxes are crucial to creating the environment and can be used as a template to create one's box.
Create our Development environment
One of the most important use cases of Vagrant is creating a virtual environment for development. In order to create a new environment, what we need to do is create a new Vagrantfile based on an existing Box. The first step is to update an existing image and execute the command we need., The Vagrantfile is like this:
**Vagrant.configure("2") do |config| config.vm.box = "precise64"
**config.vm.box_url = "http
config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install python-dev python-pip -q -y SHELL end
The section config.vm.provision will be used to execute the command line in the Box we created., In this case, we install Python. When we execute the command, Vagrant starts to execute the necessary command to install Python in our environment. We can test the environment by executing the command:
This opens a command line with the VM we have just created, allowing us to interact with the image. The result of a vagrant ssh is this:
**Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
- Documentation: https
://help.ubuntu.com/ New release '14.04.6 LTS' available. Run' do-release-upgrade' to upgrade to it.
Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2 vagrant@precise64:~$**
Here we see that we are now in the new environment, and we can check python use the simple command python. This shows us the Python REPL up and running:
**vagrant@precise64:~$ Python Python 2.7.3 (default, Oct 26 2016, 21:01:49) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.
This article offered a short introduction to Hashicorp's Vagrant. It is an amazing tool for creating and sharing development environments, we've seen how to create a new Vagrant environment, and how this provides the basis for a more complex environment.
Learn About Vagrant With This Course: