diff --git a/scripts/configure_vagrant_k3s.sh b/scripts/configure_vagrant_k3s.sh index 7a1f469..0442834 100755 --- a/scripts/configure_vagrant_k3s.sh +++ b/scripts/configure_vagrant_k3s.sh @@ -3,7 +3,6 @@ # 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." @@ -21,6 +20,8 @@ else fi fi + + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" echo "Script directory: $SCRIPT_DIR" @@ -38,6 +39,8 @@ if [ -z "$(vagrant status | grep 'running')" ]; then vagrant up fi + + network_info=() vagrant_ports=() @@ -112,32 +115,30 @@ for info in "${network_info[@]}"; do done # Print Vagrant ports -echo "Vagrant Ports:" -for port in "${vagrant_ports[@]}"; do - echo "Port: $port" -done echo "Creating Ansible inventory file..." -ANSIBLE_DIR="$VAGRANT_DIR/ansible" -mkdir -p "$ANSIBLE_DIR" -INVENTORY_FILE="$ANSIBLE_DIR/ansible_inventory.ini" +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=.vagrant/machines/$vm/virtualbox/private_key ansible_python_interpreter=/usr/bin/python3" >> "$INVENTORY_FILE" + 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 - echo $vm >> "$INVENTORY_FILE" + if [[ "$vm" == *"vm"* ]]; then + echo $vm >> "$INVENTORY_FILE" + # vm=$(echo "$vm" | sed 's/vm//') + fi done echo "Ansible inventory file created at: $INVENTORY_FILE" -# source venv ansible - fi \ No newline at end of file diff --git a/scripts/install_vagrant_k3s.sh b/scripts/install_vagrant_k3s.sh new file mode 100755 index 0000000..b97723d --- /dev/null +++ b/scripts/install_vagrant_k3s.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +VAGRANT_DIR="$SCRIPT_DIR/../vagrant/dev/ubuntu/" + +echo "Script directory: $SCRIPT_DIR" +echo "Vagrant directory: $VAGRANT_DIR" +cd "$VAGRANT_DIR" || { + echo "Failed to change directory to Vagrant directory: $VAGRANT_DIR" + exit 1 +} + +vagrant up vm1 vm2 vm3 +if [ $? -ne 0 ]; then + echo "Failed to start Vagrant VMs. Please check your Vagrant setup." + exit 1 +fi + +../../../scripts/configure_vagrant_k3s.sh && \ + vagrant up workstation diff --git a/vagrant/dev/ubuntu/.envrc.example b/vagrant/dev/ubuntu/.envrc.example new file mode 100644 index 0000000..4ceadd0 --- /dev/null +++ b/vagrant/dev/ubuntu/.envrc.example @@ -0,0 +1 @@ +export VAGRANT_BRIDGE= \ No newline at end of file diff --git a/vagrant/dev/ubuntu/ansible/install_k3s_3node.yaml b/vagrant/dev/ubuntu/ansible/install_k3s_3node.yaml index e826648..d45d4cb 100644 --- a/vagrant/dev/ubuntu/ansible/install_k3s_3node.yaml +++ b/vagrant/dev/ubuntu/ansible/install_k3s_3node.yaml @@ -109,4 +109,39 @@ msg: "K3S installation failed on {{ inventory_hostname }}" when: inventory_hostname != 'vm1' and not k3s_binary.stat.exists and k3s_install_result.rc != 0 + - name: Ensure /home/vagrant/.kube directory exists + ansible.builtin.file: + path: /home/vagrant/.kube + state: directory + owner: vagrant + group: vagrant + mode: '0700' + - name: Copy kubeconfig to vagrant user + ansible.builtin.copy: + src: /etc/rancher/k3s/k3s.yaml + dest: /home/vagrant/.kube/config + owner: vagrant + group: vagrant + mode: '0600' + remote_src: true + + - name: Ensure KUBECONFIG is set in vagrant .bashrc + ansible.builtin.lineinfile: + path: /home/vagrant/.bashrc + line: 'export KUBECONFIG=~/.kube/config' + state: present + insertafter: EOF + owner: vagrant + group: vagrant + mode: '0644' + + - name: Ensure kubectl completion is sourced in vagrant .bashrc + ansible.builtin.lineinfile: + path: /home/vagrant/.bashrc + line: 'source <(kubectl completion bash)' + state: present + insertafter: EOF + owner: vagrant + group: vagrant + mode: '0644' \ No newline at end of file diff --git a/vagrant/dev/ubuntu/ansible/provision_workstation.sh b/vagrant/dev/ubuntu/ansible/provision_workstation.sh index 63b0037..69edef0 100644 --- a/vagrant/dev/ubuntu/ansible/provision_workstation.sh +++ b/vagrant/dev/ubuntu/ansible/provision_workstation.sh @@ -16,14 +16,15 @@ 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 -u root cp /vagrant/.vagrant/machines/vm$i/virtualbox/private_key /root/.ssh/vm${i}_key sudo chmod 600 /home/vagrant/.ssh/vm${i}_key +sudo chmod 600 /root/.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 -cp /vagrant/ansible/* /home/vagrant/ansible/ ANSIBLE_DIR=/home/vagrant/ansible echo "Ansible directory: $ANSIBLE_DIR" @@ -42,6 +43,7 @@ if [ ! -d "venv" ]; then exit 1 fi echo "Virtual environment created and activated." + cp /vagrant/ansible/requirements.txt . if [ -f "requirements.txt" ]; then echo "Installing dependencies from requirements.txt..." pip install --upgrade pip @@ -58,6 +60,11 @@ fi ANSIBLE_VENV_DIR="$ANSIBLE_DIR/venv" +echo "Ansible virtual environment directory: $ANSIBLE_VENV_DIR" + +ls -al "$ANSIBLE_VENV_DIR/bin/activate" + + if [ -d "$ANSIBLE_VENV_DIR" ]; then echo "Activating Ansible virtual environment..." source "$ANSIBLE_VENV_DIR/bin/activate" @@ -70,13 +77,15 @@ echo "" ansible --version -cp -r /vagrant/.vagrant/machines /home/vagrant/machines -cp -r ~/.vagrant/machines /home/vagrant/machines +if [ $? -ne 0 ]; then + echo "Ansible is not installed or not found in the virtual environment. Please check your installation." + exit 1 +fi + +cp -r /vagrant/ansible/* /home/vagrant/ansible/ -chmod 600 /home/vagrant/machines/*/virtualbox/private_key -chmod 600 ~/machines/*/virtualbox/private_key eval `ssh-agent -s` -ssh-add ~/machines/*/virtualbox/private_key +ssh-add # ~/machines/*/virtualbox/private_key BASHRC="/home/vagrant/.bashrc" BLOCK_START="# ADDED BY infctl provisioning" @@ -95,8 +104,23 @@ else fi echo "" +echo "user id is $(id)" +echo "group id is $(groups)" -ANSIBLE_HOST_KEY_CHECKING=False ansible --inventory-file ansible_inventory.ini -m ping all | cat +ls -al /home/vagrant/ansible +echo "" +cat /vagrant/ansible/ansible_inventory.ini +echo "" + +echo "root keys" + +ls -al ~/.ssh/vm*_key + +echo "vagrant keys" + +ls -al /home/vagrant/.ssh/vm*_key + +ANSIBLE_HOST_KEY_CHECKING=False ansible --inventory-file /home/vagrant/ansible/ansible_inventory.ini -m ping all if [ $? -ne 0 ]; then echo "Ansible ping failed. Please check your Vagrant VMs and network configuration." @@ -104,12 +128,12 @@ if [ $? -ne 0 ]; then fi # install_keepalived.yaml -ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook install_keepalived.yaml --inventory-file ansible_inventory.ini | cat +ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook install_keepalived.yaml --inventory-file ansible_inventory.ini if [ $? -ne 0 ]; then echo "Ansible playbook failed. Please check your Vagrant VMs and network configuration." exit 1 fi -echo "Keepalived installation completed successfully." +echo "Keepalived installation completed." # install_k3s_3node.yaml ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook install_k3s_3node.yaml --inventory-file ansible_inventory.ini | cat @@ -117,4 +141,4 @@ if [ $? -ne 0 ]; then echo "Ansible playbook failed. Please check your Vagrant VMs and network configuration." exit 1 fi -echo "K3s installation completed successfully." +