How to provision infrastructure automatically using Vagrant (IaaC) Part - 1

How to provision infrastructure automatically using Vagrant (IaaC) Part - 1

So the other day i was watching a movie called "Immitation Game". It was a good movie and i have probably watched it at least a dozen times now. But what since i was using Vagrant and provisioning test infrastructure I ended up understanding the movie's plot at a different level and closely related to the movie.

This movie is closely inspired by true events that happened during World War 2. The plot has a mathematician team up with a bunch of other mathematicians(unwillingly) to crack cypher exchanged between German troops during the war.

This Cypher was specifically created using a machine called Enigma which had different settings and techniques used to provide over 151 trillion combinations. Which made it near impossible for anyone to Decipher it.

However, Dr. Alan Turing the main protagonist not only found a way to decipher just one message but also deciphered how Enigma worked and his major achievement was building a machine called Bombe, that changed the entire outcome of WW2.

But my key understanding was to comprehend Alan's ability to automate the deciphering process. Similar to the modern need to automate our day to day tasks using the tools at hand. And create new tools and technologies to do so.

image.png

Similar to Alan's automation we nowadays automate the provisioning of Infrastructure using tools such as Vagrant. Today we will learn how to provision infrastructure using Vagrant.

Vagrant at its core uses 2 different things to provision a machine i.e(infrastructure), These are:

image.png

Here is the Architecture based on which Vagrant works in a nutshell how it proceeds with each task at hand.

image.png

Now, Lets understand what this basically means. To keep it short and crisp, Vagrant uses a file called "Vagrant File" to hold all the instructions it would need to provision a machine. You can compare it to a task/todo list for the Vagrant program that is acting as an employee for us. Simply lay down the tasks in front of him in a language that it understands (i.e Python, Ruby, Shell, Golang) and that should be good to go.

Now, let us understand it with the commands to further cement the concept of it all.

Vagrant init "YOUR-BOX-NAME"

This command would create a basic vagrant file that would have the name of the box and a bunch of other code commented out. You can remove the # and adjust accordingly if you want to change anything.

Great, once you have a vagrant file it is fairly easy to use it. You can open this in a text editor for e.g: Sublime Text, Notepad++ or even Notepad(for psychos).

For example here is a snippet of code from one of my simple vagrant files:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# 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|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "geerlingguy/centos7"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

Here the below mentioned line works as a function definition just like you would name a function and later call it accordingly, you can do the same by changing the name from config to whatever you wish.

Vagrant.configure("2") do |config - > MY-NAME|

Once you understand the meaning of this the rest of it becomes pretty easy and if you take a closer look at it, every setting in use starts with the same function name. This signifies the calling of the function (in our case VAGRANT BOX).

CONFIG.vm.box = "geerlingguy/centos7"
CONFIG.vm.network "private_network", ip: "192.168.33.10"
CONFIG.vm.network "public_network"

Next Part would consists of all the commands following this and explain how the provisioned machine actually look like also what could you do with the provisioning.