2022-10-03 20:58:05 +00:00
|
|
|
---
|
|
|
|
- block:
|
|
|
|
- name: Copy K3s service file
|
|
|
|
template:
|
|
|
|
src: "etc/systemd/system/k3s.service.j2"
|
|
|
|
dest: "{{ __k3s_service_file }}"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: 0644
|
|
|
|
notify: __k3s_restart
|
|
|
|
|
|
|
|
- name: Force restart
|
|
|
|
meta: flush_handlers
|
|
|
|
|
|
|
|
- name: Ensure K3s service is up and running
|
|
|
|
service:
|
|
|
|
name: k3s
|
|
|
|
daemon_reload: True
|
|
|
|
enabled: True
|
|
|
|
state: started
|
|
|
|
|
2022-11-16 21:17:08 +00:00
|
|
|
- name: Wait for Kubernetes API
|
|
|
|
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
|
|
|
|
|
2022-10-03 20:58:05 +00:00
|
|
|
- name: Set server address
|
|
|
|
command: >-
|
2022-10-17 19:26:56 +00:00
|
|
|
{{ __k3s_binary }} kubectl config set-cluster default
|
2022-10-03 20:58:05 +00:00
|
|
|
--server=https://{{ __k3s_server_ip }}:6443
|
2022-10-05 20:54:36 +00:00
|
|
|
--kubeconfig {{ __k3s_config_dir }}/k3s.yaml
|
2022-10-03 20:58:05 +00:00
|
|
|
changed_when: False
|
|
|
|
|
2022-11-16 21:17:08 +00:00
|
|
|
- name: Create setup resources
|
|
|
|
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
|
|
|
|
|
2022-10-05 20:54:36 +00:00
|
|
|
- name: Wait for initial setup
|
|
|
|
command: >-
|
2022-10-17 19:26:56 +00:00
|
|
|
{{ __k3s_binary }} kubectl get deployment coredns
|
2022-10-05 20:54:36 +00:00
|
|
|
-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"
|
2022-10-27 19:22:44 +00:00
|
|
|
retries: 30
|
2022-11-16 21:17:08 +00:00
|
|
|
delay: 10
|
2022-10-05 20:54:36 +00:00
|
|
|
changed_when: False
|
2022-10-17 19:26:56 +00:00
|
|
|
when: k3s_server | bool
|
2022-10-05 20:54:36 +00:00
|
|
|
|
2022-10-27 12:11:11 +00:00
|
|
|
- name: Add node role label
|
|
|
|
k8s:
|
|
|
|
kind: "Node"
|
2022-11-18 09:12:48 +00:00
|
|
|
name: "{{ k3s_node_name }}"
|
2022-10-27 12:11:11 +00:00
|
|
|
kubeconfig: "{{ __k3s_config_dir }}/k3s.yaml"
|
2022-11-18 09:12:48 +00:00
|
|
|
state: patched
|
2022-10-27 12:11:11 +00:00
|
|
|
definition:
|
|
|
|
metadata:
|
2022-11-20 13:10:29 +00:00
|
|
|
labels: "{{ (__k3s_node_lables['control'] if k3s_server | bool else __k3s_node_lables['worker']) | combine(__k3s_node_lables['general'], k3s_node_labels) }}"
|
2022-10-27 12:11:11 +00:00
|
|
|
delegate_to: "{{ hostvars[k3s_server_nodes[0]]['inventory_hostname'] }}"
|
|
|
|
|
2022-10-25 07:58:53 +00:00
|
|
|
- name: Patch existing resources
|
|
|
|
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:
|
2022-11-16 21:17:08 +00:00
|
|
|
label: "{{ item.kind | lower }}/{{ item.name | lower }}"
|
2022-10-25 07:58:53 +00:00
|
|
|
when: k3s_server | bool
|
|
|
|
|
2022-10-05 20:54:36 +00:00
|
|
|
- name: Remove auto-deploying manifests
|
|
|
|
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', '') }}"
|
2022-10-17 19:26:56 +00:00
|
|
|
when: ansible_hostname == hostvars[k3s_server_nodes[0]]['ansible_hostname']
|
2022-10-03 20:58:05 +00:00
|
|
|
become: True
|
|
|
|
become_user: root
|