--- - name: Install k3s agents and join them to master hosts: master become: yes vars: master_ip: "47.113.186.215" k3s_download_url: "http://download.9981.tech/k3s-v1.35.0%2Bk3s1" tasks: - name: Fetch the node token from master slurp: src: /var/lib/rancher/k3s/server/node-token register: node_token_result - name: Set node token fact set_fact: node_token: "{{ node_token_result.content | b64decode | trim }}" - name: Copy node token to worker nodes copy: content: "{{ node_token }}" dest: /root/node-token delegate_to: "{{ item }}" loop: - "101.201.78.54" - "47.120.61.39" delegate_facts: yes - name: Install k3s on worker nodes and join to cluster hosts: workers become: yes vars: master_ip: "47.113.186.215" k3s_download_url: "http://download.9981.tech/k3s-v1.35.0%2Bk3s1" tasks: - name: Stop and cleanup any existing k3s installation on workers shell: | systemctl stop k3s-agent 2>/dev/null || true pkill -f k3s 2>/dev/null || true rm -rf /var/lib/rancher/k3s rm -f /etc/systemd/system/k3s-agent.service systemctl daemon-reload 2>/dev/null || true register: cleanup_result ignore_errors: yes - name: Print cleanup status debug: msg: "Cleanup completed on worker node {{ inventory_hostname }}" - name: Check if k3s binary exists stat: path: /usr/local/bin/k3s register: k3s_binary - name: Download k3s binary get_url: url: "{{ k3s_download_url }}" dest: /usr/local/bin/k3s mode: '0755' when: not k3s_binary.stat.exists - name: Print k3s binary status debug: msg: "k3s binary already exists on worker {{ inventory_hostname }}, skipping download" when: k3s_binary.stat.exists - name: Create k3s-agent systemd service file template: src: k3s-agent.service.j2 dest: /etc/systemd/system/k3s-agent.service mode: '0644' - name: Reload systemd daemon systemd: daemon_reload: yes - name: Enable and start k3s-agent service systemd: name: k3s-agent enabled: yes state: started - name: Wait for k3s-agent to start wait_for: port: 10250 host: "{{ inventory_hostname }}" timeout: 300 delay: 10 ignore_errors: yes - name: Display success message debug: msg: "k3s agent installed and joined to cluster on worker {{ inventory_hostname }}"