Vagrant Introduction | Configuring and Managing Vagrant Environments
In this article, I am going to teach you about the quick introduction of the Vagrant and the settings, configurations, and managing the vagrant Environments.
What is Vagrant?
Vagrant is an open source tool for working with virtual environments. Vagrant acts as a wrapper and sits on top of the virtualization solution (VMware, Hyper-V, VirtualBox or libvirt) and provides a simple command line interface to manage the VM’s with the custom configuration.
Why we use Vagrant?
- No need to learn different CLI command of Virtualization providers, vagrant takes cares to manage the underlying VM’s with its easy CLI interface.
- Well defined environments as configuration used to create the environments are in simple text files. The user can recreate as many Vagrant Instances (VMs) using the same Vagrant file.
- Vagrant is capable of executing configuration management software like Puppet/Ansible/Chef once the base system is ready (using the box). This help o setup the environment (System + Application) on the target machines in an automated way.
- Developer can create/destroy multiple development environments in minutes.
- As vagrant is wrapper only, you can choose the guest OS images supported by the virtualization platform you choice. Example on CentOS 7, you may run CentOS 6 VM.
How does Vagrant work?
Vagrant performs the following tasks with the single command:
- Creates a VM using base images.
- Configures the virtual machine specific settings and configuration using the Vagrantfile.
- Automates the configuration management of the virtual machine using the configuration management software.
Workflow: the below image will explain how Vagrant environments will look like.
Box: A box is a packaged Vagrant environment, typically a virtual machine.
Provider: A provider provides virtualization support. Providers can be of 02 types;
- Local: VirtualBox,VMware,Docker,Hyper-V
- Remote: AWS Cloud, Openstack
Vagrantfile: Configuration file for the target machine’s config. Contains information like
- Which Base Image to use.
- What will be the hostname?
- What will be the network?
- Any specific compute.
- Provisioner Config (Shell/Automation Tools)
Provisioner: A provisioner is a tool to set up the virtual environment and can be as simple as a shell script, also it can be automation toll-like Chef, Ansible or Puppet.
The example of Shell Provisioner:
config.vm.provision "shell", inline: <<-SHELL
yum install httpd
systemctl start httpd.service
The Example of Automation Tool Provisioner:
config.vm.provision "ansible" do |ansible|
ansible.verbose = "v"
ansible.playbook = "playbook.yml"
LAB Details and Pre-Req’s:
In this session, I am going to demonstrate vagrant on a Linux machine. Details are:
Host: CentOS 7.3 x86_64
Host Platform: VMware Workstation 12
Hardware Virtualization: AMD-v/Intel VT-x
Host Network: VM have 02 vNICs
- vNIC1 : Host Only (Host Only Connectivity) 188.8.131.52/24
- vNIC2 : Bridged (Internet Connectivity) 192.168.1.20/24
VirtualBox Version : 5.1.x86_64
Vagrant Version: 1.9.3
Also, Check out the Video for more details:
Update the system and install the pre-req packages for VirtualBox to work.
# yum update –y
# yum install gcc make kernel-devel -y
Not mandatory, but let’s install “X Windows”, to easily look into the VirtualBox issues if any.
# yum groupinstall "X Window System"
Take a reboot of the system.
# init 6
1. Download the latest available versions of Virtualbox and Vagrant software from the download pages of the products.
- VirtualBox: https://www.virtualbox.org/wiki/Linux_Downloads
- Vagrant: https://www.vagrantup.com/downloads.html
2. Once software installers downloaded (they are rpm’s generally), transfer those to the machine and it is going to be configured as a Vagrant server.
3. Install the Virtualbox and Vagrant RPM.
# yum localinstall /software/VirtualBox-5.1-5.1.18_114002_el7-1.x86_64.rpm
# yum localinstall /software/vagrant_1.9.3_x86_64.rpm
“vboxconfig” will install, configure the virtualbox modules and start the service.
Validate the Installation:
# vboxmanage --version
# vagrant --version
vboxmanage is the CLI provided by the VirtualBox package.
Downloading Base Image:
Though you can build a virtual machine from scratch, but that is going to a time taking process. You can find different images from Atlas ( Vagrant Repo) for your Virtualization Provider.
Vagrant uses a base image to quickly clone a virtual machine. These base images are known as “boxes”.
Vagrant Image Repository:
To download the base Image
# vagrant box add centos/7
Note – In the above example, we are downloading base image with name “centos/7”.
Image download will take some time depending on your internet connectivity as Image will download the Atlas repo.
Once box downloads successful, you can validate it by running the below command.
# vagrant box list
Set the Path of Virtual Machine
# vboxmanage setproperty machinefolder /app_store/virtual_machines
Initializes a new Vagrant environment:
# mkdir /path/to/directory
# cd /path/to/directory
# vagrant init <box_image>
# mkdir /app_store/centos7_demo
# cd /app_store/centos7_demo
# vagrant init centos/7
“vagrant init “ creates Vagrantfile in the current directory, you can customize the file as per requirement.
Start and provision the vagrant environment:
# vagrant up
Connect to the machine via SSH:
# vagrant ssh
# ssh -p <port> 127.0.0.1 -l vagrant
Please Note – when you run multiple vagrant machines, vagrant allocates new SSH port forwarding for every machine.
Default credentials for the Vagrant machine are generally “vagrant/vagrant”.
1. List all the VMs:
# vagrant global-status
2. To know the vagrant port mapping:
# vagrant port <vagrant machine>
3. To display IP address of guest machine:
# vagrant ip-show
Please Note – you need to install “vagrant-ip-show” before you run the below-mentioned command.
# vagrant plugin install vagrant-ip-show
Often used Vagrant CLI commands: This below commands will help you to handle Vagrant environments.
1. Outputs status of the vagrant machine:
# vagrant status
2. Outputs status Vagrant environments for this user:
# vagrant global-status
3. Starts and provisions the vagrant environment:
# vagrant up
4. Stops the vagrant machine:
# vagrant halt
5. Suspends the machine:
# vagrant suspend
6. Stops and deletes all traces of the vagrant machine:
# vagrant destroy
7. Restarts vagrant machine loads new Vagrantfile config:
# vagrant reload
8. Displays information about guest port mappings:
# vagrant port
Summary: In this article, I have tried to explain you the Vagrant Environments and how it will useful for everyone. Check out the Vagrant video for more details and if you have any suggestion/queries please comment on below. I will help you out to provide the response as my best.