2019-06-03 11:25:05 +08:00
|
|
|
# 警告:此脚本将清理指定 etcd 节点
|
|
|
|
# 使用:`easzctl del-etcd 1.1.1.1`
|
|
|
|
|
2019-06-01 22:16:14 +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:
|
2019-02-25 22:16:03 +08:00
|
|
|
- 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
|
|
|
|
2019-02-25 22:16:03 +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:
|
|
|
|
- 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: "{{ groups.etcd[0] }}"
|
|
|
|
|
|
|
|
- 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: "{{ groups.etcd[0] }}"
|
|
|
|
|
|
|
|
- name: rm etcd's node in hosts
|
|
|
|
lineinfile:
|
|
|
|
dest: "{{ base_dir }}/hosts"
|
|
|
|
state: absent
|
|
|
|
regexp: '{{ ETCD_NAME.stdout }}'
|
|
|
|
when: "ETCD_NAME.stdout != ''"
|
|
|
|
|
|
|
|
- name: delete a etcd member
|
|
|
|
shell: "ETCDCTL_API=3 {{ bin_dir }}/etcdctl member remove {{ ETCD_ID.stdout }}"
|
|
|
|
delegate_to: "{{ groups.etcd[0] }}"
|
|
|
|
when: "ETCD_ID.stdout != ''"
|
|
|
|
|
|
|
|
- name: rm data of the deleted etcd node
|
|
|
|
file: name=/var/lib/etcd state=absent
|
|
|
|
delegate_to: "{{ ETCD_TO_DEL }}"
|
|
|
|
when: "ETCD_ID.stdout != ''"
|
|
|
|
|
|
|
|
- name: reconfig and restart the etcd cluster
|
2019-06-01 22:16:14 +08:00
|
|
|
shell: "ansible-playbook {{ base_dir }}/02.etcd.yml > /tmp/ansible-playbook.log 2>&1"
|
2019-02-17 22:07:27 +08:00
|
|
|
when: "ETCD_ID.stdout != ''"
|
|
|
|
run_once: true
|
|
|
|
# 满足条件才进行删除
|
|
|
|
when: "groups['etcd']|length > 1 and ETCD_TO_DEL in groups['etcd']"
|