mirror of
https://github.com/PyratLabs/ansible-role-k3s.git
synced 2024-11-22 11:48:22 +01:00
Updated state tasks to dynamic include rather thn static import
This is an initial attempt to address issue #22, I have also included a task to drain the node before deleting it.
This commit is contained in:
parent
02e12e61a8
commit
e3301a59e4
@ -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"
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
38
tasks/teardown/drain-and-remove-nodes.yml
Normal file
38
tasks/teardown/drain-and-remove-nodes.yml
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user