update: enhance Vagrantfile with workstation configuration and adjust network settings for VMs

This commit is contained in:
jon brookes 2025-08-07 10:22:11 +01:00
parent 9ce3197c0b
commit e9e05d1c60
3 changed files with 60 additions and 109 deletions

4
.gitignore vendored
View file

@ -17,3 +17,7 @@ deleted
*/venv* */venv*
.vagrant .vagrant
vagrant/dev/ubuntu/toggle_defender.ps1
scripts/ansible_inventory.ini
scripts/ansible_inventory.ini
vagrant/dev/ubuntu/ansible/ansible_inventory.ini

View file

@ -46,19 +46,17 @@ running_vms=$(vagrant status | grep "running" | awk '{print $1}')
for vm in $running_vms; do for vm in $running_vms; do
# Check network interfaces # Check network interfaces and get specific IPs
vm_info=$(vagrant ssh "$vm" -c "ip -j addr" | jq -r ' vm_ips=$(vagrant ssh "$vm" -c "ip -j addr" | jq -r '.[] |
.[] | select(.addr_info != null) |
"Interface: \(.ifname)\n" + .addr_info[] |
(if .addr_info then select(.family == "inet" and (.local | startswith("192.168.56.8"))) |
(.addr_info | map(" IP (\(.family)): \(.local)") | join("\n")) .local')
else
""
end)
')
# Save the VM's network info to the array # Save the VM's IP to the array if it matches our pattern
network_info+=("$vm:\n$vm_info") if [ ! -z "$vm_ips" ]; then
network_info+=("$vm:$vm_ips")
fi
done done
@ -74,7 +72,7 @@ echo "Network information gathered successfully."
# get vagrant ports # get vagrant ports
echo "Gathering Vagrant port information..." # echo "Gathering Vagrant port information..."
# Ensure unique ports are added to the vagrant_ports array # Ensure unique ports are added to the vagrant_ports array
@ -89,29 +87,27 @@ add_unique_port() {
} }
while read -r line; do while read -r line; do
echo "Processing line: $line"
# Extract the port number # Extract the port number
port=$(echo "$line" | awk '{print $2}') vagrant_ports+=("$line")
echo "Extracted port: $port"
vagrant_ports+=("$port")
done < <(vagrant ssh-config | grep Port) done < <(vagrant ssh-config | grep Port)
while read -r line; do while read -r line; do
echo "Processing line: $line"
# Extract the port number # Extract the port number
port=$(echo "$line" | awk '{print $2}') port=$(echo "$line" | awk '{print $2}')
echo "Extracted port: $port"
add_unique_port "$port" add_unique_port "$port"
done < <(vagrant ssh-config | grep Port) done < <(vagrant ssh-config | grep Port)
ips=()
# Print network information # Print network information
for info in "${network_info[@]}"; do for info in "${network_info[@]}"; do
echo "----------------------------------------" echo "----------------------------------------"
echo -e "$info" echo -e "$info"
# vm2:192.168.56.81
ip_addr=$(echo "$info" | cut -d':' -f2)
ips+=("$ip_addr")
echo "----------------------------------------" echo "----------------------------------------"
done done
@ -122,54 +118,26 @@ for port in "${vagrant_ports[@]}"; do
done done
echo "Creating Ansible inventory file..." echo "Creating Ansible inventory file..."
inventory_file="$SCRIPT_DIR/ansible_inventory.ini" ANSIBLE_DIR="$VAGRANT_DIR/ansible"
echo "[all]" > "$inventory_file" mkdir -p "$ANSIBLE_DIR"
INVENTORY_FILE="$ANSIBLE_DIR/ansible_inventory.ini"
echo "[all]" > "$INVENTORY_FILE"
i=0 i=0
for vm in $running_vms; do for info in "${network_info[@]}"; do
port="${vagrant_ports[$i]}" port="22"
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" 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=.vagrant/machines/$vm/virtualbox/private_key ansible_python_interpreter=/usr/bin/python3" >> "$INVENTORY_FILE"
((i++)) ((i++))
done done
echo "" >> "$inventory_file" echo "" >> "$INVENTORY_FILE"
echo "[vms]" >> "$inventory_file" echo "[vms]" >> "$INVENTORY_FILE"
for vm in $running_vms; do for vm in $running_vms; do
echo $vm >> "$inventory_file" echo $vm >> "$INVENTORY_FILE"
done done
echo "Ansible inventory file created at: $inventory_file" echo "Ansible inventory file created at: $INVENTORY_FILE"
# source venv ansible # 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 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
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook ../../../ansible/install_keepalived.yaml --inventory-file ../../../scripts/ansible_inventory.ini | cat
# exit if error from playbook
if [ $? -ne 0 ]; then
echo "Ansible playbook failed. Please check your Vagrant VMs and network configuration."
exit 1
fi
echo "Keepalived installation completed successfully."
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook ../../../ansible/install_k3s_3node.yaml --inventory-file ../../../scripts/ansible_inventory.ini | cat
if [ $? -ne 0 ]; then
echo "Ansible playbook failed. Please check your Vagrant VMs and network configuration."
exit 1
fi
echo "K3s installation completed successfully."

View file

@ -9,59 +9,16 @@ Vagrant.configure("2") do |config|
config.vbguest.auto_update = false config.vbguest.auto_update = false
# Ansible Controller/Workstation Configuration
config.vm.define "workstation" do |ws|
ws.vm.box = "ubuntu/jammy64"
ws.vm.hostname = "ansible-workstation"
ws.vm.synced_folder ".", "/vagrant"
# Fixed private network IP
ws.vm.network "private_network", ip: "192.168.56.10"
ws.vm.provider "virtualbox" do |vb|
vb.memory = "1024" # Less memory needed for control node
vb.cpus = 1
end
ws.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt-get install -y ansible git vim
# Set up ansible environment for vagrant user
sudo -u vagrant mkdir -p /home/vagrant/.ansible
sudo -u vagrant touch /home/vagrant/.ansible/ansible.cfg
# Create workspace and SSH directories
sudo -u vagrant mkdir -p /home/vagrant/ansible
sudo -u vagrant mkdir -p /home/vagrant/.ssh
sudo chmod 700 /home/vagrant/.ssh
# Copy the Vagrant private keys (these will be synced by Vagrant)
for i in {1..3}; do
sudo -u vagrant cp /vagrant/.vagrant/machines/vm$i/virtualbox/private_key /home/vagrant/.ssh/vm${i}_key
sudo chmod 600 /home/vagrant/.ssh/vm${i}_key
done
# Disable host key checking for easier learning
echo "[defaults]" > /home/vagrant/.ansible/ansible.cfg
echo "host_key_checking = False" >> /home/vagrant/.ansible/ansible.cfg
SHELL
end
# VM 1 Configuration # VM 1 Configuration
config.vm.define "vm1" do |vm1| config.vm.define "vm1" do |vm1|
vm1.vm.box = "ubuntu/jammy64" vm1.vm.box = "ubuntu/jammy64"
vm1.vm.hostname = "vm1"
# Fixed private network IP # Fixed private network IP
vm1.vm.network "private_network", ip: "192.168.56.80" vm1.vm.network "private_network", ip: "192.168.56.80"
# Public network for external access # Public network for external access
vm1.vm.network "public_network", bridge: "wlp0s20f3" vm1.vm.network "public_network", bridge: "Intel(R) Wi-Fi 6E AX211 160MHz"
vm1.vm.provider "virtualbox" do |vb| vm1.vm.provider "virtualbox" do |vb|
vb.memory = "2048" # 2GB memory vb.memory = "2048" # 2GB memory
@ -78,12 +35,13 @@ Vagrant.configure("2") do |config|
# VM 2 Configuration # VM 2 Configuration
config.vm.define "vm2" do |vm2| config.vm.define "vm2" do |vm2|
vm2.vm.box = "ubuntu/jammy64" vm2.vm.box = "ubuntu/jammy64"
vm2.vm.hostname = "vm2"
# Fixed private network IP # Fixed private network IP
vm2.vm.network "private_network", ip: "192.168.56.81" vm2.vm.network "private_network", ip: "192.168.56.81"
# Public network for external access # Public network for external access
vm2.vm.network "public_network", bridge: "wlp0s20f3" vm2.vm.network "public_network", bridge: "Intel(R) Wi-Fi 6E AX211 160MHz"
vm2.vm.provider "virtualbox" do |vb| vm2.vm.provider "virtualbox" do |vb|
vb.memory = "2048" # 2GB memory vb.memory = "2048" # 2GB memory
@ -100,12 +58,13 @@ Vagrant.configure("2") do |config|
# VM 3 Configuration # VM 3 Configuration
config.vm.define "vm3" do |vm3| config.vm.define "vm3" do |vm3|
vm3.vm.box = "ubuntu/jammy64" vm3.vm.box = "ubuntu/jammy64"
vm3.vm.hostname = "vm3"
# Fixed private network IP # Fixed private network IP
vm3.vm.network "private_network", ip: "192.168.56.82" vm3.vm.network "private_network", ip: "192.168.56.82"
# Public network for external access # Public network for external access
vm3.vm.network "public_network", bridge: "wlp0s20f3" vm3.vm.network "public_network", bridge: "Intel(R) Wi-Fi 6E AX211 160MHz"
vm3.vm.provider "virtualbox" do |vb| vm3.vm.provider "virtualbox" do |vb|
vb.memory = "2048" # 2GB memory vb.memory = "2048" # 2GB memory
@ -118,4 +77,24 @@ Vagrant.configure("2") do |config|
# python3 -m pip install --upgrade pip # python3 -m pip install --upgrade pip
SHELL SHELL
end end
# Ansible Controller/Workstation Configuration
config.vm.define "workstation" do |ws|
ws.vm.box = "ubuntu/jammy64"
ws.vm.hostname = "ansible-workstation"
ws.vm.synced_folder ".", "/vagrant"
# Fixed private network IP
ws.vm.network "private_network", ip: "192.168.56.10"
ws.vm.provider "virtualbox" do |vb|
vb.memory = "1024" # Less memory needed for control node
vb.cpus = 1
end
ws.vm.provision "shell", path: "ansible/provision_workstation.sh"
end
end end