|
| 1 | +--- |
| 2 | +- name: Get k3s installed version |
| 3 | + ansible.builtin.command: k3s --version |
| 4 | + register: k3s_version_output |
| 5 | + changed_when: false |
| 6 | + ignore_errors: true |
| 7 | + |
| 8 | +- name: Set k3s installed version |
| 9 | + when: not ansible_check_mode and k3s_version_output.rc == 0 |
| 10 | + ansible.builtin.set_fact: |
| 11 | + installed_k3s_version: "{{ k3s_version_output.stdout_lines[0].split(' ')[2] }}" |
| 12 | + |
| 13 | +# If airgapped, all K3s artifacts are already on the node. |
| 14 | +# We should be downloading and installing the newer version only if we are in one of the following cases : |
| 15 | +# - we couldn't get k3s installed version in the first task of this role |
| 16 | +# - the installed version of K3s on the nodes is older than the requested version in ansible vars |
| 17 | +- name: Download artifact only if needed |
| 18 | + when: not ansible_check_mode and airgap_dir is undefined and ( k3s_version_output.rc != 0 or installed_k3s_version is version(k3s_version, '<') ) |
| 19 | + block: |
| 20 | + - name: Download K3s install script |
| 21 | + ansible.builtin.get_url: |
| 22 | + url: https://get.k3s.io/ |
| 23 | + timeout: 120 |
| 24 | + dest: /usr/local/bin/k3s-install.sh |
| 25 | + owner: root |
| 26 | + group: root |
| 27 | + mode: "0755" |
| 28 | + |
| 29 | + - name: Download K3s binary |
| 30 | + ansible.builtin.command: |
| 31 | + cmd: /usr/local/bin/k3s-install.sh |
| 32 | + environment: |
| 33 | + INSTALL_K3S_SKIP_START: "true" |
| 34 | + INSTALL_K3S_VERSION: "{{ k3s_version }}" |
| 35 | + INSTALL_K3S_EXEC: "agent" |
| 36 | + changed_when: true |
| 37 | + |
| 38 | +- name: Setup optional config file |
| 39 | + when: agent_config_yaml is defined |
| 40 | + block: |
| 41 | + - name: Make config directory |
| 42 | + ansible.builtin.file: |
| 43 | + path: "/etc/rancher/k3s" |
| 44 | + mode: "0755" |
| 45 | + state: directory |
| 46 | + - name: Copy config values |
| 47 | + ansible.builtin.copy: |
| 48 | + content: "{{ agent_config_yaml }}" |
| 49 | + dest: "/etc/rancher/k3s/config.yaml" |
| 50 | + mode: "0644" |
| 51 | + register: _agent_config_result |
| 52 | + |
| 53 | +- name: Get the token from the first server |
| 54 | + ansible.builtin.set_fact: |
| 55 | + token: "{{ hostvars[groups[server_group][0]].token }}" |
| 56 | + |
| 57 | +- name: Delete any existing token from the environment if different from the new one |
| 58 | + ansible.builtin.lineinfile: |
| 59 | + state: absent |
| 60 | + path: "{{ systemd_dir }}/k3s-agent.service.env" |
| 61 | + regexp: "^K3S_TOKEN=\\s*(?!{{ token }}\\s*$)" |
| 62 | + |
| 63 | +- name: Add the token for joining the cluster to the environment |
| 64 | + no_log: true # avoid logging the server token |
| 65 | + ansible.builtin.lineinfile: |
| 66 | + path: "{{ systemd_dir }}/k3s-agent.service.env" |
| 67 | + line: "{{ item }}" |
| 68 | + with_items: |
| 69 | + - "K3S_TOKEN={{ token }}" |
| 70 | + |
| 71 | +- name: Copy K3s service file |
| 72 | + register: k3s_agent_service |
| 73 | + ansible.builtin.template: |
| 74 | + src: "k3s-agent.service.j2" |
| 75 | + dest: "{{ systemd_dir }}/k3s-agent.service" |
| 76 | + owner: root |
| 77 | + group: root |
| 78 | + mode: "u=rw,g=r,o=r" |
| 79 | + |
| 80 | +- name: Enable and check K3s service |
| 81 | + ansible.builtin.systemd: |
| 82 | + name: k3s-agent |
| 83 | + daemon_reload: "{{ true if k3s_agent_service.changed else false }}" |
| 84 | + state: "{{ 'restarted' if (k3s_agent_service.changed or _agent_config_result.changed) else 'started' }}" |
| 85 | + enabled: true |
0 commit comments