ansible-role-k3s/tasks/teardown/drain-and-remove-nodes.yml
2021-10-10 14:07:04 +01:00

56 lines
2 KiB
YAML

---
- name: Check if kubectl exists
ansible.builtin.stat:
path: "{{ k3s_install_dir }}/kubectl"
register: k3s_check_kubectl
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
- name: Clean up nodes that are in an uninstalled state
block:
- name: Gather a list of nodes
ansible.builtin.command:
cmd: "{{ 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
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure uninstalled nodes are drained
ansible.builtin.command:
cmd: >-
{{ k3s_install_dir }}/kubectl drain {{ item }}
--ignore-daemonsets
--{{ k3s_drain_command[ansible_version.string is version_compare('1.22', '>=')] }}
--force
delegate_to: "{{ k3s_control_delegate }}"
run_once: true
when:
- kubectl_get_nodes_result.stdout is defined
- item in kubectl_get_nodes_result.stdout
- hostvars[item].k3s_state is defined
- hostvars[item].k3s_state == 'uninstalled'
loop: "{{ ansible_play_hosts }}"
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
- name: Ensure uninstalled nodes are removed
ansible.builtin.command:
cmd: "{{ k3s_install_dir }}/kubectl delete node {{ item }}"
delegate_to: "{{ k3s_control_delegate }}"
run_once: true
when:
- kubectl_get_nodes_result.stdout is defined
- item in kubectl_get_nodes_result.stdout
- hostvars[item].k3s_state is defined
- hostvars[item].k3s_state == 'uninstalled'
loop: "{{ ansible_play_hosts }}"
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
when:
- k3s_check_kubectl.stat.exists is defined
- k3s_check_kubectl.stat.exists
- k3s_control_delegate is defined
- not ansible_check_mode