How to Provision Virtual Machines using Vagrant : The Automated Way of Creating VM's

In the past , whenever we had to build VirtualMachines using Virtual Box we had to undergo a cumbersome process where you had to setup disks, networks and then install operating system and sometimes even doing this could take several hours as you had to download the ISO for operating system and then install it and fix any errors during this whole process of configuration.

What if you get an automated way of handling this which helps you to create a new VM within minutes, Welcome to the world of Vagrant ( A Hashicorp Product). Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, 

Vagrant Boxes are prepackaged development environments that are the foundation of Vagrant. We shall be making use of Prebuilt Vagrant boxes:

We have different Vagrant Boxes for various providers like hyperv, libvirt ,virtualbox ,vmware_desktop

In this article we shall take a look at how you can provision a new virtualbox 

  • Download VirtualBox (if you don't have available on your host)
  • Download Vagrant 
  • Search for the various Vagrant boxes from (https://app.vagrantup.com/boxes/search) search for centos
  • Once you have downloaded and installed vagrant : perform the below steps:

List the Vagrant Boxes on your Host

Joys-MacBook-Pro:~ udemy$ vagrant box list

There are no installed boxes! Use `vagrant box add` to add some.

Download the Vagrant Box (centos/7)

Joys-MacBook-Pro:~ udemy$ vagrant box add centos/7

==> box: Loading metadata for box 'centos/7'

    box: URL: https://vagrantcloud.com/centos/7

This box can work with multiple providers! The providers that it

can work with are listed below. Please review the list and choose

the provider you will be working with.


1) hyperv

2) libvirt

3) virtualbox

4) vmware_desktop


Enter your choice: 3

==> box: Adding box 'centos/7' (v1905.1) for provider: virtualbox

    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box

    box: Download redirected to host: cloud.centos.org

==> box: Successfully added box 'centos/7' (v1905.1) for 'virtualbox'!

 

Initialize the Vagrant Box

The below step will create a configuration file for you called "Vagrantfile"

Joys-MacBook-Pro:ora18onprem udemy$ vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Vagrant Setup

Now we have got the configuration file and we can make changes as per our environment, you will notice that all the parameters are hashed out (#) , so we need to just enter the config that we want,

Most importantly , Check your Host Only Adapter Settings : File -> Host Network Manager and check that by default you have the network settings  (DHCP where starting address is : 192.168.56.101 & upper bound is 192.168.56.254 so we need to choose the IP address within this range

The Config changes that i did 

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

   config.vm.box = "centos/7"
   config.vm.network "private_network", ip: "192.168.56.101"
   config.vm.hostname = "ora18onprem"

config.vm.provider "virtualbox" do |vb|

  #   # Display the VirtualBox GUI when booting the machine

  #   vb.gui = true
  
# Customize the amount of memory on the VM:

      # Setting Memory for the Guest VM

      vb.memory = "2500"
#  Give a name for your VM otherwise you get very weird names

      vb.name = "ora18onprem"

  end
=====================================================================

 

Create your VM Machine as per the Configuration

the command to create and run the VM is 

vagrant up 

Joys-MacBook-Pro:ora18onprem udemy$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1905.1' is up to date...
==> default: Setting the name of the VM: ora18onprem
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /Users/udemy/ora18onprem/ => /vagrant

How to SSH to Guest Machine

vagrant ssh

a simple command helps you to ssh (without any password) to the Guest VM and once you are logged in as vagrant user just perform sudo su - to become a root user.

Happy Learning.

Kshitij Joy (Oracle Certified Master)

DB Alchemist Academy

You may explore our E-Learning Page to have an insight into our High Rated Courses on Udemy and Enrol to the Complete Course.

Share you comments

{{ errorMessage }}