xoxys.k3s/tasks/init.yml

55 lines
1.7 KiB
YAML
Raw Normal View History

2022-10-03 20:58:05 +00:00
---
2024-02-18 20:09:43 +00:00
- name: Ensure k3s-init service is stopped
ansible.builtin.service:
name: k3s-init
state: stopped
failed_when: False
2022-10-03 20:58:05 +00:00
2024-02-18 20:09:43 +00:00
- name: Clean previous runs of k3s-init
ansible.builtin.command: systemctl reset-failed k3s-init # noqa command-instead-of-module
2024-02-18 20:09:43 +00:00
failed_when: False
changed_when: False
2022-10-03 20:58:05 +00:00
2024-02-18 20:09:43 +00:00
- name: Init cluster inside the transient k3s-init service
ansible.builtin.command:
cmd: >-
systemd-run
-p RestartSec=2
-p Restart=on-failure
--unit=k3s-init
{{ __k3s_binary }} server {{ __k3s_server_init_args }}
creates: "{{ __k3s_service_file }}"
register: __k3s_init
2022-10-03 20:58:05 +00:00
- name: Wait for k3s-init to finish
when: __k3s_init.changed | bool # noqa no-handler
2024-02-18 20:09:43 +00:00
block:
2022-10-03 20:58:05 +00:00
- name: Verify that all nodes actually joined
2024-02-18 20:09:43 +00:00
ansible.builtin.command: >-
2022-10-17 19:26:56 +00:00
{{ __k3s_binary }} kubectl get nodes
2022-11-17 14:48:27 +00:00
-l "node-role.kubernetes.io/control-plane=true"
2022-10-03 20:58:05 +00:00
-o=jsonpath="{.items[*].metadata.name}"
register: __k3s_init_nodes
until: __k3s_init_nodes.rc == 0 and (__k3s_init_nodes.stdout.split() | length) == (k3s_server_nodes | length)
retries: 30
2022-10-03 20:58:05 +00:00
delay: 10
changed_when: False
always:
- name: Fetch k3s-init logs
2024-02-18 20:09:43 +00:00
ansible.builtin.command: journalctl --all --unit=k3s-init.service
2022-10-03 20:58:05 +00:00
changed_when: False
register: __k3s_init_log
2022-10-04 08:13:40 +00:00
2022-10-03 20:58:05 +00:00
- name: Save k3s-init logs
2024-02-18 20:09:43 +00:00
ansible.builtin.copy:
2022-10-03 20:58:05 +00:00
content: "{{ __k3s_init_log.stdout }}"
2022-10-05 20:54:36 +00:00
dest: "{{ __k3s_log_dir }}/k3s-init.log"
2024-02-18 20:09:43 +00:00
mode: "0640"
2022-10-03 20:58:05 +00:00
when: k3s_init_log_enabled | bool
2022-10-04 08:13:40 +00:00
2022-10-03 20:58:05 +00:00
- name: Kill the temporary service used for initialization
2024-02-18 20:09:43 +00:00
ansible.builtin.service:
2022-10-03 20:58:05 +00:00
name: k3s-init
state: stopped
failed_when: False