Check K3s installed version before download tasks (#297)
- [Agent : Download artefact only if needed](roles/k3s_agent/tasks/main.yml#L13) - [Server : Download artefact only if needed](roles/k3s_server/tasks/main.yml#L13) - [Upgrade : Upgrade node only if needed](roles/k3s_upgrade/tasks/main.yml#L14) Linked issue #264 k3s_server and k3s_agent tasks are not idempotent Signed-off-by: Loïc Dubard <loic97429@gmail.com>
This commit is contained in:
parent
1e266a52f9
commit
a2916230ba
|
@ -1,24 +1,39 @@
|
||||||
---
|
---
|
||||||
# If airgapped, all K3s artifacts are already on the node.
|
- name: Get k3s installed version
|
||||||
- name: Download K3s install script
|
ansible.builtin.command: k3s --version
|
||||||
when: airgap_dir is undefined
|
register: k3s_version_output
|
||||||
ansible.builtin.get_url:
|
changed_when: false
|
||||||
url: https://get.k3s.io/
|
ignore_errors: true
|
||||||
timeout: 120
|
|
||||||
dest: /usr/local/bin/k3s-install.sh
|
|
||||||
owner: root
|
|
||||||
group: root
|
|
||||||
mode: 0755
|
|
||||||
|
|
||||||
- name: Download K3s binary
|
- name: Set k3s installed version
|
||||||
when: airgap_dir is undefined
|
when: k3s_version_output.rc == 0
|
||||||
ansible.builtin.command:
|
ansible.builtin.set_fact:
|
||||||
cmd: /usr/local/bin/k3s-install.sh
|
installed_k3s_version: "{{ k3s_version_output.stdout_lines[0].split(' ')[2] }}"
|
||||||
environment:
|
|
||||||
INSTALL_K3S_SKIP_START: "true"
|
# If airgapped, all K3s artifacts are already on the node.
|
||||||
INSTALL_K3S_VERSION: "{{ k3s_version }}"
|
# We should be downloading and installing the newer version only if we are in one of the following cases :
|
||||||
INSTALL_K3S_EXEC: "agent"
|
# - we couldn't get k3s installed version in the first task of this role
|
||||||
changed_when: true
|
# - the installed version of K3s on the nodes is older than the requested version in ansible vars
|
||||||
|
- name: Download artefact only if needed
|
||||||
|
when: k3s_version_output.rc != 0 or installed_k3s_version is version(k3s_version, '<') and airgap_dir is undefined
|
||||||
|
block:
|
||||||
|
- name: Download K3s install script
|
||||||
|
ansible.builtin.get_url:
|
||||||
|
url: https://get.k3s.io/
|
||||||
|
timeout: 120
|
||||||
|
dest: /usr/local/bin/k3s-install.sh
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: 0755
|
||||||
|
|
||||||
|
- name: Download K3s binary
|
||||||
|
ansible.builtin.command:
|
||||||
|
cmd: /usr/local/bin/k3s-install.sh
|
||||||
|
environment:
|
||||||
|
INSTALL_K3S_SKIP_START: "true"
|
||||||
|
INSTALL_K3S_VERSION: "{{ k3s_version }}"
|
||||||
|
INSTALL_K3S_EXEC: "agent"
|
||||||
|
changed_when: true
|
||||||
|
|
||||||
- name: Copy K3s service file
|
- name: Copy K3s service file
|
||||||
register: k3s_agent_service
|
register: k3s_agent_service
|
||||||
|
|
|
@ -1,23 +1,38 @@
|
||||||
---
|
---
|
||||||
# If airgapped, all K3s artifacts are already on the node.
|
- name: Get k3s installed version
|
||||||
- name: Download K3s install script
|
ansible.builtin.command: k3s --version
|
||||||
when: airgap_dir is undefined
|
register: k3s_version_output
|
||||||
ansible.builtin.get_url:
|
changed_when: false
|
||||||
url: https://get.k3s.io/
|
ignore_errors: true
|
||||||
timeout: 120
|
|
||||||
dest: /usr/local/bin/k3s-install.sh
|
|
||||||
owner: root
|
|
||||||
group: root
|
|
||||||
mode: 0755
|
|
||||||
|
|
||||||
- name: Download K3s binary
|
- name: Set k3s installed version
|
||||||
when: airgap_dir is undefined
|
when: k3s_version_output.rc == 0
|
||||||
ansible.builtin.command:
|
ansible.builtin.set_fact:
|
||||||
cmd: /usr/local/bin/k3s-install.sh
|
installed_k3s_version: "{{ k3s_version_output.stdout_lines[0].split(' ')[2] }}"
|
||||||
environment:
|
|
||||||
INSTALL_K3S_SKIP_START: "true"
|
# If airgapped, all K3s artifacts are already on the node.
|
||||||
INSTALL_K3S_VERSION: "{{ k3s_version }}"
|
# We should be downloading and installing the newer version only if we are in one of the following cases :
|
||||||
changed_when: true
|
# - we couldn't get k3s installed version in the first task of this role
|
||||||
|
# - the installed version of K3s on the nodes is older than the requested version in ansible vars
|
||||||
|
- name: Download artefact only if needed
|
||||||
|
when: k3s_version_output.rc != 0 or installed_k3s_version is version(k3s_version, '<') and airgap_dir is undefined
|
||||||
|
block:
|
||||||
|
- name: Download K3s install script
|
||||||
|
ansible.builtin.get_url:
|
||||||
|
url: https://get.k3s.io/
|
||||||
|
timeout: 120
|
||||||
|
dest: /usr/local/bin/k3s-install.sh
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: 0755
|
||||||
|
|
||||||
|
- name: Download K3s binary
|
||||||
|
ansible.builtin.command:
|
||||||
|
cmd: /usr/local/bin/k3s-install.sh
|
||||||
|
environment:
|
||||||
|
INSTALL_K3S_SKIP_START: "true"
|
||||||
|
INSTALL_K3S_VERSION: "{{ k3s_version }}"
|
||||||
|
changed_when: true
|
||||||
|
|
||||||
- name: Add K3s autocomplete to user bashrc
|
- name: Add K3s autocomplete to user bashrc
|
||||||
ansible.builtin.lineinfile:
|
ansible.builtin.lineinfile:
|
||||||
|
|
|
@ -1,34 +1,48 @@
|
||||||
---
|
---
|
||||||
# with_fileglob doesn't work with remote_src, it tries to find the file on the
|
# with_fileglob doesn't work with remote_src, it tries to find the file on the
|
||||||
# local control-plane instead of the remote host. Shell supports wildcards.
|
# local control-plane instead of the remote host. Shell supports wildcards.
|
||||||
- name: Save current K3s service
|
- name: Get k3s installed version
|
||||||
ansible.builtin.shell:
|
ansible.builtin.command: k3s --version
|
||||||
cmd: "cp {{ systemd_dir }}/k3s*.service /tmp/"
|
register: k3s_version_output
|
||||||
changed_when: true
|
changed_when: false
|
||||||
|
|
||||||
- name: Install new K3s Version
|
- name: Set k3s installed version
|
||||||
ansible.builtin.command:
|
ansible.builtin.set_fact:
|
||||||
cmd: /usr/local/bin/k3s-install.sh
|
installed_k3s_version: "{{ k3s_version_output.stdout_lines[0].split(' ')[2] }}"
|
||||||
environment:
|
|
||||||
INSTALL_K3S_SKIP_START: "true"
|
|
||||||
INSTALL_K3S_VERSION: "{{ k3s_version }}"
|
|
||||||
changed_when: true
|
|
||||||
|
|
||||||
- name: Restore K3s service
|
# We should be downloading and installing the newer version only if we are in the following case :
|
||||||
ansible.builtin.shell:
|
# - the installed version of K3s on the nodes is older than the requested version in ansible vars
|
||||||
cmd: "mv /tmp/k3s*.service {{ systemd_dir }}/"
|
- name: Update node only if needed
|
||||||
changed_when: true
|
when: installed_k3s_version is version(k3s_version, '<')
|
||||||
|
block:
|
||||||
|
- name: Save current K3s service
|
||||||
|
ansible.builtin.shell:
|
||||||
|
cmd: "cp {{ systemd_dir }}/k3s*.service /tmp/"
|
||||||
|
changed_when: true
|
||||||
|
|
||||||
- name: Restart K3s service [server]
|
- name: Install new K3s Version
|
||||||
when: "'server' in group_names"
|
ansible.builtin.command:
|
||||||
ansible.builtin.systemd:
|
cmd: /usr/local/bin/k3s-install.sh
|
||||||
state: restarted
|
environment:
|
||||||
daemon_reload: true
|
INSTALL_K3S_SKIP_START: "true"
|
||||||
name: k3s
|
INSTALL_K3S_VERSION: "{{ k3s_version }}"
|
||||||
|
changed_when: true
|
||||||
|
|
||||||
- name: Restart K3s service [agent]
|
- name: Restore K3s service
|
||||||
when: "'agent' in group_names"
|
ansible.builtin.shell:
|
||||||
ansible.builtin.systemd:
|
cmd: "mv /tmp/k3s*.service {{ systemd_dir }}/"
|
||||||
state: restarted
|
changed_when: true
|
||||||
daemon_reload: true
|
|
||||||
name: k3s-agent
|
- name: Restart K3s service [server]
|
||||||
|
when: "'server' in group_names"
|
||||||
|
ansible.builtin.systemd:
|
||||||
|
state: restarted
|
||||||
|
daemon_reload: true
|
||||||
|
name: k3s
|
||||||
|
|
||||||
|
- name: Restart K3s service [agent]
|
||||||
|
when: "'agent' in group_names"
|
||||||
|
ansible.builtin.systemd:
|
||||||
|
state: restarted
|
||||||
|
daemon_reload: true
|
||||||
|
name: k3s-agent
|
||||||
|
|
Loading…
Reference in New Issue