# 警告:此脚本将清理指定 etcd 节点 # 使用:`easzctl del-etcd 1.1.1.1` - hosts: localhost 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: # 以下几个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" # 在etcd健康节点上才能进行操作 - 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 }}" - 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 }}" - name: delete a etcd member shell: "ETCDCTL_API=3 {{ bin_dir }}/etcdctl member remove {{ ETCD_ID.stdout }}" delegate_to: "{{ RUNNING_NODE.stdout }}" when: "ETCD_ID.stdout != ''" #- 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 shell: "ssh {{ ETCD_TO_DEL }} rm -rf /var/lib/etcd" ignore_errors: true # 在[etcd]组内删除node节点,lineinfile模块不好用,只能用sed - name: remove the etcd's node entry in hosts shell: 'sed -i "/^\[etcd/,/^\[kube-master/ {/^{{ ETCD_TO_DEL }}[^0-9]/d}" {{ base_dir }}/hosts' args: warn: false - name: reconfig and restart the etcd cluster shell: "ansible-playbook {{ base_dir }}/02.etcd.yml > /tmp/ansible-playbook.log 2>&1" # 满足条件才进行删除 when: "groups['etcd']|length > 1 and ETCD_TO_DEL in groups['etcd']"