update: add network configuration variables and enhance Ansible playbooks for dynamic IP handling
This commit is contained in:
parent
eba7f8322d
commit
ae59661982
6 changed files with 68 additions and 31 deletions
9
.envrc.example
Normal file
9
.envrc.example
Normal 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"
|
||||||
|
|
@ -53,9 +53,10 @@ for vm in $running_vms; do
|
||||||
vm_ips=$(vagrant ssh "$vm" -c "ip -j addr" | jq -r '.[] |
|
vm_ips=$(vagrant ssh "$vm" -c "ip -j addr" | jq -r '.[] |
|
||||||
select(.addr_info != null) |
|
select(.addr_info != null) |
|
||||||
.addr_info[] |
|
.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')
|
.local')
|
||||||
|
|
||||||
|
|
||||||
# Save the VM's IP to the array if it matches our pattern
|
# Save the VM's IP to the array if it matches our pattern
|
||||||
if [ ! -z "$vm_ips" ]; then
|
if [ ! -z "$vm_ips" ]; then
|
||||||
network_info+=("$vm:$vm_ips")
|
network_info+=("$vm:$vm_ips")
|
||||||
|
|
@ -108,7 +109,6 @@ ips=()
|
||||||
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)
|
ip_addr=$(echo "$info" | cut -d':' -f2)
|
||||||
ips+=("$ip_addr")
|
ips+=("$ip_addr")
|
||||||
echo "----------------------------------------"
|
echo "----------------------------------------"
|
||||||
|
|
|
||||||
16
vagrant/dev/ubuntu/Vagrantfile
vendored
16
vagrant/dev/ubuntu/Vagrantfile
vendored
|
|
@ -6,6 +6,12 @@
|
||||||
# backwards compatibility). Please don't change it unless you know what
|
# backwards compatibility). Please don't change it unless you know what
|
||||||
# you're doing.
|
# 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|
|
Vagrant.configure("2") do |config|
|
||||||
if defined?(VagrantVbguest)
|
if defined?(VagrantVbguest)
|
||||||
config.vbguest.auto_update = false
|
config.vbguest.auto_update = false
|
||||||
|
|
@ -18,7 +24,7 @@ Vagrant.configure("2") do |config|
|
||||||
vm1.vm.hostname = "vm1"
|
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: vm1_ip
|
||||||
|
|
||||||
# Public network for external access
|
# Public network for external access
|
||||||
if ENV['VAGRANT_BRIDGE']
|
if ENV['VAGRANT_BRIDGE']
|
||||||
|
|
@ -45,7 +51,7 @@ Vagrant.configure("2") do |config|
|
||||||
vm2.vm.hostname = "vm2"
|
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: vm2_ip
|
||||||
|
|
||||||
# Public network for external access
|
# Public network for external access
|
||||||
if ENV['VAGRANT_BRIDGE']
|
if ENV['VAGRANT_BRIDGE']
|
||||||
|
|
@ -72,9 +78,9 @@ Vagrant.configure("2") do |config|
|
||||||
vm3.vm.hostname = "vm3"
|
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: vm3_ip
|
||||||
|
|
||||||
# Public network for external access
|
# Public network for external access
|
||||||
if ENV['VAGRANT_BRIDGE']
|
if ENV['VAGRANT_BRIDGE']
|
||||||
vm3.vm.network "public_network", bridge: ENV['VAGRANT_BRIDGE']
|
vm3.vm.network "public_network", bridge: ENV['VAGRANT_BRIDGE']
|
||||||
else
|
else
|
||||||
|
|
@ -100,7 +106,7 @@ Vagrant.configure("2") do |config|
|
||||||
ws.vm.synced_folder ".", "/vagrant"
|
ws.vm.synced_folder ".", "/vagrant"
|
||||||
|
|
||||||
# Fixed private network IP
|
# 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|
|
ws.vm.provider "virtualbox" do |vb|
|
||||||
vb.memory = "1024" # Less memory needed for control node
|
vb.memory = "1024" # Less memory needed for control node
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
connection: local
|
connection: local
|
||||||
become: true
|
become: true
|
||||||
become_user: root
|
become_user: root
|
||||||
|
vars_files:
|
||||||
|
- vars.yaml
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: Ensure .kube directory exists
|
- name: Ensure .kube directory exists
|
||||||
|
|
@ -17,7 +19,7 @@
|
||||||
- name: Copy kubeconfig using scp directly
|
- name: Copy kubeconfig using scp directly
|
||||||
ansible.builtin.command: >
|
ansible.builtin.command: >
|
||||||
scp -i ~/.ssh/vm1_key -o StrictHostKeyChecking=no
|
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
|
/home/vagrant/.kube/config
|
||||||
become: false
|
become: false
|
||||||
|
|
||||||
|
|
@ -32,7 +34,7 @@
|
||||||
- name: copy k3s binary to /usr/local/bin/k3s
|
- name: copy k3s binary to /usr/local/bin/k3s
|
||||||
ansible.builtin.command: >
|
ansible.builtin.command: >
|
||||||
scp -i ~/.ssh/vm1_key -o StrictHostKeyChecking=no
|
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
|
/usr/local/bin/k3s
|
||||||
become: true
|
become: true
|
||||||
|
|
||||||
|
|
@ -63,8 +65,8 @@
|
||||||
group: vagrant
|
group: vagrant
|
||||||
mode: "0644"
|
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:
|
ansible.builtin.replace:
|
||||||
path: /home/vagrant/.kube/config
|
path: /home/vagrant/.kube/config
|
||||||
regexp: "127.0.0.1:6443"
|
regexp: "127.0.0.1:6443"
|
||||||
replace: "192.168.56.80:6443"
|
replace: "{{ vm1_ip }}:6443"
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,19 @@
|
||||||
hosts: vm1,vm2,vm3
|
hosts: vm1,vm2,vm3
|
||||||
become: true
|
become: true
|
||||||
become_user: root
|
become_user: root
|
||||||
serial: 1 # Ensure tasks are executed one host at a time
|
serial: 1 # Ensure tasks are executed one host at a time
|
||||||
|
vars_files:
|
||||||
|
- vars.yaml
|
||||||
|
|
||||||
tasks:
|
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
|
- name: Check if k3s is already installed
|
||||||
ansible.builtin.stat:
|
ansible.builtin.stat:
|
||||||
path: /usr/local/bin/k3s
|
path: /usr/local/bin/k3s
|
||||||
|
|
@ -20,10 +30,10 @@
|
||||||
- name: Generate and save k3s token if not present (first node)
|
- name: Generate and save k3s token if not present (first node)
|
||||||
ansible.builtin.copy:
|
ansible.builtin.copy:
|
||||||
dest: /opt/k3s-token
|
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
|
owner: root
|
||||||
group: root
|
group: root
|
||||||
mode: '0600'
|
mode: "0600"
|
||||||
force: false
|
force: false
|
||||||
register: generated_k3s_token
|
register: generated_k3s_token
|
||||||
when: inventory_hostname == 'vm1' and not k3s_token_file.stat.exists
|
when: inventory_hostname == 'vm1' and not k3s_token_file.stat.exists
|
||||||
|
|
@ -32,21 +42,20 @@
|
||||||
ansible.builtin.get_url:
|
ansible.builtin.get_url:
|
||||||
url: https://get.k3s.io
|
url: https://get.k3s.io
|
||||||
dest: /tmp/k3s_install.sh
|
dest: /tmp/k3s_install.sh
|
||||||
mode: '0755'
|
mode: "0755"
|
||||||
when: not k3s_binary.stat.exists
|
when: not k3s_binary.stat.exists
|
||||||
|
|
||||||
- name: Ensure .kube directory exists
|
- name: Ensure .kube directory exists
|
||||||
ansible.builtin.file:
|
ansible.builtin.file:
|
||||||
path: /home/user/.kube
|
path: /home/user/.kube
|
||||||
state: directory
|
state: directory
|
||||||
mode: '0755'
|
mode: "0755"
|
||||||
when: inventory_hostname == 'vm1' and not k3s_binary.stat.exists
|
when: inventory_hostname == 'vm1' and not k3s_binary.stat.exists
|
||||||
|
|
||||||
- name: Install k3s on first node
|
- name: Install k3s on first node
|
||||||
ansible.builtin.shell: |
|
ansible.builtin.shell: |
|
||||||
set -o pipefail
|
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 {{ vm1_ip }} --node-name vm1 --node-ip {{ vm1_ip }}
|
||||||
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
|
|
||||||
if [ $? -eq 0 ]; then
|
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
|
mkdir -p /home/vagrant/.kube && cp /etc/rancher/k3s/k3s.yaml /home/vagrant/.kube/config && chown vagrant:vagrant /home/vagrant/.kube/config
|
||||||
fi
|
fi
|
||||||
|
|
@ -64,7 +73,7 @@
|
||||||
|
|
||||||
- name: Wait for k3s API server to be ready on master node
|
- name: Wait for k3s API server to be ready on master node
|
||||||
ansible.builtin.wait_for:
|
ansible.builtin.wait_for:
|
||||||
host: 192.168.56.80
|
host: "{{ vm1_ip }}"
|
||||||
port: 6443
|
port: 6443
|
||||||
timeout: 60
|
timeout: 60
|
||||||
delegate_to: "{{ inventory_hostname }}"
|
delegate_to: "{{ inventory_hostname }}"
|
||||||
|
|
@ -74,15 +83,15 @@
|
||||||
ansible.builtin.shell: |
|
ansible.builtin.shell: |
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
{% if inventory_hostname == 'vm2' %}
|
{% if inventory_hostname == 'vm2' %}
|
||||||
NODE_IP="192.168.56.81"
|
NODE_IP="{{ vm2_ip }}"
|
||||||
{% elif inventory_hostname == 'vm3' %}
|
{% elif inventory_hostname == 'vm3' %}
|
||||||
NODE_IP="192.168.56.82"
|
NODE_IP="{{ vm3_ip }}"
|
||||||
{% else %}
|
{% else %}
|
||||||
NODE_IP="192.168.56.80"
|
NODE_IP="{{ vm1_ip }}"
|
||||||
{% endif %}
|
{% endif %}
|
||||||
K3S_URL=https://192.168.56.80:6443 \
|
K3S_URL=https://{{ vm1_ip }}:6443 \
|
||||||
K3S_TOKEN={{ k3s_token_content.stdout }} \
|
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
|
/bin/bash /tmp/k3s_install.sh 2>&1
|
||||||
exit_code=$?
|
exit_code=$?
|
||||||
if [ $exit_code -ne 0 ]; then
|
if [ $exit_code -ne 0 ]; then
|
||||||
|
|
@ -115,7 +124,7 @@
|
||||||
state: directory
|
state: directory
|
||||||
owner: vagrant
|
owner: vagrant
|
||||||
group: vagrant
|
group: vagrant
|
||||||
mode: '0700'
|
mode: "0700"
|
||||||
|
|
||||||
- name: Copy kubeconfig to vagrant user
|
- name: Copy kubeconfig to vagrant user
|
||||||
ansible.builtin.copy:
|
ansible.builtin.copy:
|
||||||
|
|
@ -123,25 +132,25 @@
|
||||||
dest: /home/vagrant/.kube/config
|
dest: /home/vagrant/.kube/config
|
||||||
owner: vagrant
|
owner: vagrant
|
||||||
group: vagrant
|
group: vagrant
|
||||||
mode: '0600'
|
mode: "0600"
|
||||||
remote_src: true
|
remote_src: true
|
||||||
|
|
||||||
- name: Ensure KUBECONFIG is set in vagrant .bashrc
|
- name: Ensure KUBECONFIG is set in vagrant .bashrc
|
||||||
ansible.builtin.lineinfile:
|
ansible.builtin.lineinfile:
|
||||||
path: /home/vagrant/.bashrc
|
path: /home/vagrant/.bashrc
|
||||||
line: 'export KUBECONFIG=~/.kube/config'
|
line: "export KUBECONFIG=~/.kube/config"
|
||||||
state: present
|
state: present
|
||||||
insertafter: EOF
|
insertafter: EOF
|
||||||
owner: vagrant
|
owner: vagrant
|
||||||
group: vagrant
|
group: vagrant
|
||||||
mode: '0644'
|
mode: "0644"
|
||||||
|
|
||||||
- name: Ensure kubectl completion is sourced in vagrant .bashrc
|
- name: Ensure kubectl completion is sourced in vagrant .bashrc
|
||||||
ansible.builtin.lineinfile:
|
ansible.builtin.lineinfile:
|
||||||
path: /home/vagrant/.bashrc
|
path: /home/vagrant/.bashrc
|
||||||
line: 'source <(kubectl completion bash)'
|
line: "source <(kubectl completion bash)"
|
||||||
state: present
|
state: present
|
||||||
insertafter: EOF
|
insertafter: EOF
|
||||||
owner: vagrant
|
owner: vagrant
|
||||||
group: vagrant
|
group: vagrant
|
||||||
mode: '0644'
|
mode: "0644"
|
||||||
|
|
|
||||||
11
vagrant/dev/ubuntu/ansible/vars.yaml
Normal file
11
vagrant/dev/ubuntu/ansible/vars.yaml
Normal 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"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue