Vagrant – Using Vagrantfile to manage virtual machines

Vagrant is a tool for working with virtual machines. Vagrant provides a simple and easy to use command-line client for managing virtual machines. Vagrant is open source, which means that anyone can download it, modify it, and share it freely.
Virtual machine hypervisors like Virtualbox often provides their own command line interface for managing virtual machines (VBoxManage for virtual box). Vagrant provides an additional layer and thus simplify and unify the management by providing a consistent approach accross multiple systems.

Vagrantfile is a special file containing text-based definitions of what each environment looks like. Vagrant uses instructions in the Vagrantfile to manage the virtual environment. This common text-based format that is defined through code is advantageous in that many users can easily collaborate on it, it can be backed up, shared and tracked using a source code management system.

Terminology used by Vagrant.

  • Box: A box is a packaged Vagrant environment, typically a virtual machine.
  • Provider: A provider is the location in which the virtual environment runs. It can be local (the default is to use VirtualBox), remote, or even a special case like a Docker container.
  • Provisioner: A provisioner is a tool to set up the virtual environment, and can be as simple as a shell script, but alternatively a more advanced tool like Chef, Puppet, or Ansible can be used.

Installation:

Vagrant binaries can be downloaded in the official download page here https://www.vagrantup.com/downloads.

You can also install from the commandline using these commands:
Mac

brew tap hashicorp/tap
brew install vagrant

Ubuntu

sudo apt update
sudo apt install vagrat

Redhat based systems

sudo yum update
sudo yum install vagrant

Creating an Ubuntu 20.04 VM

To initiate a new Vagrantfile to be used to create an Ubuntu 20.04 virtual machine with using this box generic/ubuntu2004:

vagrant init generic/ubuntu2004

Lets check thecontent of the file:

cat Vagrantfile

Without the comments, this is the content of the Vagrantfile

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

Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2004"
end

You can also download the boxes locally with these commannds:

# You will be prompted to choose your provider if no default is set
vagrant box add generic/ubuntu2004

# You can also specify the provider
vagrant box add generic/debian10 --provider libvirt
vagrant box add generic/fedora33 --provider virtualbox

To list available boxes:

vagrant box list

Examples

Here is a complicated Vagrantfile:

Vagrant.configure("2") do |config|
    config.vm.box = "generic/ubuntu2004"

    config.vm.provider "virtualbox" do |vb|
        vb.memory = "1024"
    end

    hostname = 'ub-k0s'

    config.vm.define "#{hostname}" do |s|
        s.vm.hostname = "#{hostname}"
        s.vm.network 'private_network', ip: "192.168.20.7"

        s.vm.provider 'virtualbox' do |v|
            v.name = "#{hostname}"
            v.linked_clone = true
        end

        s.vm.provision :ansible do |vm|
            vm.verbose = true
            vm.playbook = "provision/main.yaml"
        end
    end

end

Here is the content of the playbook provision/main.yaml

- hosts: all
  gather_facts: false
  become: true
  tasks:
    - name: Upgrade packages
      apt:
        name: "*"
        state: latest
        update_cache: yes

    - name: Install required packages
      apt:
        name:
          - vim
          - telnet
          - htop
        state: latest

To bring up the box:

vagrant up

To check current state of the machines, ensure you are in the dir that has the Vangrantfile and use this commands:

vagrant status

vagrant status debian-box

Output

❯ vagrant status
Current machine states:

ub-k0s                    running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

ssh into the box

vagrant ssh
vagrant ssh ub-k0s

Bringing up multiple instances

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

Vagrant.configure(2) do |config|

  config.vm.provision "shell", path: "bootstrap.sh"

  NodeCount = 2

  (1..NodeCount).each do |i|

    config.vm.define "ubuntuvm#{i}" do |node|

      node.vm.box               = "generic/ubuntu2004"
      node.vm.box_check_update  = false
      node.vm.box_version       = "3.3.2"
      node.vm.hostname          = "ubuntuvm#{i}.example.com"

      node.vm.network "private_network", ip: "172.16.16.10#{i}"

      node.vm.provider :virtualbox do |v|
        v.name    = "ubuntuvm#{i}"
        v.memory  = 1024
        v.cpus    = 1
      end

      node.vm.provider :libvirt do |v|
        v.nested  = true
        v.memory  = 1024
        v.cpus    = 1
      end

    end

  end

end

This is the content of bootstrap.sh

#!/bin/bash

# Enable ssh password authentication
echo "Enable ssh password authentication"
sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
sed -i 's/.*PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
systemctl reload sshd

# Set Root password
echo "Set root password"
echo -e "admin\nadmin" | passwd root >/dev/null 2>&1

To login to a server:

vagrant ssh debian-box

To stop server:

vagrant halt debian-box

To clean up

vagrant destroy
vagrant destroy -f
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy