kubeasz/tools/remove_etcd_node.yml

53 lines
1.8 KiB
YAML

# remove a etcd member
- hosts: deploy
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']"
- name: fail info2
fail: msg="you CAN NOT delete the last member of etcd cluster!"
when: "groups['etcd']|length < 2"
- 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 }}'
connection: local
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
shell: "ansible-playbook /etc/ansible/02.etcd.yml > /tmp/ansible-playbook.log 2>&1"
connection: local
when: "ETCD_ID.stdout != ''"
run_once: true
# 满足条件才进行删除
when: "groups['etcd']|length > 1 and ETCD_TO_DEL in groups['etcd']"