Ansible Lab Environment Setup
A safe, disposable lab environment is the single most important enabler of rapid Ansible learning. You need to be able to break things, rebuild them, and experiment without fear of affecting real infrastructure. Vagrant and VirtualBox give you exactly this capability — a programmable environment where virtual machines spin up and tear down in minutes with a single command.
Why Vagrant and VirtualBox
VirtualBox is a free, open-source hypervisor that runs virtual machines on your laptop or desktop. Vagrant is a command-line tool that automates the creation and management of VirtualBox virtual machines using a simple configuration file called a Vagrantfile. Together, they let you:
- Create multiple virtual machines with one command (
vagrant up) - Destroy and recreate them instantly (
vagrant destroy && vagrant up) - Share identical environments with teammates by sharing the Vagrantfile
- Simulate real multi-server environments on a single laptop
For learners with cloud accounts, an alternative is to use AWS Free Tier EC2 instances or DigitalOcean Droplets. The cloud approach requires an account but eliminates the need for VirtualBox on low-powered machines.
Installing VirtualBox
Download VirtualBox from virtualbox.org. The installation packages are available for Windows, macOS, and Linux. On Ubuntu, you can also install via apt:
sudo apt update sudo apt install -y virtualbox
After installation, verify VirtualBox is working by launching the application and confirming the main interface opens without errors.
Installing Vagrant
Download Vagrant from vagrantup.com. On macOS with Homebrew:
brew install vagrant
On Ubuntu:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list sudo apt update && sudo apt install vagrant
Verify the installation:
vagrant --version
Creating Your Lab Environment
Create a directory for your Ansible lab and navigate to it:
mkdir ansible-lab && cd ansible-lab
Create a Vagrantfile that defines three virtual machines — one web server, a second web server, and a database server:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.define "web01" do |web01|
web01.vm.hostname = "web01"
web01.vm.network "private_network", ip: "192.168.56.11"
end
config.vm.define "web02" do |web02|
web02.vm.hostname = "web02"
web02.vm.network "private_network", ip: "192.168.56.12"
end
config.vm.define "db01" do |db01|
db01.vm.hostname = "db01"
db01.vm.network "private_network", ip: "192.168.56.13"
end
config.vm.provider "virtualbox" do |vb|
vb.memory = "512"
vb.cpus = 1
end
endThis Vagrantfile uses the official Ubuntu 22.04 LTS box, assigns static private IP addresses, and limits each VM to 512 MB of RAM — suitable for most laptops.
Starting the Lab
From the ansible-lab directory, start all three VMs:
vagrant up
The first run downloads the Ubuntu box image (approximately 500 MB) and provisions three VMs. Subsequent runs start in under a minute. You will see progress output for each VM as it boots.
Verify all VMs are running:
vagrant status
You should see all three VMs listed as "running".
Connecting to Your VMs with SSH
Vagrant automatically configures SSH access. Connect to any VM with:
vagrant ssh web01
You will be dropped into an interactive shell on the web01 VM. Type exit to return to your control node.
Vagrant generates unique SSH keys for each VM. For Ansible to manage these VMs, you need to extract the SSH connection details. Run:
vagrant ssh-config
This shows the IdentityFile path for each VM. For the lab, the simplest approach is to copy your own SSH public key to each VM so Ansible can connect using your standard key.
Configuring SSH Key Access for Ansible
If you do not have an SSH key pair, generate one:
ssh-keygen -t ed25519 -C "ansible-lab"
Accept the defaults and leave the passphrase empty for the lab environment. Copy your public key to each VM using vagrant's built-in SSH:
vagrant ssh web01 -c "mkdir -p ~/.ssh && echo '$(cat ~/.ssh/id_ed25519.pub)' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" vagrant ssh web02 -c "mkdir -p ~/.ssh && echo '$(cat ~/.ssh/id_ed25519.pub)' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" vagrant ssh db01 -c "mkdir -p ~/.ssh && echo '$(cat ~/.ssh/id_ed25519.pub)' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Creating the Ansible Inventory File
Create an inventory file in your lab directory:
[webservers] web01 ansible_host=192.168.56.11 web02 ansible_host=192.168.56.12 [databases] db01 ansible_host=192.168.56.13 [all:vars] ansible_user=vagrant ansible_ssh_private_key_file=~/.ssh/id_ed25519
Save this as inventory.ini. This file tells Ansible where to find your three VMs and how to connect to them.
Testing Connectivity
With everything configured, test that Ansible can reach all three nodes:
ansible all -i inventory.ini -m ping
A successful response looks like:
web01 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web02 | SUCCESS => {
"changed": false,
"ping": "pong"
}
db01 | SUCCESS => {
"changed": false,
"ping": "pong"
}All three nodes responding with "pong" confirms your lab is fully operational.
Essential Vagrant Commands
vagrant up— Start all VMs (or a specific one withvagrant up web01)vagrant halt— Stop VMs gracefully without destroying themvagrant destroy -f— Delete all VMs and their disk imagesvagrant reload— Restart VMs and re-apply the Vagrantfilevagrant snapshot save web01 baseline— Save a snapshot of VM state for quick restoration
Cloud Alternative: AWS Free Tier
If VirtualBox is not suitable for your hardware (common on M1/M2 Macs where VirtualBox has limited support), use three AWS EC2 t2.micro instances in the same VPC. The free tier provides 750 hours per month of t2.micro usage — enough for the entire course with careful management. Create a key pair in the AWS console, launch three Ubuntu 22.04 instances, and use their private IP addresses in your inventory file.
Try This: Snapshot Workflow
After confirming all three nodes respond to ping, take a Vagrant snapshot of each VM to create a clean baseline you can restore to if an experiment goes wrong:
vagrant snapshot save web01 baseline vagrant snapshot save web02 baseline vagrant snapshot save db01 baseline
Restore a snapshot with vagrant snapshot restore web01 baseline. Use this workflow throughout the course to safely explore destructive operations.
Summary
Vagrant and VirtualBox provide a programmable, disposable lab environment where you can safely practice every technique in this course. A Vagrantfile defines three virtual machines that serve as web servers and a database server. SSH key-based authentication connects the control node to managed nodes. The ansible all -m ping command verifies end-to-end connectivity. With this lab in place, you are ready to begin writing automation.
