diff --git a/tasks/build/configure-k3s-cluster.yml b/tasks/build/configure-k3s-cluster.yml index 309d457..a60d346 100644 --- a/tasks/build/configure-k3s-cluster.yml +++ b/tasks/build/configure-k3s-cluster.yml @@ -1,33 +1,5 @@ --- -- 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 - loop: "{{ play_hosts }}" - -- 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 - register: k3s_control_delegate_raw - -- name: Ensure control node is delegated to for obtaining a token - set_fact: - k3s_control_delegate: "{{ k3s_control_delegate_raw.stdout.split(' @@@ ')[0] }}" - -- 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) }}" - when: k3s_control_node_address is not defined - - name: Ensure NODE_TOKEN is captured from control node slurp: path: "/var/lib/rancher/k3s/server/node-token" diff --git a/tasks/build/preconfigure-k3s.yml b/tasks/build/preconfigure-k3s.yml index 9471b16..87f441f 100644 --- a/tasks/build/preconfigure-k3s.yml +++ b/tasks/build/preconfigure-k3s.yml @@ -22,12 +22,43 @@ - name: Ensure a count of control masters is generated set_fact: k3s_controller_count: "{{ k3s_controller_count + [ item ] }}" - when: hostvars[item].k3s_control_node + when: hostvars[item].k3s_control_node is defined + and 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 + when: k3s_controller_count is defined + and k3s_controller_count | length > 1 and inventory_hostname == k3s_controller_count[0] and k3s_build_cluster is defined and k3s_build_cluster + +- 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 + loop: "{{ play_hosts }}" + when: hostvars[item].k3s_control_node is defined + +- 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 + register: k3s_control_delegate_raw + +- name: Ensure control node is delegated to for obtaining a token + set_fact: + k3s_control_delegate: "{{ k3s_control_delegate_raw.stdout.split(' @@@ ')[0] }}" + +- 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) }}" + when: k3s_control_node_address is not defined diff --git a/tasks/state-installed.yml b/tasks/state-installed.yml index 109ae53..e79275e 100644 --- a/tasks/state-installed.yml +++ b/tasks/state-installed.yml @@ -4,6 +4,8 @@ - import_tasks: build/preconfigure-k3s.yml +- import_tasks: teardown/drain-and-remove-nodes.yml + - import_tasks: build/get-version.yml when: k3s_release_version is not defined or not k3s_release_version diff --git a/tasks/state-uninstalled.yml b/tasks/state-uninstalled.yml index a75fcfa..7324168 100644 --- a/tasks/state-uninstalled.yml +++ b/tasks/state-uninstalled.yml @@ -1,5 +1,9 @@ --- +- import_tasks: build/preconfigure-k3s.yml + +- import_tasks: teardown/drain-and-remove-nodes.yml + - import_tasks: teardown/uninstall-k3s.yml - import_tasks: teardown/uninstall-docker.yml diff --git a/tasks/teardown/drain-and-remove-nodes.yml b/tasks/teardown/drain-and-remove-nodes.yml new file mode 100644 index 0000000..175dae5 --- /dev/null +++ b/tasks/teardown/drain-and-remove-nodes.yml @@ -0,0 +1,38 @@ +--- + +- name: Check if kubectl exists + stat: + path: "{{ k3s_install_dir }}/kubectl" + register: k3s_check_kubectl + +- name: Clean up nodes that are in an uninstalled state + block: + + - name: Gather a list of nodes + command: "{{ k3s_install_dir }}/kubectl get nodes" + changed_when: false + failed_when: false + delegate_to: "{{ k3s_control_delegate }}" + run_once: true + register: kubectl_get_nodes_result + + - name: Ensure uninstalled nodes are drained + command: "{{ k3s_install_dir }}/kubectl drain {{ item }} --ignore-daemonsets" + delegate_to: "{{ k3s_control_delegate }}" + run_once: true + when: item in kubectl_get_nodes_result.stdout + and hostvars[item].k3s_cluster_state is defined + and hostvars[item].k3s_cluster_state == 'uninstalled' + loop: "{{ play_hosts }}" + + - name: Ensure uninstalled nodes are removed + command: "{{ k3s_install_dir }}/kubectl delete node {{ item }}" + delegate_to: "{{ k3s_control_delegate }}" + run_once: true + when: item in kubectl_get_nodes_result.stdout + and hostvars[item].k3s_cluster_state is defined + and hostvars[item].k3s_cluster_state == 'uninstalled' + loop: "{{ play_hosts }}" + + when: k3s_check_kubectl.stat.exists is defined + and k3s_check_kubectl.stat.exists