Environment in 2 minutes with Vagrant.

Today I want to share with you an awesome thing - Vagrnat! What is Vagrant? Following the Wikipedia:

Vagrant is an open-source software product for building and maintaining portable virtual software development environments, e.g. for VirtualBox, Hyper-V, Docker, VMware, and AWS.

In other words, Vagrant allows you to create a virtual machine(s) (VMs), install os and pre-configure it with only one command!

Start with vagrant

All the example were tested on Fedora Linux 25. If you have another distro you should read your distro’s documentation.

The easiest way is to install vagrant and VirtualBox from your distro’s maintainer repository. In my case it was just like that:

dnf install VirtualBox
dnf install vagrant

But of course, you can always make it manually from rpm-package or source code. Ok, whats next?

Vagrantfile!

Vagrantfile is a description of you future virtual stand. Let’s take a look at the average Vagrantfile:

Vagrant.configure("2") do |config|

  config.vm.define "host1" do |host1|
    host1.vm.box = "centos/7"
    host1.vm.network "private_network", ip: "192.168.33.10"
  end

  config.vm.define "host2" do |host2|
    host2.vm.box = "centos/7"
    host2.vm.network "private_network", ip: "192.168.33.20"
  end

end

Pretty clear, I think. In the very basic example, we have two VMs with CentOS 7 which is connected to a private network. Note, that your own PC will have an IP address 192.168.33.1 (you may see IP address with any tool you use, ‘ip a’ for example). This file, Vagrantfile, should be placed in working dir. After that, we can type only one command and see all the Magic!

$ vagrant up

Now we can test connection between host and VM’s

ping 192.168.33.10
PING 192.168.33.10 (192.168.33.10) 56(84) bytes of data.
64 bytes from 192.168.33.10: icmp_seq=1 ttl=64 time=0.654 ms

Note, that the first run may take 5-10 minutes (Depends on your internet connection). But next runs will take seconds. Why? Because ‘centos/7’ box has already been downloaded on your computer. Boxes? Wha..

Boxes!

In vagrant’s terms Boxes is an OS images, which is prepared for you by Vagrant’s maintainer - Hashicorp or by any other. A number of boxes you may find here. So if you don’t like CentOS (but why? It’s so cute!) you may create a new Vagrantfile with the following content in it:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
end

or just replace ‘centos/7’ from first Vagrantfile’s example with ‘vm.box = “ubuntu/trusty64”’. And after ‘vagrant up’ you’ll get shiny new Ubuntu inside your VM. And ‘ubuntu/trusty64’ box will be added to your local box’ storage (~/.vagrant.d/boxes).

Post-install configuration

It’s nice to have VM’s which can start in seconds, but what about configuration? Vagrants allow you to create an on-boot shell script, in which you may include what you want. Let’s improve our Vagrantfile:

Vagrant.configure("2") do |config|

  config.vm.define "host1" do |host1|
    host1.vm.box = "centos/7"
    host1.vm.network "private_network", ip: "192.168.33.10"
    host1.vm.provision :shell, path: "bootstrap.sh"
  end

  config.vm.define "host2" do |host2|
    host2.vm.box = "centos/7"
    host2.vm.network "private_network", ip: "192.168.33.20"
    host2.vm.provision :shell, path: "bootstrap.sh"
  end

end

I’ve just added line with instructions for vagrant to run ‘bootstrap.sh’ on each VM. ‘bootstrat.sh’ lay in the same folder with Vagrantfile. Content of ‘bootstrap.sh’:

#!/bin/bash
echo "Nice day to learn Vagrant" > /tmp/hello

Now check how it works:

$ vagrant up
<...output omitted...>
==> host1: Running provisioner: shell...
<...output omitted...>
==> host2: Running provisioner: shell...
<...output omitted...>
$ vagrant ssh host1
[vagrant@localhost ~]$ cat /tmp/hello 
Nice day to learn Vagrant
[vagrant@localhost ~]$ exit

Seems like everything’s OK! Just a couple of thing before you dive into Vagrant (just a little memo): most useful commands are

  • vagrant up - start your environment
  • vagrant status - show status of VM’s
  • vagrant ssh <VM’s name> - connect to VM
  • vagrant halt - shutdown VM(s)
  • vagrant destroy - remove VM(s)
  • vagrant - show help

Conclusion

When I discovered Vagrant I had a feeling like I’d found a treasure! Seriously, Vagrant is that good! Last thing I’d like to advise is a book called ‘Vagrant UP&Running’ by Mitchel Hashimoto. It’s pretty old, as I know (2013) but it helped me in the past. And of course an official documentation on https://www.vagrantup.com/docs/index.html.

That’s all, see you.

Written on August 7, 2017