kubeasz/playbooks/31.deletcd.yml

80 lines
3.0 KiB
YAML
Raw Normal View History

# WARNNING: this playbook will clean the etcd {{ ETCD_TO_DEL }}
2019-06-03 11:25:05 +08:00
- hosts: localhost
2019-02-17 22:07:27 +08:00
vars_prompt:
- name: "ETCD_TO_DEL"
prompt: "which etcd node is about to be deleted?(e.g 192.168.1.1)"
private: no
confirm: yes
tasks:
# step0: run prechecks
- fail: msg="{{ ETCD_TO_DEL }} is NOT a member of etcd cluster!"
when: "ETCD_TO_DEL not in groups['etcd']"
2019-02-17 22:07:27 +08:00
- fail: msg="you CAN NOT delete the last member of etcd cluster!"
when: "groups['etcd']|length < 2"
2019-02-17 22:07:27 +08:00
- block:
# step1: find a healthy member in the etcd cluster
- name: set NODE_IPS of the etcd cluster
set_fact: NODE_IPS="{% for host in groups['etcd'] %}{{ host }} {% endfor %}"
- name: get etcd cluster status
shell: 'for ip in {{ NODE_IPS }};do \
ETCDCTL_API=3 {{ base_dir }}/bin/etcdctl \
--endpoints=https://"$ip":2379 \
2020-12-25 11:53:00 +08:00
--cacert={{ cluster_dir }}/ssl/ca.pem \
2020-12-30 11:25:54 +08:00
--cert={{ cluster_dir }}/ssl/etcd.pem \
--key={{ cluster_dir }}/ssl/etcd-key.pem \
endpoint health; \
done'
register: ETCD_CLUSTER_STATUS
ignore_errors: true
2020-11-19 20:19:27 +08:00
- debug: var="ETCD_CLUSTER_STATUS"
- name: get a running ectd node
shell: 'echo -e "{{ ETCD_CLUSTER_STATUS.stdout }}" \
"{{ ETCD_CLUSTER_STATUS.stderr }}" \
|grep "is healthy"|sed -n "1p"|cut -d: -f2|cut -d/ -f3'
register: RUNNING_NODE
- debug: var="RUNNING_NODE.stdout"
2021-01-07 09:30:50 +08:00
# step2: remove jobs run on the healthy member
2019-02-17 22:07:27 +08:00
- name: get ID of etcd node to delete
shell: "ETCDCTL_API=3 {{ bin_dir }}/etcdctl member list \
|grep {{ ETCD_TO_DEL }}:2380|cut -d',' -f1"
2019-02-17 22:07:27 +08:00
register: ETCD_ID
delegate_to: "{{ RUNNING_NODE.stdout }}"
2019-02-17 22:07:27 +08:00
- name: get NAME of etcd node to delete
shell: "ETCDCTL_API=3 {{ bin_dir }}/etcdctl member list \
|grep {{ ETCD_TO_DEL }}:2380|cut -d' ' -f3|cut -d',' -f1"
2019-02-17 22:07:27 +08:00
register: ETCD_NAME
delegate_to: "{{ RUNNING_NODE.stdout }}"
2019-02-17 22:07:27 +08:00
2021-01-07 09:30:50 +08:00
- debug: var="ETCD_NAME.stdout"
2019-02-17 22:07:27 +08:00
- name: delete a etcd member
shell: "ETCDCTL_API=3 {{ bin_dir }}/etcdctl member remove {{ ETCD_ID.stdout }}"
delegate_to: "{{ RUNNING_NODE.stdout }}"
2019-02-17 22:07:27 +08:00
when: "ETCD_ID.stdout != ''"
2021-01-07 09:30:50 +08:00
- name: clean etcd {{ ETCD_TO_DEL }}
shell: "ansible-playbook -i {{ base_dir }}/clusters/{{ CLUSTER }}/hosts \
{{ base_dir }}/roles/clean/clean_node.yml \
-e NODE_TO_CLEAN={{ ETCD_TO_DEL }} \
-e DEL_ETCD=yes >> /tmp/ansible-`date +'%Y%m%d%H%M%S'`.log 2>&1 \
|| echo 'data not cleaned on {{ ETCD_TO_DEL }}'"
register: CLEAN_STATUS
2019-07-23 22:22:52 +08:00
2021-01-07 09:30:50 +08:00
- debug: var="CLEAN_STATUS"
2019-07-23 22:22:52 +08:00
# lineinfile is inadequate to delete lines between some specific line range
- name: remove the etcd's node entry in hosts
2021-01-07 09:30:50 +08:00
shell: 'sed -i "/^\[etcd/,/^\[kube-master/ {/^{{ ETCD_TO_DEL }}[^0-9]/d}" {{ base_dir }}/clusters/{{ CLUSTER }}/hosts'
2019-07-23 22:22:52 +08:00
args:
warn: false
2019-02-17 22:07:27 +08:00
when: "groups['etcd']|length > 1 and ETCD_TO_DEL in groups['etcd']"