Ansible Lab using Vagrant

@amitmund July 11, 2026

Ansible Lab using Vagrant (1 Control Node + 3 Managed Nodes)

Goal: Automatically provision an Ansible lab using Vagrant + VirtualBox consisting of:

  • 1 Ubuntu Ansible Control Node
  • 3 Ubuntu Managed Nodes

This provides a fully reproducible Infrastructure-as-Code lab.


Architecture

                     VirtualBox

        +---------------------------------------+

        +-------------+      Private Network
        | ansible-master |-----------------------------+
        | 192.168.56.10 |                             |
        +---------------+                             |
               |                                      |
        SSH + Ansible                                |
               |                                      |
    --------------------------------------------      |
    |                    |                  |          |
    ▼                    ▼                  ▼          |
+-----------+      +-----------+      +-----------+
| node1     |      | node2     |      | node3     |
|192.168.56.11|    |192.168.56.12|    |192.168.56.13|
+-----------+      +-----------+      +-----------+

Prerequisites

Install:

  • VirtualBox
  • Vagrant

Ubuntu

sudo apt install virtualbox

sudo apt install vagrant

Verify

vagrant --version

VBoxManage --version

Project Structure

ansible-lab/

├── Vagrantfile

├── inventory.ini

├── ansible.cfg

├── playbooks/

├── roles/

└── README.md

Vagrantfile

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

  BOX="ubuntu/noble64"

  config.vm.box = BOX

  config.vm.provider "virtualbox" do |vb|
      vb.memory = 2048
      vb.cpus = 2
  end

  nodes = {
      "master" => "192.168.56.10",
      "node1"  => "192.168.56.11",
      "node2"  => "192.168.56.12",
      "node3"  => "192.168.56.13"
  }

  nodes.each do |name,ip|

      config.vm.define name do |machine|

          machine.vm.hostname=name

          machine.vm.network "private_network", ip: ip

          machine.vm.provider "virtualbox" do |vb|
              vb.name=name
          end

      end

  end

end

Start the Lab

vagrant up

Vagrant will

  • Download Ubuntu
  • Create 4 VMs
  • Configure networking
  • Configure SSH

Verify

vagrant status

Expected

master

node1

node2

node3

running

SSH into Master

vagrant ssh master

Install Ansible

Inside master

sudo apt update

sudo apt install ansible -y

Verify

ansible --version

Configure SSH

Vagrant automatically creates SSH keys.

List machines

vagrant ssh-config

Example

Host node1

HostName 127.0.0.1

Port 2201

IdentityFile ...

Option 1

Copy Vagrant's private key

cp /vagrant/.vagrant/machines/node1/virtualbox/private_key ~/.ssh/node1

Option 2 (Recommended)

Generate a new SSH key

ssh-keygen

Copy

ssh-copy-id [email protected]

ssh-copy-id [email protected]

ssh-copy-id [email protected]

Inventory

inventory.ini

[master]

master ansible_host=192.168.56.10

[web]

node1 ansible_host=192.168.56.11

node2 ansible_host=192.168.56.12

[database]

node3 ansible_host=192.168.56.13

[all:vars]

ansible_user=vagrant

ansible_python_interpreter=/usr/bin/python3

Test Connectivity

ansible all -i inventory.ini -m ping

Expected

SUCCESS

Gather Facts

ansible all -m setup

First Playbook

---
- name: Ping all nodes

  hosts: all

  tasks:

    - ping:

Run

ansible-playbook -i inventory.ini ping.yml

Install Nginx

---
- name: Install nginx

  hosts: web

  become: yes

  tasks:

    - apt:

        name: nginx

        state: present

        update_cache: yes

Useful Vagrant Commands

Start

vagrant up

Stop

vagrant halt

Suspend

vagrant suspend

Resume

vagrant resume

Restart

vagrant reload

SSH

vagrant ssh master

vagrant ssh node1

Destroy

vagrant destroy -f

Provision

vagrant provision

Status

vagrant status

Global Status

vagrant global-status

Networking

Check

ip addr

Ping

ping 192.168.56.11

Troubleshooting

VM won't boot

VBoxManage list vms

SSH Timeout

vagrant reload

Network Issues

vagrant reload

Destroy Everything

vagrant destroy -f

Recreate

vagrant up

Advantages of Vagrant

  • Infrastructure as Code
  • Reproducible
  • Disposable
  • Git-friendly
  • Cross-platform
  • Fast setup
  • Cloud-like workflow
  • Ideal for CI/CD practice

Recommended Next Steps

  1. Automate Ansible installation using Vagrant provisioning.
  2. Automatically generate the Ansible inventory from the Vagrantfile.
  3. Configure passwordless SSH during provisioning.
  4. Add Docker installation playbooks.
  5. Add Kubernetes nodes.
  6. Add Prometheus and Grafana.
  7. Add Jenkins.
  8. Add HashiCorp Vault.
  9. Convert the Vagrant lab into Terraform-managed VMs.
  10. Migrate the same setup to AWS or Azure.

Learning Exercises

  • Provision all four VMs with a single vagrant up.
  • Configure Ansible automatically during provisioning.
  • Write a playbook to install Docker on all nodes.
  • Create an Ansible role for Nginx.
  • Add a fifth node dynamically.
  • Use Vagrant snapshots before running experiments.
  • Destroy and recreate the lab from scratch.
  • Integrate the lab with GitHub for version control.

Interview Questions

  1. What problem does Vagrant solve?
  2. How does Vagrant differ from Terraform?
  3. What is a Vagrant Box?
  4. What is provisioning in Vagrant?
  5. How does Vagrant manage SSH access?
  6. Why is Vagrant useful for learning Ansible?
  7. What providers does Vagrant support?
  8. How would you share a Vagrant lab with your team?
  9. How do you recreate an environment from scratch?
  10. What are the limitations of Vagrant compared to cloud infrastructure?
0 Likes
26 Views
0 Comments

Filters

No filters available for this view.

Reset All