2019-03-09 21:54:44 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
- name: Ensure k3s control node fact is set
|
|
|
|
set_fact:
|
2020-05-16 21:18:10 +02:00
|
|
|
k3s_control_node: "{{ 'false' if k3s_build_cluster else 'true' }}"
|
2019-03-09 21:54:44 +01:00
|
|
|
when: k3s_control_node is not defined
|
|
|
|
|
2020-11-10 20:01:05 +01:00
|
|
|
- name: Ensure k3s primary control node fact is set
|
2020-01-11 23:42:29 +01:00
|
|
|
set_fact:
|
2020-05-16 21:18:10 +02:00
|
|
|
k3s_primary_control_node: "{{ 'false' if k3s_build_cluster else 'true' }}"
|
2020-01-11 23:42:29 +01:00
|
|
|
when: k3s_primary_control_node is not defined
|
|
|
|
|
2019-03-09 21:54:44 +01:00
|
|
|
- name: Ensure a k3s control node is defined if none are found in play_hosts
|
|
|
|
block:
|
2020-05-16 21:18:10 +02:00
|
|
|
- name: Set the control host
|
2019-03-09 21:54:44 +01:00
|
|
|
set_fact:
|
|
|
|
k3s_control_node: true
|
|
|
|
when: inventory_hostname == play_hosts[0]
|
|
|
|
when: true not in (hostvars | json_query('*.k3s_control_node'))
|
2020-05-16 21:18:10 +02:00
|
|
|
and k3s_build_cluster is defined and k3s_build_cluster
|
2019-03-09 21:54:44 +01:00
|
|
|
|
2020-11-10 20:01:05 +01:00
|
|
|
- name: Ensure a count of control nodes is generated
|
2019-03-09 21:54:44 +01:00
|
|
|
set_fact:
|
|
|
|
k3s_controller_count: "{{ k3s_controller_count + [ item ] }}"
|
2020-05-18 20:53:03 +02:00
|
|
|
when: hostvars[item].k3s_control_node is defined
|
|
|
|
and hostvars[item].k3s_control_node
|
2020-01-11 23:42:29 +01:00
|
|
|
loop: "{{ play_hosts }}"
|
|
|
|
|
|
|
|
- name: Ensure a primary k3s control node is defined if multiple are found in play_hosts
|
|
|
|
set_fact:
|
|
|
|
k3s_primary_control_node: true
|
2020-05-18 20:53:03 +02:00
|
|
|
when: k3s_controller_count is defined
|
|
|
|
and k3s_controller_count | length > 1
|
2020-01-11 23:42:29 +01:00
|
|
|
and inventory_hostname == k3s_controller_count[0]
|
2020-05-16 21:18:10 +02:00
|
|
|
and k3s_build_cluster is defined and k3s_build_cluster
|
2020-05-18 20:53:03 +02:00
|
|
|
|
|
|
|
- name: Ensure ansible_host is mapped to inventory_hostname
|
|
|
|
lineinfile:
|
|
|
|
path: /tmp/inventory.txt
|
|
|
|
line: >-
|
|
|
|
{{ item }}
|
|
|
|
@@@
|
|
|
|
{{ hostvars[item].ansible_host | default(hostvars[item].ansible_fqdn) }}
|
|
|
|
@@@
|
|
|
|
C_{{ hostvars[item].k3s_control_node }}
|
|
|
|
@@@
|
|
|
|
P_{{ hostvars[item].k3s_primary_control_node | default(False) }}
|
|
|
|
create: true
|
2020-05-30 16:16:20 +02:00
|
|
|
regexp: "^{{ item }} @@@ {{ hostvars[item].ansible_host | default(hostvars[item].ansible_fqdn) }}"
|
2020-09-15 19:10:25 +02:00
|
|
|
mode: 0600
|
2020-05-18 20:53:03 +02:00
|
|
|
loop: "{{ play_hosts }}"
|
2020-07-25 18:39:01 +02:00
|
|
|
check_mode: false
|
2020-05-18 20:53:03 +02:00
|
|
|
when: hostvars[item].k3s_control_node is defined
|
|
|
|
|
2020-11-10 20:01:05 +01:00
|
|
|
- name: Delegate a control plane node
|
2020-05-25 17:25:09 +02:00
|
|
|
block:
|
|
|
|
- name: Lookup control node from file
|
|
|
|
command: "grep '{{ 'P_True' if (k3s_controller_count | length > 1) else 'C_True' }}' /tmp/inventory.txt"
|
|
|
|
changed_when: false
|
2020-07-25 18:39:01 +02:00
|
|
|
check_mode: false
|
2020-05-25 17:25:09 +02:00
|
|
|
register: k3s_control_delegate_raw
|
2020-05-18 20:53:03 +02:00
|
|
|
|
2020-05-25 17:25:09 +02:00
|
|
|
- name: Ensure control node is delegated to for obtaining a token
|
|
|
|
set_fact:
|
|
|
|
k3s_control_delegate: "{{ k3s_control_delegate_raw.stdout.split(' @@@ ')[0] }}"
|
2020-07-25 18:39:01 +02:00
|
|
|
check_mode: false
|
2020-05-25 18:57:43 +02:00
|
|
|
when: k3s_control_delegate is not defined
|
2020-05-18 20:53:03 +02:00
|
|
|
|
2020-05-25 17:25:09 +02:00
|
|
|
- name: Ensure the control node address is registered in Ansible
|
|
|
|
set_fact:
|
|
|
|
k3s_control_node_address: "{{ hostvars[k3s_control_delegate].ansible_host | default(hostvars[k3s_control_delegate].ansible_fqdn) }}"
|
2020-07-25 18:39:01 +02:00
|
|
|
check_mode: false
|
2020-05-25 18:57:43 +02:00
|
|
|
when: k3s_control_node_address is not defined
|
|
|
|
|
2020-05-18 20:53:03 +02:00
|
|
|
when: k3s_control_node_address is not defined
|
2020-05-25 18:57:43 +02:00
|
|
|
or k3s_control_delegate is not defined
|
2020-10-22 20:26:15 +02:00
|
|
|
|
|
|
|
- name: Ensure k3s_runtime_config is set for control plane
|
|
|
|
set_fact:
|
2020-11-29 21:10:42 +01:00
|
|
|
k3s_runtime_config: "{{ (k3s_server | default({})) | combine((k3s_agent | default({}))) }}"
|
|
|
|
when: (k3s_server is defined or k3s_agent is defined)
|
2020-10-23 17:31:21 +02:00
|
|
|
and (k3s_control_node is defined and k3s_control_node)
|
2020-10-22 20:26:15 +02:00
|
|
|
|
|
|
|
- name: Ensure k3s_runtime_config is set for agents
|
|
|
|
set_fact:
|
2020-11-29 21:10:42 +01:00
|
|
|
k3s_runtime_config: "{{ (k3s_agent | default({})) }}"
|
2020-10-23 17:31:21 +02:00
|
|
|
when: k3s_agent is defined
|
|
|
|
and (k3s_control_node is not defined or not k3s_control_node)
|