ansible/playbooks/tasks/k3s-node.yml
2026-10-11 22:29:14 +02:00

74 lines
2.4 KiB
YAML

---
# Install, upgrade or reconfigure k3s on one node. Included by deploy_kube.yml,
# one node at a time. Does nothing when the version and config already match.
- name: Create /etc/rancher/k3s
ansible.builtin.file:
path: /etc/rancher/k3s
state: directory
owner: root
group: root
mode: "0755"
# Content only: k3s restarts when this task changes, not for a mode fix.
- name: Write /etc/rancher/k3s/config.yaml
ansible.builtin.template:
src: k3s-config.yaml.j2
dest: /etc/rancher/k3s/config.yaml
diff: false # holds the join token
register: k3s_config
# The k3s install leaves it 0644, readable by every local user.
- name: Make the config readable by root only
ansible.builtin.file:
path: /etc/rancher/k3s/config.yaml
owner: root
group: root
mode: "0600"
- name: Read the installed k3s version
ansible.builtin.command: /usr/local/bin/k3s --version
register: k3s_installed
changed_when: false
failed_when: false
check_mode: false
- name: Decide whether k3s must be installed
ansible.builtin.set_fact:
k3s_needs_install: "{{ k3s_installed.rc != 0 or k3s_version not in k3s_installed.stdout }}"
- name: Report the version change
ansible.builtin.debug:
msg: "{{ inventory_hostname }}: {{ k3s_installed.stdout_lines | first | default('k3s not installed') }} -> {{ k3s_version }}"
when: k3s_needs_install
- name: Download the install script of this k3s version
ansible.builtin.get_url:
url: https://raw.githubusercontent.com/k3s-io/k3s/{{ k3s_version }}/install.sh
dest: /usr/local/bin/k3s-install.sh
mode: "0755"
when: k3s_needs_install
- name: Install k3s {{ k3s_version }}
ansible.builtin.command: /usr/local/bin/k3s-install.sh
environment:
INSTALL_K3S_VERSION: "{{ k3s_version }}"
INSTALL_K3S_EXEC: "{{ k3s_role }}"
changed_when: true
when: k3s_needs_install
- name: Restart k3s for the new config
ansible.builtin.systemd_service:
name: "{{ k3s_service }}"
state: restarted
when: k3s_config is changed and not k3s_needs_install
- name: Wait for the node to be Ready
ansible.builtin.command: k3s kubectl wait --for=condition=Ready node/{{ inventory_hostname }} --timeout=30s
delegate_to: "{{ k3s_init_host }}"
register: k3s_ready
until: k3s_ready.rc == 0
retries: 20
delay: 15
changed_when: false
when: (k3s_needs_install or k3s_config is changed) and not ansible_check_mode