kubeasz/tools/11.deletcd.yml

79 lines
2.9 KiB
YAML
Raw Normal View History

2019-06-03 11:25:05 +08:00
# 警告:此脚本将清理指定 etcd 节点
# 使用:`easzctl del-etcd 1.1.1.1`
- 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:
- name: fail info1
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
- name: fail info2
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:
2019-07-23 22:22:52 +08:00
# 以下几个tasks是为寻找etcd集群中第一个健康节点
- 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 \
--cacert={{ base_dir }}/.cluster/ssl/ca.pem \
--cert={{ base_dir }}/.cluster/ssl/admin.pem \
--key={{ base_dir }}/.cluster/ssl/admin-key.pem \
endpoint health; \
done'
register: ETCD_CLUSTER_STATUS
- debug: var="ETCD_CLUSTER_STATUS.stdout"
- name: get a running ectd node
shell: 'echo -e "{{ ETCD_CLUSTER_STATUS.stdout }}"|grep "is healthy"|sed -n "1p"|cut -d: -f2|cut -d/ -f3'
register: RUNNING_NODE
- debug: var="RUNNING_NODE.stdout"
2019-07-23 22:22:52 +08:00
# 在etcd健康节点上才能进行操作
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"
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"
register: ETCD_NAME
delegate_to: "{{ RUNNING_NODE.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 != ''"
2019-07-23 22:22:52 +08:00
#- name: remove data of the deleted etcd node if possible
# file: name=/var/lib/etcd state=absent
# delegate_to: "{{ ETCD_TO_DEL }}"
# ignore_errors: true
- name: remove data of the deleted etcd node if possible
2019-07-23 22:22:52 +08:00
shell: "ssh {{ ETCD_TO_DEL }} rm -rf /var/lib/etcd"
ignore_errors: true
2019-07-23 22:22:52 +08:00
# 在[etcd]组内删除node节点lineinfile模块不好用只能用sed
- name: remove the etcd's node entry in hosts
2019-07-23 22:22:52 +08:00
shell: 'sed -i "/^\[etcd/,/^\[kube-master/ {/^{{ ETCD_TO_DEL }}[^0-9]/d}" {{ base_dir }}/hosts'
args:
warn: false
2019-02-17 22:07:27 +08:00
- name: reconfig and restart the etcd cluster
shell: "ansible-playbook {{ base_dir }}/02.etcd.yml > /tmp/ansible-playbook.log 2>&1"
2019-07-23 22:22:52 +08:00
2019-02-17 22:07:27 +08:00
# 满足条件才进行删除
when: "groups['etcd']|length > 1 and ETCD_TO_DEL in groups['etcd']"