mirror of
https://github.com/k3s-io/k3s-ansible.git
synced 2025-12-25 00:12:37 +01:00
* Prevent multiple tokens in k3s.service.env If site.yml playbook is executed multiple times with different tokens, they will all accumulate in k3s.service.env. They won't do any harm because the last one wins, however it is a matter of good housekeeping to delete the old before inserting a new one. Signed-off-by: Marko Vukovic <8951449+anon-software@users.noreply.github.com> * Selectively remove existing token from the environment file If the existing token in the environment file is the same as the token used for the playbook run, leave it in the file to avoid false changed status from the task. Signed-off-by: Marko Vukovic <8951449+anon-software@users.noreply.github.com> --------- Signed-off-by: Marko Vukovic <8951449+anon-software@users.noreply.github.com>
67 lines
2.3 KiB
YAML
67 lines
2.3 KiB
YAML
---
|
|
- name: Get k3s installed version
|
|
ansible.builtin.command: k3s --version
|
|
register: k3s_version_output
|
|
changed_when: false
|
|
ignore_errors: true
|
|
|
|
- name: Set k3s installed version
|
|
when: k3s_version_output.rc == 0
|
|
ansible.builtin.set_fact:
|
|
installed_k3s_version: "{{ k3s_version_output.stdout_lines[0].split(' ')[2] }}"
|
|
|
|
# If airgapped, all K3s artifacts are already on the node.
|
|
# We should be downloading and installing the newer version only if we are in one of the following cases :
|
|
# - 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 artifact 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: Delete any existing token from the environment if different from the new one
|
|
ansible.builtin.lineinfile:
|
|
state: absent
|
|
path: "{{ systemd_dir }}/k3s-agent.service.env"
|
|
regexp: "^K3S_TOKEN=\\s*(?!{{ token }}\\s*$)"
|
|
|
|
- name: Add the token for joining the cluster to the environment
|
|
no_log: true # avoid logging the server token
|
|
ansible.builtin.lineinfile:
|
|
path: "{{ systemd_dir }}/k3s-agent.service.env"
|
|
line: "{{ item }}"
|
|
with_items:
|
|
- "K3S_TOKEN={{ token }}"
|
|
|
|
- name: Copy K3s service file
|
|
register: k3s_agent_service
|
|
ansible.builtin.template:
|
|
src: "k3s-agent.service.j2"
|
|
dest: "{{ systemd_dir }}/k3s-agent.service"
|
|
owner: root
|
|
group: root
|
|
mode: "u=rw,g=r,o=r"
|
|
|
|
- name: Enable and check K3s service
|
|
ansible.builtin.systemd:
|
|
name: k3s-agent
|
|
daemon_reload: "{{ true if k3s_agent_service.changed else false }}"
|
|
state: "{{ 'restarted' if k3s_agent_service.changed else 'started' }}"
|
|
enabled: true
|