Sandbox Environment with Vagrant

Loading

In this blog, we will take you through the process of configuring a small Ansible environment using Vagrant. Vagrant provides a solution for creating disposable or sandbox virtual dev environments, all within a single Vagrantfile, which closely mimics the production environment. We define everything we need in a VM in the Vagrantfile and Vagrant will take care of configuring those in the VM.

Vagrant uses a concept of base images for operating systems that are customised and stored in a public Vagrant repository, and for this configuration, we will be using the standard RHEL9 image.

Lab Configuration

We have the following VMs created which will be later used for the Ansible Tower configuration.

HostnameRoleIP Address
aapAnsible Automation Provider10.0.2.50
haproxyLoad Balancer10.0.2.51
webserver01Web Server10.0.2.52
webserver01Web Server10.0.2.53
dbDatabase Server10.0.2.54

How to Manage Vagrant

Here are some useful Vagrant commands to manage our VM’s.

vagrant versionShow version of Vagrant
vagrant init <image name>Initialize the Vagrantfile eg: generic/rhel9
vagrant up <VM name>Launches the VM
vagrant halt <VM name>Shutdown the VM
vagrant reloadRestarts the VM
vagrant statusShow the current state of the VM
vagrant sshssh connection to VM
vagrant destroy <VM name>Delete the VM

Prerequisites for Installing Vagrant

Vagrant is a wrapper utility that works on top of Virtual machine solutions like Oracle Virtualbox, HyperV, VMware, Docker etc.

Create a Lab Environment with Vagrant

Create a directory to define and hold the configuration necessary for Vagrant to run. Our defined Vagrant file is added to this location and Vagrant uses the configuration in the Vagrantfile to build out the VM’s

Define the Vagrantfile for Multiple Vagrant VMs

# Ansible Deployment Lab Servers for Virtual Box 

# Deploy Ansible Automation Provider - AAP 
Vagrant.configure("2") do |config|
  config.vm.define "aap" do |aap|
    aap.vm.box = "generic/rhel9"
    aap.vm.hostname = 'aap'
    aap.vm.box_url = "generic/rhel9"
    aap.vm.network :private_network, ip: "10.0.2.50"
    aap.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      v.customize ["modifyvm", :id, "--memory", 1024]
      v.customize ["modifyvm", :id, "--name", "aap"]
    end
  end
# Deploy Load Balancer
  config.vm.define "haproxy" do |haproxy|
    haproxy.vm.box = "generic/rhel9"
    haproxy.vm.hostname = 'haproxy'
    haproxy.vm.box_url = "generic/rhel9"
    haproxy.vm.network :private_network, ip: "10.0.2.51"
    haproxy.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      v.customize ["modifyvm", :id, "--memory", 1024]
      v.customize ["modifyvm", :id, "--name", "haproxy"]
    end
  end
# Deploy WebServer01
  config.vm.define "web01" do |web01|
    web01.vm.box = "generic/rhel9"
    config.vm.provision :shell, path: "web01config.sh"
    web01.vm.hostname = 'web01'
    web01.vm.box_url = "generic/rhel9"
    web01.vm.network :private_network, ip: "10.0.2.52"
    web01.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      v.customize ["modifyvm", :id, "--memory", 1024]
      v.customize ["modifyvm", :id, "--name", "web01"]
    end
  end
# Deploy WebServer02
  config.vm.define "web02" do |web02|
    web02.vm.box = "generic/rhel9"
    config.vm.provision :shell, path: "web02config.sh"
    web02.vm.hostname = 'web02'
    web02.vm.box_url = "generic/rhel9"
    web02.vm.network :private_network, ip: "10.0.2.53"
    web02.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      v.customize ["modifyvm", :id, "--memory", 1024]
      v.customize ["modifyvm", :id, "--name", "web02"]
    end
  end
# Deploy Database Server
  config.vm.define "db" do |db|
    db.vm.box = "generic/rhel9"
    db.vm.hostname = 'db'
    db.vm.box_url = "generic/rhel9"
    db.vm.network :private_network, ip: "10.0.2.54"
    db.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      v.customize ["modifyvm", :id, "--memory", 1024]
      v.customize ["modifyvm", :id, "--name", "db"]
    end
  end

Define the following for our webserver config web01config.sh & web02config.sh

#!Apache Config

dnf install --assumeyes httpd mod_ssl
#setenforce 0
systemctl start httpd
systemctl enable httpd
firewall-cmd --permanent --add-service=http --add-service=https
firewall-cmd --reload
iptables-A INPUT -m state --state NEW -m tcp -p tcp -s 192.168.1.0/24 --dport 80 -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
service iptables save
service iptables restart
echo This is my webserver $HOSTNAME > /var/www/html/index.htmlecho "<h1> This is a test website </h1>" >/var/www/html/index.html

Start the virtual machines by running the following command:

Show the status of all virtual machines by running the following command:

Connect to the VM via ssh command

(Visited 19 times, 1 visits today)