diff --git a/molecule/default/playbook-ha-postgres.yml b/molecule/default/playbook-ha-postgres.yml index ec17daa..c1a613b 100644 --- a/molecule/default/playbook-ha-postgres.yml +++ b/molecule/default/playbook-ha-postgres.yml @@ -9,5 +9,6 @@ - name: Set each node to be a control node set_fact: k3s_control_node: true + when: inventory_hostname in ['node2', 'node3'] roles: - role: xanmanning.k3s diff --git a/tasks/build/install-k3s.yml b/tasks/build/install-k3s.yml index 476845a..8de5737 100644 --- a/tasks/build/install-k3s.yml +++ b/tasks/build/install-k3s.yml @@ -5,13 +5,15 @@ path: "{{ k3s_systemd_unit_directory }}" state: directory recurse: true - when: k3s_control_node + when: (k3s_control_node and k3s_controller_count | length == 1) + or (k3s_primary_control_node and k3s_controller_count | length > 1) - name: Ensure k3s service unit file is present on control plane template: src: k3s.service.j2 dest: "{{ k3s_systemd_unit_directory }}/k3s.service" - when: k3s_control_node + when: (k3s_control_node and k3s_controller_count | length == 1) + or (k3s_primary_control_node and k3s_controller_count | length > 1) notify: - reload systemd @@ -36,4 +38,5 @@ state: started enabled: true scope: "{{ k3s_systemd_context }}" - when: k3s_control_node + when: (k3s_control_node and k3s_controller_count | length == 1) + or (k3s_primary_control_node and k3s_controller_count | length > 1) diff --git a/tasks/build/preconfigure-k3s.yml b/tasks/build/preconfigure-k3s.yml index 50cf661..b83b8cc 100644 --- a/tasks/build/preconfigure-k3s.yml +++ b/tasks/build/preconfigure-k3s.yml @@ -5,6 +5,11 @@ k3s_control_node: false when: k3s_control_node is not defined +- name: Ensure k3s master control node fact is set + set_fact: + k3s_primary_control_node: false + when: k3s_primary_control_node is not defined + - name: Ensure a k3s control node is defined if none are found in play_hosts block: - name: Set control host @@ -16,5 +21,11 @@ - name: Ensure a count of control masters is generated set_fact: k3s_controller_count: "{{ k3s_controller_count + [ item ] }}" - when: item - loop: "{{ hostvars | json_query('*.k3s_control_node') }}" + when: hostvars[item].k3s_control_node + 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 + when: k3s_controller_count | length > 1 + and inventory_hostname == k3s_controller_count[0]