update: add installation scripts for Vagrant and Ansible, enhance k3s setup, and improve provisioning process
This commit is contained in:
parent
ff55197c58
commit
514e05a1f6
5 changed files with 103 additions and 22 deletions
|
|
@ -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.
|
# 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..."
|
echo "Checking Vagrant prerequisites..."
|
||||||
|
|
||||||
# Check if Vagrant is installed
|
# Check if Vagrant is installed
|
||||||
if ! command -v vagrant &> /dev/null; then
|
if ! command -v vagrant &> /dev/null; then
|
||||||
echo "Vagrant is not installed. Please install Vagrant to proceed."
|
echo "Vagrant is not installed. Please install Vagrant to proceed."
|
||||||
|
|
@ -21,6 +20,8 @@ else
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
echo "Script directory: $SCRIPT_DIR"
|
echo "Script directory: $SCRIPT_DIR"
|
||||||
|
|
||||||
|
|
@ -38,6 +39,8 @@ if [ -z "$(vagrant status | grep 'running')" ]; then
|
||||||
vagrant up
|
vagrant up
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
network_info=()
|
network_info=()
|
||||||
vagrant_ports=()
|
vagrant_ports=()
|
||||||
|
|
||||||
|
|
@ -112,32 +115,30 @@ for info in "${network_info[@]}"; do
|
||||||
done
|
done
|
||||||
|
|
||||||
# Print Vagrant ports
|
# Print Vagrant ports
|
||||||
echo "Vagrant Ports:"
|
|
||||||
for port in "${vagrant_ports[@]}"; do
|
|
||||||
echo "Port: $port"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "Creating Ansible inventory file..."
|
echo "Creating Ansible inventory file..."
|
||||||
ANSIBLE_DIR="$VAGRANT_DIR/ansible"
|
INVENTORY_FILE="ansible/ansible_inventory.ini"
|
||||||
mkdir -p "$ANSIBLE_DIR"
|
|
||||||
INVENTORY_FILE="$ANSIBLE_DIR/ansible_inventory.ini"
|
echo "Ansible inventory file: $INVENTORY_FILE"
|
||||||
|
|
||||||
echo "[all]" > "$INVENTORY_FILE"
|
echo "[all]" > "$INVENTORY_FILE"
|
||||||
i=0
|
i=0
|
||||||
for info in "${network_info[@]}"; do
|
for info in "${network_info[@]}"; do
|
||||||
port="22"
|
port="22"
|
||||||
vm=$(echo "$info" | cut -d':' -f1)
|
vm=$(echo "$info" | cut -d':' -f1)
|
||||||
host_ip=$(echo "$info" | cut -d':' -f2)
|
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++))
|
((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
|
||||||
|
if [[ "$vm" == *"vm"* ]]; then
|
||||||
echo $vm >> "$INVENTORY_FILE"
|
echo $vm >> "$INVENTORY_FILE"
|
||||||
|
# vm=$(echo "$vm" | sed 's/vm//')
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Ansible inventory file created at: $INVENTORY_FILE"
|
echo "Ansible inventory file created at: $INVENTORY_FILE"
|
||||||
|
|
||||||
# source venv ansible
|
|
||||||
fi
|
|
||||||
20
scripts/install_vagrant_k3s.sh
Executable file
20
scripts/install_vagrant_k3s.sh
Executable file
|
|
@ -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
|
||||||
1
vagrant/dev/ubuntu/.envrc.example
Normal file
1
vagrant/dev/ubuntu/.envrc.example
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
export VAGRANT_BRIDGE=<preferred interface to bride to>
|
||||||
|
|
@ -109,4 +109,39 @@
|
||||||
msg: "K3S installation failed on {{ inventory_hostname }}"
|
msg: "K3S installation failed on {{ inventory_hostname }}"
|
||||||
when: inventory_hostname != 'vm1' and not k3s_binary.stat.exists and k3s_install_result.rc != 0
|
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'
|
||||||
|
|
@ -16,14 +16,15 @@ sudo chmod 700 /home/vagrant/.ssh
|
||||||
# Copy the Vagrant private keys (these will be synced by Vagrant)
|
# Copy the Vagrant private keys (these will be synced by Vagrant)
|
||||||
for i in {1..3}; do
|
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 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 /home/vagrant/.ssh/vm${i}_key
|
||||||
|
sudo chmod 600 /root/.ssh/vm${i}_key
|
||||||
done
|
done
|
||||||
|
|
||||||
# Disable host key checking for easier learning
|
# Disable host key checking for easier learning
|
||||||
echo "[defaults]" > /home/vagrant/.ansible/ansible.cfg
|
echo "[defaults]" > /home/vagrant/.ansible/ansible.cfg
|
||||||
echo "host_key_checking = False" >> /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
|
ANSIBLE_DIR=/home/vagrant/ansible
|
||||||
echo "Ansible directory: $ANSIBLE_DIR"
|
echo "Ansible directory: $ANSIBLE_DIR"
|
||||||
|
|
@ -42,6 +43,7 @@ if [ ! -d "venv" ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "Virtual environment created and activated."
|
echo "Virtual environment created and activated."
|
||||||
|
cp /vagrant/ansible/requirements.txt .
|
||||||
if [ -f "requirements.txt" ]; then
|
if [ -f "requirements.txt" ]; then
|
||||||
echo "Installing dependencies from requirements.txt..."
|
echo "Installing dependencies from requirements.txt..."
|
||||||
pip install --upgrade pip
|
pip install --upgrade pip
|
||||||
|
|
@ -58,6 +60,11 @@ fi
|
||||||
|
|
||||||
ANSIBLE_VENV_DIR="$ANSIBLE_DIR/venv"
|
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
|
if [ -d "$ANSIBLE_VENV_DIR" ]; then
|
||||||
echo "Activating Ansible virtual environment..."
|
echo "Activating Ansible virtual environment..."
|
||||||
source "$ANSIBLE_VENV_DIR/bin/activate"
|
source "$ANSIBLE_VENV_DIR/bin/activate"
|
||||||
|
|
@ -70,13 +77,15 @@ echo ""
|
||||||
|
|
||||||
ansible --version
|
ansible --version
|
||||||
|
|
||||||
cp -r /vagrant/.vagrant/machines /home/vagrant/machines
|
if [ $? -ne 0 ]; then
|
||||||
cp -r ~/.vagrant/machines /home/vagrant/machines
|
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`
|
eval `ssh-agent -s`
|
||||||
ssh-add ~/machines/*/virtualbox/private_key
|
ssh-add # ~/machines/*/virtualbox/private_key
|
||||||
|
|
||||||
BASHRC="/home/vagrant/.bashrc"
|
BASHRC="/home/vagrant/.bashrc"
|
||||||
BLOCK_START="# ADDED BY infctl provisioning"
|
BLOCK_START="# ADDED BY infctl provisioning"
|
||||||
|
|
@ -95,8 +104,23 @@ else
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo ""
|
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
|
if [ $? -ne 0 ]; then
|
||||||
echo "Ansible ping failed. Please check your Vagrant VMs and network configuration."
|
echo "Ansible ping failed. Please check your Vagrant VMs and network configuration."
|
||||||
|
|
@ -104,12 +128,12 @@ if [ $? -ne 0 ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# install_keepalived.yaml
|
# 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
|
if [ $? -ne 0 ]; then
|
||||||
echo "Ansible playbook failed. Please check your Vagrant VMs and network configuration."
|
echo "Ansible playbook failed. Please check your Vagrant VMs and network configuration."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "Keepalived installation completed successfully."
|
echo "Keepalived installation completed."
|
||||||
|
|
||||||
# install_k3s_3node.yaml
|
# install_k3s_3node.yaml
|
||||||
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook install_k3s_3node.yaml --inventory-file ansible_inventory.ini | cat
|
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."
|
echo "Ansible playbook failed. Please check your Vagrant VMs and network configuration."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "K3s installation completed successfully."
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue