#!/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 running on Windows if [[ "$(uname -s)" == *"MINGW"* || "$(uname -s)" == *"CYGWIN"* || "$OSTYPE" == "msys" ]]; then echo "Detected Windows environment. Skipping VBoxManage check as Vagrant will handle VirtualBox interactions." else # Check if VirtualBox is installed (for non-Windows systems) if ! command -v VBoxManage &> /dev/null; then echo "VirtualBox is not installed. Please install VirtualBox to proceed." exit 1 fi fi SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" echo "Script directory: $SCRIPT_DIR" VAGRANT_DIR="$SCRIPT_DIR/../vagrant/dev/ubuntu/" echo "Vagrant directory: $VAGRANT_DIR" 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=() vagrant_ports=() 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 and get specific IPs vm_ips=$(vagrant ssh "$vm" -c "ip -j addr | jq -r '.[] | select(.ifname==\"enp0s8\") | .addr_info[] | select(.family==\"inet\" and .scope==\"global\" and .prefixlen<32 and .local!=\"'${VAGRANT_NETWORK_PREFIX:-192.168.56.250}'\") | .local'") # Save the VM's IP to the array if it matches our pattern if [ ! -z "$vm_ips" ]; then network_info+=("$vm:$vm_ips") fi 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." # Ensure unique ports are added to the vagrant_ports array add_unique_port() { local port=$1 for existing_port in "${vagrant_ports[@]}"; do if [ "$existing_port" == "$port" ]; then return fi done vagrant_ports+=("$port") } while read -r line; do # Extract the port number vagrant_ports+=("$line") done < <(vagrant ssh-config | grep Port) while read -r line; do # Extract the port number port=$(echo "$line" | awk '{print $2}') add_unique_port "$port" done < <(vagrant ssh-config | grep Port) ips=() # Print network information for info in "${network_info[@]}"; do echo "----------------------------------------" echo -e "$info" ip_addr=$(echo "$info" | cut -d':' -f2) ips+=("$ip_addr") echo "----------------------------------------" done # Print Vagrant ports echo "Creating Ansible inventory file..." INVENTORY_FILE="ansible/ansible_inventory.ini" echo "Ansible inventory file: $INVENTORY_FILE" echo "[all]" > "$INVENTORY_FILE" i=0 for info in "${network_info[@]}"; do port="22" vm=$(echo "$info" | cut -d':' -f1) host_ip=$(echo "$info" | cut -d':' -f2) echo "$vm ansible_host=$host_ip ansible_port=$port ansible_user=vagrant ansible_ssh_private_key_file=~/.ssh/${vm}_key ansible_python_interpreter=/usr/bin/python3" >> "$INVENTORY_FILE" ((i++)) done echo "" >> "$INVENTORY_FILE" echo "[vms]" >> "$INVENTORY_FILE" for vm in $running_vms; do if [[ "$vm" == *"vm"* ]]; then echo $vm >> "$INVENTORY_FILE" # vm=$(echo "$vm" | sed 's/vm//') fi done echo "Ansible inventory file created at: $INVENTORY_FILE"