xoxys.k3s/tasks/setup.yml
Robert Kaussow 5cfc5bc86c
Some checks failed
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline failed
ci/woodpecker/push/docs unknown status
ci/woodpecker/push/notify Pipeline was successful
ci: migrate to woodpecker
2024-02-18 21:09:43 +01:00

93 lines
2.9 KiB
YAML

---
- name: Copy K3s service file
ansible.builtin.template:
src: "etc/systemd/system/k3s.service.j2"
dest: "{{ __k3s_service_file }}"
owner: root
group: root
mode: "0644"
notify: __k3s_restart
- name: Force restart
ansible.builtin.meta: flush_handlers
- name: Ensure K3s service is up and running
ansible.builtin.service:
name: k3s
daemon_reload: True
enabled: True
state: started
- name: Wait for Kubernetes API
ansible.builtin.command: >-
{{ __k3s_binary }} kubectl get --raw='/readyz'
register: __k3s_api_readyz
until: __k3s_api_readyz.rc == 0 and __k3s_api_readyz.stdout == "ok"
retries: 30
delay: 10
changed_when: False
when: k3s_server | bool
- name: Set server address
ansible.builtin.command: >-
{{ __k3s_binary }} kubectl config set-cluster default
--server=https://{{ __k3s_server_ip }}:6443
--kubeconfig {{ __k3s_config_dir }}/k3s.yaml
changed_when: False
- name: Create setup resources
kubernetes.core.k8s:
kind: "{{ item.kind }}"
name: "{{ item.name }}"
kubeconfig: "{{ __k3s_config_dir }}/k3s.yaml"
state: present
definition: "{{ item.definition }}"
loop: "{{ k3s_server_resource_creations }}"
loop_control:
label: "{{ item.kind | lower }}/{{ item.name | lower }}"
when: k3s_server | bool
- name: Wait for initial setup
ansible.builtin.command: >-
{{ __k3s_binary }} kubectl get deployment coredns
-n kube-system
-o go-template={% raw %}'{{ .status.availableReplicas }}'{% endraw %}
register: __k3s_init_setup
until: __k3s_init_setup.rc == 0 and __k3s_init_setup.stdout == "1"
retries: 30
delay: 10
changed_when: False
when: k3s_server | bool
- name: Add node role label
kubernetes.core.k8s:
kind: "Node"
name: "{{ k3s_node_name }}"
kubeconfig: "{{ __k3s_config_dir }}/k3s.yaml"
state: patched
definition:
metadata:
labels: "{{ (__k3s_node_lables['control'] if k3s_server | bool else __k3s_node_lables['worker']) | combine(__k3s_node_lables['general'], k3s_node_labels) }}"
delegate_to: "{{ hostvars[k3s_server_nodes[0]]['inventory_hostname'] }}"
- name: Patch existing resources
kubernetes.core.k8s:
kind: "{{ item.kind }}"
name: "{{ item.name }}"
kubeconfig: "{{ __k3s_config_dir }}/k3s.yaml"
state: patched
definition: "{{ item.definition }}"
loop: "{{ k3s_server_resource_patches }}"
loop_control:
label: "{{ item.kind | lower }}/{{ item.name | lower }}"
when: k3s_server | bool
- name: Remove auto-deploying manifests
ansible.builtin.file:
path: "{{ __k3s_manifests_dir }}/{{ item.dest | default(item.url) | default(item) | basename | replace('.j2', '') }}"
state: absent
loop: "{{ k3s_server_manifests_urls + k3s_server_manifests_templates }}"
loop_control:
label: "{{ __k3s_manifests_dir }}/{{ item.dest | default(item.url) | default(item) | basename | replace('.j2', '') }}"
when: ansible_hostname == hostvars[k3s_server_nodes[0]]['ansible_hostname']