Quick introduction to Vagrant

Vagrant is a program that enables you to create portable and reproducible development environments easily supporting many hosts and guests operating systems and various features such as synced folders, forwarded ports and support for famous provisioners such as Chef, Puppet or Ansible.

If you have been using tools like Virtual Box you may think of it such as a scripting engine for VirtualBox. What are the use cases for Vagrant over Virtual Box ?

1) Network

You need to set up quickly multi networks. Vagrant gives you a single config file to set these up, enabling you to launch all of them with one command. Simply edit the Vagrantfile and reload the VMs, whereas with VirtualBox you’d have to open the settings for each VM, and change them inside.

2) Multi platform

There’s a large number of boxes available at sites such as http://vagrantbox.es. This enables you to try various OSes or distributions, applying the same provisioning to set up similar environments. This can help with testing or adding support to new platforms, and would be time-consuming using just VirtualBox.

3) Shareable configuration files

The configuration is stored in a text file called Vagrantfile which can be easily versioned and shared. So in case you had problems with your image just revert the changes and reload the VM!

Hello World Vagrant

Vagrant uses boxes – essentially virtual machines pre-configured with software – as the starting unit of any configuration. You can find a ton of existing boxes at atlas.hashicorp.com – if one suits your needs exactly, you can use it directly. If it’s close, you can create a new one based on it. You can also build your own boxes from scratch using Virtualbox.

To install plain Vagrant package, run:

$ yum install vagrant

This will install base Vagrant package which is what you want in case you only need to use Vagrant with Docker or provider that is currently not directly supported in Fedora such as VirtualBox. Next, try the vagrant command from your OS prompt. You should get something looking like this:

$ vagrant
Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           log in to HashiCorp's Atlas
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     version         prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Now you’re ready to try configuring a virtual machine. Every Vagrant development environment requires a box. You can search for boxes at https://atlas.hashicorp.com/search. We will use the fedora/23-cloud-base box so enter:

$ vagrant init fedora/23-cloud-base

You will see a Vagrantfile has been created for you. The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines. The syntax of Vagrantfiles is Ruby, but knowledge of the Ruby programming language is not necessary to make modifications to the Vagrantfile, since it is mostly simple variable assignment.

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
config.vm.box = "fedora/23-cloud-base"
end

Now, let’s boot the server:

$ vagrant up

Vagrant spins up a virtual machine using the selected box image. You can choose the VM provider for starting the VM. The default is virtualbox if you leave the –provider flag out of your vagrant up command.

You can check the status of the VM with:

$ vagrant status
Current machine states:

default                   running (libvirt)

Once the machine is up and running, you can connect to it as the vagrant user:

$ vagrant ssh

The Vagrant user you’ll connect to is usually named vagrant with a password of vagrant, but that isn’t actually required. In fact, Vagrant creates and signs special SSH keys between your host machine and the box, so that you don’t have to enter passwords to work in the command line. Your user traditionally is granted sudo rights, so a simple su – is all you need to get in as Root.

Once in the VM shell, you can check the IP address assigned to the VM:

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.121.192  netmask 255.255.255.0  broadcast 192.168.121.255
        inet6 fe80::5054:ff:fe82:93f  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:82:09:3f  txqueuelen 1000  (Ethernet)
        RX packets 1203  bytes 92341 (90.1 KiB)
        RX errors 0  dropped 2  overruns 0  frame 0
        TX packets 355  bytes 49444 (48.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Once completed, return to the host and stop the VM with:

$ vagrant halt
Found the article helpful? if so please follow us on Socials