prole/infrastructure/roles/k3s/tasks/start.yml

52 lines
1.8 KiB
YAML

---
- name: Set k3s service name
ansible.builtin.set_fact:
k3s_service_name: "{{ 'k3s' if k3s_role == 'server' else 'k3s-agent' }}"
- name: Gather service facts
ansible.builtin.service_facts:
- name: Build k3s install environment when service is missing
ansible.builtin.set_fact:
k3s_install_env: >-
{{ {'INSTALL_K3S_EXEC': k3s_role}
| combine((k3s_version | default('') | length > 0) | ternary({'INSTALL_K3S_VERSION': k3s_version}, {})) }}
when: (k3s_service_name + ".service") not in ansible_facts.services
- name: Install k3s when service is missing
ansible.builtin.shell: |
set -euo pipefail
timeout "{{ k3s_install_timeout | default(600) }}" sh -c \
'curl -sfL --connect-timeout 20 --max-time 300 https://get.k3s.io | sh -'
environment:
"{{ k3s_install_env }}"
args:
creates: /usr/local/bin/k3s
executable: /bin/bash
when: (k3s_service_name + ".service") not in ansible_facts.services
- name: Refresh service facts after install
ansible.builtin.service_facts:
- name: Ensure k3s service is installed
ansible.builtin.assert:
that:
- (k3s_service_name + ".service") in ansible_facts.services
fail_msg: "k3s service {{ k3s_service_name }} is not installed. Run the k3s role or reset playbook first."
- name: Start k3s service
ansible.builtin.systemd:
name: "{{ k3s_service_name }}"
state: started
enabled: true
no_block: true
- name: Wait for k3s to become active
ansible.builtin.command: "systemctl is-active {{ k3s_service_name }}"
register: k3s_service_state
changed_when: false
retries: "{{ [1, (k3s_start_timeout_seconds | default(120) | int) // 10] | max }}"
delay: 10
until: k3s_service_state.stdout in ["active", "activating"]
failed_when: k3s_service_state.stdout not in ["active", "activating"]