2019-03-09 21:54:44 +01:00
|
|
|
---
|
|
|
|
|
2020-12-05 22:56:28 +01:00
|
|
|
- name: Ensure k3s_build_cluster is false if running against a single node.
|
2020-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
2020-12-05 22:56:28 +01:00
|
|
|
k3s_build_cluster: false
|
2021-02-16 16:46:01 +01:00
|
|
|
when:
|
2021-07-24 18:21:31 +02:00
|
|
|
- ansible_play_hosts | length < 2
|
2021-03-14 17:29:14 +01:00
|
|
|
- k3s_registration_address is not defined
|
2020-12-05 22:56:28 +01:00
|
|
|
|
2019-03-09 21:54:44 +01:00
|
|
|
- name: Ensure k3s control node fact is set
|
2020-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
2021-07-24 18:38:45 +02:00
|
|
|
k3s_control_node: "{{ not k3s_build_cluster }}"
|
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-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
2021-07-24 18:38:45 +02:00
|
|
|
k3s_primary_control_node: "{{ not k3s_build_cluster }}"
|
2020-01-11 23:42:29 +01:00
|
|
|
when: k3s_primary_control_node is not defined
|
|
|
|
|
2021-02-27 20:02:49 +01:00
|
|
|
- name: Ensure k3s control plane port is captured
|
2020-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
|
|
|
k3s_control_plane_port: "{{ k3s_runtime_config['https-listen-port'] | default(6443) }}"
|
|
|
|
delegate_to: k3s_primary_control_node
|
|
|
|
|
2021-08-18 21:44:06 +02:00
|
|
|
- name: Ensure k3s node IP is configured when node-ip is defined
|
|
|
|
ansible.builtin.set_fact:
|
|
|
|
k3s_node_ip: "{{ k3s_runtime_config['node-ip'] }}"
|
|
|
|
when:
|
|
|
|
- k3s_runtime_config['node-ip'] is defined
|
|
|
|
|
2021-07-24 18:21:31 +02:00
|
|
|
- name: Ensure a count of control nodes is generated from ansible_play_hosts
|
2020-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
|
|
|
k3s_controller_list: "{{ k3s_controller_list + [ item ] }}"
|
2021-02-16 16:46:01 +01:00
|
|
|
when:
|
|
|
|
- hostvars[item].k3s_control_node is defined
|
|
|
|
- hostvars[item].k3s_control_node
|
2021-07-24 18:21:31 +02:00
|
|
|
loop: "{{ ansible_play_hosts }}"
|
2020-12-21 20:14:52 +01:00
|
|
|
|
2021-07-24 18:21:31 +02:00
|
|
|
- name: Ensure a k3s control node is defined if none are found in ansible_play_hosts
|
2019-03-09 21:54:44 +01:00
|
|
|
block:
|
2020-12-21 20:14:52 +01:00
|
|
|
|
2020-05-16 21:18:10 +02:00
|
|
|
- name: Set the control host
|
2020-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
2019-03-09 21:54:44 +01:00
|
|
|
k3s_control_node: true
|
2021-07-24 18:21:31 +02:00
|
|
|
when: inventory_hostname == ansible_play_hosts[0]
|
2019-03-09 21:54:44 +01:00
|
|
|
|
2020-12-21 20:14:52 +01:00
|
|
|
- name: Ensure a count of control nodes is generated
|
|
|
|
ansible.builtin.set_fact:
|
|
|
|
k3s_controller_list: "{{ k3s_controller_list + [ item ] }}"
|
2021-02-16 16:46:01 +01:00
|
|
|
when:
|
|
|
|
- hostvars[item].k3s_control_node is defined
|
|
|
|
- hostvars[item].k3s_control_node
|
2021-07-24 18:21:31 +02:00
|
|
|
loop: "{{ ansible_play_hosts }}"
|
2020-12-21 20:14:52 +01:00
|
|
|
|
2021-02-16 16:46:01 +01:00
|
|
|
when:
|
|
|
|
- k3s_controller_list | length < 1
|
|
|
|
- k3s_build_cluster is defined
|
|
|
|
- k3s_build_cluster
|
2020-01-11 23:42:29 +01:00
|
|
|
|
2021-07-24 18:21:31 +02:00
|
|
|
- name: Ensure a primary k3s control node is defined if multiple are found in ansible_play_hosts
|
2020-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
2020-01-11 23:42:29 +01:00
|
|
|
k3s_primary_control_node: true
|
2021-02-16 16:46:01 +01:00
|
|
|
when:
|
|
|
|
- k3s_controller_list is defined
|
|
|
|
- inventory_hostname == k3s_controller_list[0]
|
|
|
|
- k3s_build_cluster is defined
|
|
|
|
- k3s_build_cluster
|
2020-05-18 20:53:03 +02:00
|
|
|
|
|
|
|
- name: Ensure ansible_host is mapped to inventory_hostname
|
2021-04-18 21:39:06 +02:00
|
|
|
ansible.builtin.blockinfile:
|
2020-05-18 20:53:03 +02:00
|
|
|
path: /tmp/inventory.txt
|
2021-04-18 21:39:06 +02:00
|
|
|
block: |
|
2021-07-24 18:21:31 +02:00
|
|
|
{% for host in ansible_play_hosts %}
|
2021-04-18 21:39:06 +02:00
|
|
|
{% filter replace('\n', ' ') %}
|
|
|
|
{{ host }}
|
2020-05-18 20:53:03 +02:00
|
|
|
@@@
|
2021-07-19 11:26:57 +02:00
|
|
|
{{ hostvars[host].ansible_host | default(hostvars[host].ansible_fqdn) | string }}
|
2020-05-18 20:53:03 +02:00
|
|
|
@@@
|
2021-07-19 11:26:57 +02:00
|
|
|
C_{{ hostvars[host].k3s_control_node | string }}
|
2020-05-18 20:53:03 +02:00
|
|
|
@@@
|
2021-07-19 11:26:57 +02:00
|
|
|
P_{{ hostvars[host].k3s_primary_control_node | default(False) | string }}
|
2021-04-18 21:39:06 +02:00
|
|
|
{% endfilter %}
|
|
|
|
@@@ END:{{ host }}
|
|
|
|
{% endfor %}
|
2020-05-18 20:53:03 +02:00
|
|
|
create: true
|
2020-09-15 19:10:25 +02:00
|
|
|
mode: 0600
|
2020-07-25 18:39:01 +02:00
|
|
|
check_mode: false
|
2021-04-18 21:39:06 +02:00
|
|
|
when: k3s_control_node is defined
|
2020-05-18 20:53:03 +02:00
|
|
|
|
2021-02-28 17:45:38 +01:00
|
|
|
- name: Delegate an initializing control plane node
|
2020-05-25 17:25:09 +02:00
|
|
|
block:
|
|
|
|
- name: Lookup control node from file
|
2021-04-10 13:15:51 +02:00
|
|
|
ansible.builtin.command:
|
2021-05-29 22:26:50 +02:00
|
|
|
cmd: "grep -i '{{ 'P_True' if (k3s_controller_list | length > 1) else 'C_True' }}' /tmp/inventory.txt"
|
2020-05-25 17:25:09 +02:00
|
|
|
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
|
|
|
|
2021-02-28 17:45:38 +01:00
|
|
|
- name: Ensure control node is delegated for obtaining a cluster token
|
2020-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
2020-05-25 17:25:09 +02:00
|
|
|
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
|
|
|
|
2021-03-14 17:29:14 +01:00
|
|
|
- name: Ensure the node registration address is defined from k3s_control_node_address
|
2020-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
2021-03-14 17:29:14 +01:00
|
|
|
k3s_registration_address: "{{ k3s_control_node_address }}"
|
2020-07-25 18:39:01 +02:00
|
|
|
check_mode: false
|
2021-03-14 17:29:14 +01:00
|
|
|
when: k3s_control_node_address is defined
|
2020-05-25 18:57:43 +02:00
|
|
|
|
2021-08-14 15:04:56 +02:00
|
|
|
- name: Ensure the node registration address is defined from node-ip
|
2021-08-09 00:14:15 +02:00
|
|
|
ansible.builtin.set_fact:
|
2021-08-18 21:44:06 +02:00
|
|
|
k3s_registration_address: "{{ hostvars[k3s_control_delegate].k3s_node_ip }}"
|
2021-08-09 00:14:15 +02:00
|
|
|
check_mode: false
|
|
|
|
when:
|
|
|
|
- k3s_registration_address is not defined
|
|
|
|
- k3s_control_node_address is not defined
|
2021-08-18 21:44:06 +02:00
|
|
|
- hostvars[k3s_control_delegate].k3s_node_ip is defined
|
2021-08-09 00:14:15 +02:00
|
|
|
|
2021-03-14 17:29:14 +01:00
|
|
|
- name: Ensure the node registration address is defined
|
|
|
|
ansible.builtin.set_fact:
|
|
|
|
k3s_registration_address: "{{ hostvars[k3s_control_delegate].ansible_host | default(hostvars[k3s_control_delegate].ansible_fqdn) }}"
|
|
|
|
check_mode: false
|
|
|
|
when:
|
|
|
|
- k3s_registration_address is not defined
|
|
|
|
- k3s_control_node_address is not defined
|
|
|
|
|
|
|
|
when: k3s_registration_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 agents
|
2020-12-21 20:14:52 +01:00
|
|
|
ansible.builtin.set_fact:
|
2020-11-29 21:10:42 +01:00
|
|
|
k3s_runtime_config: "{{ (k3s_agent | default({})) }}"
|
2021-02-16 16:46:01 +01:00
|
|
|
when:
|
|
|
|
- k3s_agent is defined
|
|
|
|
- (k3s_control_node is not defined or not k3s_control_node)
|