Sat, March 24, 2018 ยท 2 min read

Vagrant making VMs setup easy

Vagrant making VMs setup easy

Vagrant enables users to create and configure lightweight, reproducible, and portable development environments.

Installing vagrant on Fedora 26

$ sudo dnf install vagrant -y
Last metadata expiration check: 2:10:16 ago on Sunday 25 March 2018 06:58:56 PM IST.
Package vagrant-1.9.1-3.fc26.noarch is already installed, skipping.
Dependencies resolved.
Nothing to do.
Complete!

Initialize Vagrantfile

$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment!

Installing vagrant plugins

$ vagrant plugin install vagrant-rekey-ssh ; vagrant plugin install vagrant-mutate ; vagrant plugin install vagrant-libvirt

Vagrantfile configuration

# -*- mode: ruby -*-
# vi: set ft=ruby :
 
Vagrant.configure("2") do |config|
 
config.vm.define :one do |one|
one.vm.box = "centos/7"
one.vm.box_check_update = false
one.vm.network "forwarded_port", guest: 80, host: 8080
one.vm.network "private_network", ip: "192.168.122.100"
one.vm.hostname = "one.vagrant.box"
 
config.vm.provider "libvirt" do |libvirt|
    libvirt.connect_via_ssh = false
    libvirt.username = "cipher"
    libvirt.storage_pool_name = "default"
    libvirt.memory = "420"
    libvirt.driver = "kvm"
end
end
 
config.vm.define :two do |two|
two.vm.box = "centos/7"
two.vm.box_check_update = false
two.vm.network "forwarded_port", guest: 80, host: 8081
two.vm.network "private_network", ip: "192.168.122.101"
two.vm.hostname = "two.vagrant.box"
 
config.vm.provider "libvirt" do |libvirt|
    libvirt.connect_via_ssh = false
    libvirt.username = "cipher"
    libvirt.storage_pool_name = "default"
    libvirt.memory = "420"
    libvirt.driver = "kvm"
end
end
end

Spinning up VMs

$ vagrant up
Bringing machine 'one' up with 'libvirt' provider...
Bringing machine 'two' up with 'libvirt' provider...
==> one: Creating image (snapshot of base box volume).
==> two: Creating image (snapshot of base box volume).
==> one: Creating domain with the following settings...
==> one:   -- Name:              cipher_one
==> one:   -- Domain type:       kvm
==> one:   -- Memory:            420M
==> two:   -- Name:              cipher_two
==> two:   -- Domain type:       kvm

Checking vagrant status

$ vagrant status
Current machine states:

one                       running (libvirt)
two                       running (libvirt)

Accessing VMs

$ vagrant ssh one
[vagrant@one ~]$ hostname
one.vagrant.box

$ vagrant ssh two
[vagrant@two ~]$ hostname
two.vagrant.box