|
31 | 31 | # INSTALL_K3S_SKIP_START does work on upgrades, because the service is already installed and started. |
32 | 32 | - name: Stop K3s service |
33 | 33 | when: k3s_upgrade_current_version is version(k3s_version, '<') |
34 | | - ansible.builtin.systemd: |
| 34 | + ansible.builtin.service: |
35 | 35 | state: stopped |
36 | 36 | name: "{{ (server_group in group_names) | ternary('k3s', 'k3s-agent') }}" |
37 | 37 |
|
|
44 | 44 | register: k3s_upgrade_old_token |
45 | 45 | changed_when: false |
46 | 46 |
|
47 | | - - name: Install new K3s Version |
| 47 | + - name: Install new K3s Version [server] |
48 | 48 | # For some reason, ansible-lint thinks using enviroment with command is an error |
49 | 49 | # even though its valid https://ansible.readthedocs.io/projects/lint/rules/inline-env-var/#correct-code |
50 | 50 | # Skip if only reconfiguring (no version change needed) |
51 | | - when: k3s_upgrade_current_version is version(k3s_version, '<') |
| 51 | + when: |
| 52 | + - k3s_upgrade_current_version is version(k3s_version, '<') |
| 53 | + - server_group in group_names |
52 | 54 | ansible.builtin.command: # noqa inline-env-var |
53 | 55 | cmd: /usr/local/bin/k3s-install.sh |
54 | 56 | environment: >- |
55 | 57 | {{ extra_install_envs |
56 | 58 | | combine({ |
57 | 59 | "INSTALL_K3S_SKIP_START": "true", |
58 | 60 | "INSTALL_K3S_VERSION": k3s_version, |
59 | | - "INSTALL_K3S_EXEC": ( "agent" if agent_group in group_names else "server" ) |
60 | 61 | }) |
61 | 62 | | combine(airgap_dir is defined and {"INSTALL_K3S_SKIP_DOWNLOAD": "true"} or {}) }} |
62 | 63 | changed_when: true |
63 | 64 |
|
| 65 | + - name: Install new K3s Version [agent] |
| 66 | + # For some reason, ansible-lint thinks using enviroment with command is an error |
| 67 | + # even though its valid https://ansible.readthedocs.io/projects/lint/rules/inline-env-var/#correct-code |
| 68 | + # Unlike server, we always run the install command, because we are using it to reconfigure the ENV and Args passed to k3s-agent. |
| 69 | + # Instead we just skip the download/replace if airgapped or no version change is needed. |
| 70 | + when: |
| 71 | + - agent_group in group_names |
| 72 | + # noqa var-naming[no-role-prefix] |
| 73 | + ansible.builtin.command: |
| 74 | + cmd: /usr/local/bin/k3s-install.sh |
| 75 | + environment: "{{ _install_envs }}" |
| 76 | + vars: |
| 77 | + _base_envs: |
| 78 | + INSTALL_K3S_SKIP_DOWNLOAD: "{{ (airgap_dir is defined or k3s_upgrade_current_version == k3s_version) | ternary('true', 'false') }}" |
| 79 | + INSTALL_K3S_SKIP_START: "true" |
| 80 | + INSTALL_K3S_SYSTEMD_DIR: "{{ systemd_dir }}" |
| 81 | + INSTALL_K3S_VERSION: "{{ k3s_version }}" |
| 82 | + INSTALL_K3S_EXEC: "agent --server https://{{ api_endpoint }}:{{ api_port }} {{ extra_agent_args }}" |
| 83 | + K3S_TOKEN: "{{ token if token is defined else k3s_upgrade_old_token.stdout }}" |
| 84 | + # We overrides the extra_install_envs with required keys from _base_envs on purpose |
| 85 | + _install_envs: "{{ extra_install_envs | default({}) | combine(_base_envs) }}" |
| 86 | + changed_when: true |
| 87 | + |
64 | 88 | - name: Regenerate K3s service file [server] |
65 | 89 | when: server_group in group_names |
66 | 90 | block: |
|
125 | 149 | cluster_init: false |
126 | 150 | join: true |
127 | 151 |
|
128 | | - - name: Regenerate K3s service file [agent] |
129 | | - when: |
130 | | - - agent_group in group_names |
131 | | - - api_endpoint is defined |
132 | | - ansible.builtin.replace: |
133 | | - path: "{{ systemd_dir }}/k3s-agent.service" |
134 | | - regexp: '^ExecStart=\/usr\/local\/bin\/k3s \\\n\s*agent.*(?:\n(?:[\t\s].*|$))*' |
135 | | - replace: | |
136 | | - ExecStart=/usr/local/bin/k3s \ |
137 | | - agent \ |
138 | | - --server https://{{ api_endpoint }}:{{ api_port }} \ |
139 | | - {{ extra_agent_args | default('') }} |
140 | | -
|
141 | | - - name: Add token to the environment |
| 152 | + - name: Add token to the environment [server] |
| 153 | + when: server_group in group_names |
142 | 154 | no_log: true # avoid logging the server token |
143 | 155 | ansible.builtin.lineinfile: |
144 | | - path: "{{ systemd_dir }}/{{ (agent_group in group_names) | ternary('k3s-agent.service.env', 'k3s.service.env') }}" |
| 156 | + path: "{{ systemd_dir }}/k3s.service.env" |
145 | 157 | regexp: '^K3S_TOKEN=' |
146 | 158 | line: "K3S_TOKEN={{ token is defined | ternary(token, k3s_upgrade_old_token.stdout) }}" |
147 | 159 |
|
|
154 | 166 |
|
155 | 167 | - name: Restart K3s service [agent] |
156 | 168 | when: agent_group in group_names |
157 | | - ansible.builtin.systemd: |
| 169 | + ansible.builtin.service: |
158 | 170 | state: restarted |
159 | | - daemon_reload: true |
160 | 171 | name: k3s-agent |
0 commit comments