2020-05-18 20:53:03 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
- name: Check if kubectl exists
|
2020-12-12 15:27:59 +01:00
|
|
|
ansible.builtin.stat:
|
2020-05-18 20:53:03 +02:00
|
|
|
path: "{{ k3s_install_dir }}/kubectl"
|
|
|
|
register: k3s_check_kubectl
|
2020-05-20 19:50:58 +02:00
|
|
|
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
|
2020-05-18 20:53:03 +02:00
|
|
|
|
|
|
|
- name: Clean up nodes that are in an uninstalled state
|
|
|
|
block:
|
|
|
|
|
|
|
|
- name: Gather a list of nodes
|
2021-04-10 13:15:51 +02:00
|
|
|
ansible.builtin.command:
|
|
|
|
cmd: "{{ k3s_install_dir }}/kubectl get nodes"
|
2020-05-18 20:53:03 +02:00
|
|
|
changed_when: false
|
|
|
|
failed_when: false
|
|
|
|
delegate_to: "{{ k3s_control_delegate }}"
|
|
|
|
run_once: true
|
|
|
|
register: kubectl_get_nodes_result
|
2020-05-20 19:50:58 +02:00
|
|
|
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
|
2020-05-18 20:53:03 +02:00
|
|
|
|
|
|
|
- name: Ensure uninstalled nodes are drained
|
2021-04-10 13:15:51 +02:00
|
|
|
ansible.builtin.command:
|
|
|
|
cmd: >-
|
|
|
|
{{ k3s_install_dir }}/kubectl drain {{ item }}
|
|
|
|
--ignore-daemonsets
|
2021-10-10 15:07:04 +02:00
|
|
|
--{{ k3s_drain_command[ansible_version.string is version_compare('1.22', '>=')] }}
|
2021-04-10 13:15:51 +02:00
|
|
|
--force
|
2020-05-18 20:53:03 +02:00
|
|
|
delegate_to: "{{ k3s_control_delegate }}"
|
|
|
|
run_once: true
|
2021-02-16 16:46:01 +01:00
|
|
|
when:
|
2021-04-30 08:31:33 +02:00
|
|
|
- kubectl_get_nodes_result.stdout is defined
|
2021-02-16 16:46:01 +01:00
|
|
|
- item in kubectl_get_nodes_result.stdout
|
|
|
|
- hostvars[item].k3s_state is defined
|
|
|
|
- hostvars[item].k3s_state == 'uninstalled'
|
2021-07-24 18:21:31 +02:00
|
|
|
loop: "{{ ansible_play_hosts }}"
|
2020-05-20 19:50:58 +02:00
|
|
|
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
|
2020-05-18 20:53:03 +02:00
|
|
|
|
|
|
|
- name: Ensure uninstalled nodes are removed
|
2021-04-10 13:15:51 +02:00
|
|
|
ansible.builtin.command:
|
|
|
|
cmd: "{{ k3s_install_dir }}/kubectl delete node {{ item }}"
|
2020-05-18 20:53:03 +02:00
|
|
|
delegate_to: "{{ k3s_control_delegate }}"
|
|
|
|
run_once: true
|
2021-02-16 16:46:01 +01:00
|
|
|
when:
|
2021-04-30 08:31:33 +02:00
|
|
|
- kubectl_get_nodes_result.stdout is defined
|
2021-02-16 16:46:01 +01:00
|
|
|
- item in kubectl_get_nodes_result.stdout
|
|
|
|
- hostvars[item].k3s_state is defined
|
|
|
|
- hostvars[item].k3s_state == 'uninstalled'
|
2021-07-24 18:21:31 +02:00
|
|
|
loop: "{{ ansible_play_hosts }}"
|
2020-05-20 19:50:58 +02:00
|
|
|
become: "{{ k3s_become_for_kubectl | ternary(true, false, k3s_become_for_all) }}"
|
2020-05-18 20:53:03 +02:00
|
|
|
|
2021-02-16 16:46:01 +01:00
|
|
|
when:
|
|
|
|
- k3s_check_kubectl.stat.exists is defined
|
|
|
|
- k3s_check_kubectl.stat.exists
|
|
|
|
- k3s_control_delegate is defined
|
|
|
|
- not ansible_check_mode
|