update: add network configuration variables and enhance Ansible playbooks for dynamic IP handling

This commit is contained in:
jon brookes 2025-08-09 11:02:15 +01:00
parent eba7f8322d
commit ae59661982
6 changed files with 68 additions and 31 deletions

9
.envrc.example Normal file
View file

@ -0,0 +1,9 @@
export VAGRANT_BRIDGE='Intel(R) Ethernet Connection (16) I219-V'
# Network configuration for Vagrant/Ansible
export WORKSTATION_IP="192.168.56.10"
export VM1_IP="192.168.56.80"
export VM2_IP="192.168.56.81"
export VM3_IP="192.168.56.82"
export VAGRANT_NETWORK_PREFIX="192.168.56"

View file

@ -53,9 +53,10 @@ for vm in $running_vms; do
vm_ips=$(vagrant ssh "$vm" -c "ip -j addr" | jq -r '.[] |
select(.addr_info != null) |
.addr_info[] |
select(.family == "inet" and (.local | startswith("192.168.56.8"))) |
select(.family == "inet" and (.local | startswith("'${VAGRANT_NETWORK_PREFIX:-192.168.56}'."))) |
.local')
# Save the VM's IP to the array if it matches our pattern
if [ ! -z "$vm_ips" ]; then
network_info+=("$vm:$vm_ips")
@ -108,7 +109,6 @@ ips=()
for info in "${network_info[@]}"; do
echo "----------------------------------------"
echo -e "$info"
# vm2:192.168.56.81
ip_addr=$(echo "$info" | cut -d':' -f2)
ips+=("$ip_addr")
echo "----------------------------------------"

View file

@ -6,6 +6,12 @@
# backwards compatibility). Please don't change it unless you know what
# you're doing.
# Load default values if environment variables are not set
vm1_ip = ENV['VM1_IP'] || "192.168.56.80"
vm2_ip = ENV['VM2_IP'] || "192.168.56.81"
vm3_ip = ENV['VM3_IP'] || "192.168.56.82"
workstation_ip = ENV['WORKSTATION_IP'] || "192.168.56.10"
Vagrant.configure("2") do |config|
if defined?(VagrantVbguest)
config.vbguest.auto_update = false
@ -18,7 +24,7 @@ Vagrant.configure("2") do |config|
vm1.vm.hostname = "vm1"
# Fixed private network IP
vm1.vm.network "private_network", ip: "192.168.56.80"
vm1.vm.network "private_network", ip: vm1_ip
# Public network for external access
if ENV['VAGRANT_BRIDGE']
@ -45,7 +51,7 @@ Vagrant.configure("2") do |config|
vm2.vm.hostname = "vm2"
# Fixed private network IP
vm2.vm.network "private_network", ip: "192.168.56.81"
vm2.vm.network "private_network", ip: vm2_ip
# Public network for external access
if ENV['VAGRANT_BRIDGE']
@ -72,7 +78,7 @@ Vagrant.configure("2") do |config|
vm3.vm.hostname = "vm3"
# Fixed private network IP
vm3.vm.network "private_network", ip: "192.168.56.82"
vm3.vm.network "private_network", ip: vm3_ip
# Public network for external access
if ENV['VAGRANT_BRIDGE']
@ -100,7 +106,7 @@ Vagrant.configure("2") do |config|
ws.vm.synced_folder ".", "/vagrant"
# Fixed private network IP
ws.vm.network "private_network", ip: "192.168.56.10"
ws.vm.network "private_network", ip: workstation_ip
ws.vm.provider "virtualbox" do |vb|
vb.memory = "1024" # Less memory needed for control node

View file

@ -4,6 +4,8 @@
connection: local
become: true
become_user: root
vars_files:
- vars.yaml
tasks:
- name: Ensure .kube directory exists
@ -17,7 +19,7 @@
- name: Copy kubeconfig using scp directly
ansible.builtin.command: >
scp -i ~/.ssh/vm1_key -o StrictHostKeyChecking=no
vagrant@192.168.56.80:/home/vagrant/.kube/config
vagrant@{{ vm1_ip }}:/home/vagrant/.kube/config
/home/vagrant/.kube/config
become: false
@ -32,7 +34,7 @@
- name: copy k3s binary to /usr/local/bin/k3s
ansible.builtin.command: >
scp -i ~/.ssh/vm1_key -o StrictHostKeyChecking=no
vagrant@192.168.56.80:/usr/local/bin/k3s
vagrant@{{ vm1_ip }}:/usr/local/bin/k3s
/usr/local/bin/k3s
become: true
@ -63,8 +65,8 @@
group: vagrant
mode: "0644"
- name: replace 127.0.0.1:6443 192.168.56.80:6443 in .kube/config
- name: replace 127.0.0.1:6443 with {{ vm1_ip }}:6443 in .kube/config
ansible.builtin.replace:
path: /home/vagrant/.kube/config
regexp: "127.0.0.1:6443"
replace: "192.168.56.80:6443"
replace: "{{ vm1_ip }}:6443"

View file

@ -4,8 +4,18 @@
become: true
become_user: root
serial: 1 # Ensure tasks are executed one host at a time
vars_files:
- vars.yaml
tasks:
- name: Debug IP variables
ansible.builtin.debug:
msg:
- "vm1_ip: {{ vm1_ip }}"
- "vm2_ip: {{ vm2_ip }}"
- "vm3_ip: {{ vm3_ip }}"
- "Current inventory_hostname: {{ inventory_hostname }}"
- name: Check if k3s is already installed
ansible.builtin.stat:
path: /usr/local/bin/k3s
@ -20,10 +30,10 @@
- name: Generate and save k3s token if not present (first node)
ansible.builtin.copy:
dest: /opt/k3s-token
content: "{{ lookup('pipe', 'head -c 16 /dev/urandom | sha256sum | cut -d\" \" -f1') }}"
content: '{{ lookup(''pipe'', ''head -c 16 /dev/urandom | sha256sum | cut -d" " -f1'') }}'
owner: root
group: root
mode: '0600'
mode: "0600"
force: false
register: generated_k3s_token
when: inventory_hostname == 'vm1' and not k3s_token_file.stat.exists
@ -32,21 +42,20 @@
ansible.builtin.get_url:
url: https://get.k3s.io
dest: /tmp/k3s_install.sh
mode: '0755'
mode: "0755"
when: not k3s_binary.stat.exists
- name: Ensure .kube directory exists
ansible.builtin.file:
path: /home/user/.kube
state: directory
mode: '0755'
mode: "0755"
when: inventory_hostname == 'vm1' and not k3s_binary.stat.exists
- name: Install k3s on first node
ansible.builtin.shell: |
set -o pipefail
# --write-kubeconfig-mode 644
K3S_TOKEN=$(cat /opt/k3s-token) /bin/bash /tmp/k3s_install.sh server --cluster-init --disable traefik --disable servicelb --tls-san 192.168.56.80 --node-name vm1 --node-ip 192.168.56.80
K3S_TOKEN=$(cat /opt/k3s-token) /bin/bash /tmp/k3s_install.sh server --cluster-init --disable traefik --disable servicelb --tls-san {{ vm1_ip }} --node-name vm1 --node-ip {{ vm1_ip }}
if [ $? -eq 0 ]; then
mkdir -p /home/vagrant/.kube && cp /etc/rancher/k3s/k3s.yaml /home/vagrant/.kube/config && chown vagrant:vagrant /home/vagrant/.kube/config
fi
@ -64,7 +73,7 @@
- name: Wait for k3s API server to be ready on master node
ansible.builtin.wait_for:
host: 192.168.56.80
host: "{{ vm1_ip }}"
port: 6443
timeout: 60
delegate_to: "{{ inventory_hostname }}"
@ -74,15 +83,15 @@
ansible.builtin.shell: |
set -o pipefail
{% if inventory_hostname == 'vm2' %}
NODE_IP="192.168.56.81"
NODE_IP="{{ vm2_ip }}"
{% elif inventory_hostname == 'vm3' %}
NODE_IP="192.168.56.82"
NODE_IP="{{ vm3_ip }}"
{% else %}
NODE_IP="192.168.56.80"
NODE_IP="{{ vm1_ip }}"
{% endif %}
K3S_URL=https://192.168.56.80:6443 \
K3S_URL=https://{{ vm1_ip }}:6443 \
K3S_TOKEN={{ k3s_token_content.stdout }} \
INSTALL_K3S_EXEC="server --disable traefik --disable servicelb --node-name={{ inventory_hostname }} --node-ip ${NODE_IP}" \
INSTALL_K3S_EXEC="server --server https://{{ vm1_ip }}:6443 --disable traefik --disable servicelb --node-name={{ inventory_hostname }} --node-ip ${NODE_IP}" \
/bin/bash /tmp/k3s_install.sh 2>&1
exit_code=$?
if [ $exit_code -ne 0 ]; then
@ -115,7 +124,7 @@
state: directory
owner: vagrant
group: vagrant
mode: '0700'
mode: "0700"
- name: Copy kubeconfig to vagrant user
ansible.builtin.copy:
@ -123,25 +132,25 @@
dest: /home/vagrant/.kube/config
owner: vagrant
group: vagrant
mode: '0600'
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'
line: "export KUBECONFIG=~/.kube/config"
state: present
insertafter: EOF
owner: vagrant
group: vagrant
mode: '0644'
mode: "0644"
- name: Ensure kubectl completion is sourced in vagrant .bashrc
ansible.builtin.lineinfile:
path: /home/vagrant/.bashrc
line: 'source <(kubectl completion bash)'
line: "source <(kubectl completion bash)"
state: present
insertafter: EOF
owner: vagrant
group: vagrant
mode: '0644'
mode: "0644"

View file

@ -0,0 +1,11 @@
---
# Network configuration - can be overridden by environment variables
vm1_ip: "{{ lookup('env', 'VM1_IP') | default('192.168.56.80', true) }}"
vm2_ip: "{{ lookup('env', 'VM2_IP') | default('192.168.56.81', true) }}"
vm3_ip: "{{ lookup('env', 'VM3_IP') | default('192.168.56.82', true) }}"
workstation_ip: "{{ lookup('env', 'WORKSTATION_IP') | default('192.168.56.10', true) }}"
network_prefix: "{{ lookup('env', 'VAGRANT_NETWORK_PREFIX') | default('192.168.56', true) }}"
# K3s configuration
k3s_cluster_name: "dev-cluster"
k3s_token_file: "/opt/k3s-token"