Great! Leveraging Vagrant + VirtualBox is an excellent and lightweight way to create a lab environment for practicing Ansible and Prometheus. Here's a step-by-step guide and cheatsheet tailored to your DevOps use case.
Vagrant + VirtualBox Lab Setup for Practicing Ansible & Prometheus
Goals
- Create multiple VMs (e.g., Control Node & Target Nodes)
- Use Ansible for configuration management and orchestration
- Deploy Prometheus monitoring stack on one or more VMs
- Practice networking and provisioning with Vagrant
Suggested VM Topology
| VM Name | Role | Purpose |
|---|
ansible-control | Ansible Control Node | Run Ansible playbooks from here |
node1 | Managed Node 1 | Target for Ansible, run Prometheus node exporter or apps |
node2 | Managed Node 2 | Another target node |
monitor | Prometheus Server | Hosts Prometheus server and optionally Grafana |
Step 1: Create a Multi-VM Vagrantfile
Vagrant.configure("2") do |config|
# Base box (Ubuntu 20.04 LTS recommended)
config.vm.box = "ubuntu/focal64"
# Ansible Control Node
config.vm.define "ansible-control" do |control|
control.vm.hostname = "ansible-control"
control.vm.network "private_network", ip: "192.168.56.10"
control.vm.provider "virtualbox" do |vb|
vb.memory = 2048
vb.cpus = 2
vb.name = "ansible-control"
end
control.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y python3 python3-pip sshpass ansible
SHELL
end
# Node 1
config.vm.define "node1" do |node1|
node1.vm.hostname = "node1"
node1.vm.network "private_network", ip: "192.168.56.11"
node1.vm.provider "virtualbox" do |vb|
vb.memory = 1024
vb.cpus = 1
vb.name = "node1"
end
node1.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y python3
SHELL
end
# Node 2
config.vm.define "node2" do |node2|
node2.vm.hostname = "node2"
node2.vm.network "private_network", ip: "192.168.56.12"
node2.vm.provider "virtualbox" do |vb|
vb.memory = 1024
vb.cpus = 1
vb.name = "node2"
end
node2.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y python3
SHELL
end
# Prometheus Server
config.vm.define "monitor" do |monitor|
monitor.vm.hostname = "monitor"
monitor.vm.network "private_network", ip: "192.168.56.20"
monitor.vm.provider "virtualbox" do |vb|
vb.memory = 2048
vb.cpus = 2
vb.name = "monitor"
end
monitor.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y wget tar
# Download latest Prometheus (adapt version)
PROM_VERSION="2.44.0"
wget https://github.com/prometheus/prometheus/releases/download/v$PROM_VERSION/prometheus-$PROM_VERSION.linux-amd64.tar.gz
tar xvf prometheus-$PROM_VERSION.linux-amd64.tar.gz
sudo mv prometheus-$PROM_VERSION.linux-amd64 /opt/prometheus
# Create prometheus user and directories
sudo useradd --no-create-home --shell /bin/false prometheus
sudo mkdir /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /etc/prometheus /var/lib/prometheus /opt/prometheus
# Setup systemd service (you can refine this with Ansible)
# We'll skip here; will be done with Ansible playbook
SHELL
end
end
Step 2: Bring up the environment
vagrant up
This will bring all VMs up and provision basics (Python is a must on Ansible controlled nodes).
Step 3: Configure Ansible Control Node
SSH or vagrant ssh ansible-control into the ansible-control VM and:
- Create
/etc/ansible/hosts inventory file with your nodes:
[all]
node1 ansible_host=192.168.56.11 ansible_user=vagrant ansible_ssh_private_key_file=/vagrant/.vagrant/machines/node1/virtualbox/private_key
node2 ansible_host=192.168.56.12 ansible_user=vagrant ansible_ssh_private_key_file=/vagrant/.vagrant/machines/node2/virtualbox/private_key
monitor ansible_host=192.168.56.20 ansible_user=vagrant ansible_ssh_private_key_file=/vagrant/.vagrant/machines/monitor/virtualbox/private_key
Note: Path to private key might vary; you can find it with vagrant ssh-config <vm_name> or use the default vagrant user with private key.
Step 4: Test Ansible connectivity
Run from ansible-control:
ansible all -m ping
Expected output:
node1 | SUCCESS => {...}
node2 | SUCCESS => {...}
monitor | SUCCESS => {...}
Step 5: Practice Ansible Playbooks
Example: Install Node Exporter on all nodes (for Prometheus metrics)
---
- name: Install node_exporter on all nodes
hosts: all
become: yes
tasks:
- name: Download node exporter
get_url:
url: https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
dest: /tmp/node_exporter.tar.gz
- name: Extract node exporter
unarchive:
src: /tmp/node_exporter.tar.gz
dest: /usr/local/bin/
remote_src: yes
extra_opts: [--strip-components=1]
- name: Create node_exporter systemd service
copy:
dest: /etc/systemd/system/node_exporter.service
content: |
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=nobody
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=default.target
- name: Reload systemd and enable node_exporter
systemd:
daemon_reload: yes
name: node_exporter
enabled: yes
state: started
Run it:
ansible-playbook -i /etc/ansible/hosts install_node_exporter.yml
Step 6: Configure Prometheus Server
Create or provision Prometheus config:
Example /etc/prometheus/prometheus.yml targeting your nodes:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['192.168.56.11:9100', '192.168.56.12:9100']
On your monitor VM:
- Place config at
/etc/prometheus/prometheus.yml
- Launch Prometheus:
/opt/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/
To practice, you can automate this with an Ansible playbook as well.
Additional Tips
Shared Folders
Make sure your /vagrant folder is shared so you can put Ansible playbooks on the host and run from control node.
Sync Time
If you face issues, some times syncing time or restarting network on VMs helps.
Networking
You can also use public_network if you want VMs reachable on your LAN.
Provision Ansible from Host
Instead of installing Ansible inside vagrant, you can run playbooks from your host machine targeting the VMs via SSH.
Summary
| Step | Description |
|---|
| 1. Multi-VM setup | Create multiple VMs with roles |
| 2. Provision | Base software installed in Vagrant provisioning |
| 3. Configure Ansible | Setup inventory and test connectivity |
| 4. Practice playbooks | Deploy node_exporter, prometheus, etc. |
| 5. Setup Prometheus | Run Prometheus server and scrape exporters |
If you want, I can help build specific Ansible playbooks for Prometheus setup, or scripts to automate monitoring install!
Would you like me to provide example playbooks or a ready-to-use repo structure?