2025-08-03 11:54:46 +01:00
#!/usr/bin/env bash
# This script checks for Vagrant and VirtualBox prerequisites, ensures Vagrant VMs are running, and gathers network and system information from the VMs.
echo "Checking Vagrant prerequisites..."
# Check if Vagrant is installed
if ! command -v vagrant & > /dev/null; then
echo "Vagrant is not installed. Please install Vagrant to proceed."
exit 1
fi
# Check if VirtualBox is installed
if ! command -v VBoxManage & > /dev/null; then
echo "VirtualBox is not installed. Please install VirtualBox to proceed."
exit 1
fi
SCRIPT_DIR = " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd ) "
echo " Script directory: $SCRIPT_DIR "
VAGRANT_DIR = " $SCRIPT_DIR /../vagrant/dev/ubuntu/ "
2025-08-04 21:44:28 +01:00
echo " Vagrant directory: $VAGRANT_DIR "
2025-08-03 11:54:46 +01:00
cd " $VAGRANT_DIR " || {
echo " Failed to change directory to Vagrant directory: $VAGRANT_DIR "
exit 1
}
# Check if any VMs are running; if not, run 'vagrant up'
if [ -z " $( vagrant status | grep 'running' ) " ] ; then
echo "No Vagrant VMs are running. Starting VMs with 'vagrant up'..."
vagrant up
fi
network_info = ( )
2025-08-04 21:44:28 +01:00
vagrant_ports = ( )
2025-08-03 11:54:46 +01:00
echo "Gathering network information from running VMs..."
running_vms = $( vagrant status | grep "running" | awk '{print $1}' )
for vm in $running_vms ; do
# Check network interfaces
vm_info = $( vagrant ssh " $vm " -c "ip -j addr" | jq -r '
2025-08-04 21:44:28 +01:00
.[ ] |
"Interface: \(.ifname)\n" +
2025-08-03 11:54:46 +01:00
( if .addr_info then
( .addr_info | map( " IP (\(.family)): \(.local)" ) | join( "\n" ) )
else
""
end)
' )
# Save the VM's network info to the array
network_info += ( " $vm :\n $vm_info " )
done
for vm in $running_vms ; do
echo $vm
echo "----------------------------------------"
vagrant ssh " $vm " -c "free" | grep 'Mem:' | awk '{print "Memory in bytes: " $2 " Memory in MB: " $2/1024/1024}'
vagrant ssh " $vm " -c "lscpu" | grep 'CPU(s):' | grep -v 'NUMA' | awk '{print "CPU count: " $2}'
echo "----------------------------------------"
done
echo "Network information gathered successfully."
2025-08-04 21:44:28 +01:00
# get vagrant ports
echo "Gathering Vagrant port information..."
# vagrant ssh-config | grep Port | while read -r line; do
# echo "Processing line: $line"
# # Processing line: Port 2222
# # Processing line: Port 2200
# # Processing line: Port 2201
# port=$(echo "$line" | awk '{print $2}')
# echo "Extracted port: $port"
# vagrant_ports+=("$port")
# done
while read -r line; do
echo " Processing line: $line "
# Extract the port number
port = $( echo " $line " | awk '{print $2}' )
echo " Extracted port: $port "
vagrant_ports += ( " $port " )
done < <( vagrant ssh-config | grep Port)
2025-08-03 11:54:46 +01:00
# Print network information
for info in " ${ network_info [@] } " ; do
echo "----------------------------------------"
echo -e " $info "
echo "----------------------------------------"
done
2025-08-04 21:44:28 +01:00
# Print Vagrant ports
echo "Vagrant Ports:"
for port in " ${ vagrant_ports [@] } " ; do
echo " Port: $port "
done
# create an ansible inventory file
# [all]
# vm1 ansible_host=<vm1_ip> ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/vm1/virtualbox/private_key
# vm2 ansible_host=<vm2_ip> ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/vm2/virtualbox/private_key
# vm3 ansible_host=<vm3_ip> ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/vm3/virtualbox/private_key
# echo "Creating Ansible inventory file..."
# inventory_file="$SCRIPT_DIR/ansible_inventory.ini"
# echo "[all]" > "$inventory_file"
# i=0
# for vm in $running_vms; do
# port="${vagrant_ports[$i]}"
# echo "$vm ansible_host=127.0.0.1 ansible_port=$port ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/$vm/virtualbox/private_key" >> "$inventory_file"
# ((i++))
# done
echo "Creating Ansible inventory file..."
inventory_file = " $SCRIPT_DIR /ansible_inventory.ini "
echo "[all]" > " $inventory_file "
i = 0
for vm in $running_vms ; do
port = " ${ vagrant_ports [ $i ] } "
echo " $vm ansible_host=127.0.0.1 ansible_port= $port ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/ $vm /virtualbox/private_key ansible_python_interpreter=/usr/bin/python3 " >> " $inventory_file "
( ( i++) )
done
echo "" >> " $inventory_file "
# echo "[children]" >> "$inventory_file"
# echo "vm_group" >> "$inventory_file"
# echo "" >> "$inventory_file"
echo "[vms]" >> " $inventory_file "
for vm in $running_vms ; do
echo $vm >> " $inventory_file "
done
echo " Ansible inventory file created at: $inventory_file "
# source venv ansible
ANSIBLE_VENV_DIR = " $SCRIPT_DIR /../ansible/venv "
if [ -d " $ANSIBLE_VENV_DIR " ] ; then
echo "Activating Ansible virtual environment..."
source " $ANSIBLE_VENV_DIR /bin/activate "
else
echo " Ansible virtual environment not found at $ANSIBLE_VENV_DIR . Please create it before running this script. "
exit 1
fi
ANSIBLE_HOST_KEY_CHECKING = False ansible --inventory-file ../../../scripts/ansible_inventory.ini -m ping all | cat
# exit if error from ping
if [ $? -ne 0 ] ; then
echo "Ansible ping failed. Please check your Vagrant VMs and network configuration."
exit 1
fi
2025-08-03 11:54:46 +01:00
2025-08-04 21:44:28 +01:00
ANSIBLE_HOST_KEY_CHECKING = False ansible-playbook ../../../ansible/install_keepalived.yaml --inventory-file ../../../scripts/ansible_inventory.ini | cat
2025-08-03 11:54:46 +01:00