diff --git a/playbooks/22.addnode.yml b/playbooks/22.addnode.yml index abdfcb2..8a3eba8 100644 --- a/playbooks/22.addnode.yml +++ b/playbooks/22.addnode.yml @@ -1,15 +1,4 @@ # Note: this playbook can not run independently -- hosts: "{{ NODE_TO_ADD }}" - tasks: - - name: 设置 k8s_nodename 在 master、node 节点 /etc/hosts 地址解析 - lineinfile: - dest: /etc/hosts - state: present - regexp: "{{ K8S_NODENAME }} .* generated by kubeasz" - line: "{{ inventory_hostname }} {{ K8S_NODENAME }} # generated by kubeasz" - delegate_to: "{{ item }}" - with_items: ["{{ groups.kube_master }}", "{{ groups.kube_node }}"] - when: "inventory_hostname != K8S_NODENAME" - hosts: "{{ NODE_TO_ADD }}" roles: diff --git a/playbooks/23.addmaster.yml b/playbooks/23.addmaster.yml index a8c8ac4..4b89ee2 100644 --- a/playbooks/23.addmaster.yml +++ b/playbooks/23.addmaster.yml @@ -1,15 +1,4 @@ # Note: this playbook cann't run independently -- hosts: "{{ NODE_TO_ADD }}" - tasks: - - name: 设置 k8s_nodename 在 master、node 节点 /etc/hosts 地址解析 - lineinfile: - dest: /etc/hosts - state: present - regexp: "{{ K8S_NODENAME }} .* generated by kubeasz" - line: "{{ inventory_hostname }} {{ K8S_NODENAME }} # generated by kubeasz" - delegate_to: "{{ item }}" - with_items: ["{{ groups.kube_master }}", "{{ groups.kube_node }}"] - when: "inventory_hostname != K8S_NODENAME" - hosts: "{{ NODE_TO_ADD }}" roles: diff --git a/roles/clean/tasks/main.yml b/roles/clean/tasks/main.yml index 82b9789..af6879d 100644 --- a/roles/clean/tasks/main.yml +++ b/roles/clean/tasks/main.yml @@ -27,6 +27,12 @@ - 'HELM_TLS_ENABLE' when: 'DEL_ENV == "yes"' +- name: 删除 k8s_nodename 在节点的 /etc/hosts 地址解析 + blockinfile: + path: /etc/hosts + state: absent + marker: "### {mark} KUBEASZ MANAGED BLOCK" + #- name: remove binaries # file: name={{ item }} state=absent # with_items: diff --git a/roles/prepare/tasks/main.yml b/roles/prepare/tasks/main.yml index 2a23b89..c6cd7e0 100644 --- a/roles/prepare/tasks/main.yml +++ b/roles/prepare/tasks/main.yml @@ -44,7 +44,6 @@ # 设置节点名称{{ K8S_NODENAME }} /etc/hosts 解析 # 1.先确保第一个主节点做好解析 -# 2.然后复制给集群所有节点 - name: 设置 k8s_nodename 在 master[0] 节点 /etc/hosts 地址解析 lineinfile: dest: /etc/hosts @@ -54,18 +53,26 @@ delegate_to: "{{ groups.kube_master[0] }}" when: "inventory_hostname != K8S_NODENAME" -- name: 获取 master[0] 节点由kubeasz 创建的 /etc/hosts 地址解析 - shell: 'grep "generated by kubeasz" /etc/hosts' - register: RESOLVE_K8S_NODENAME - delegate_to: "{{ groups.kube_master[0] }}" +# 2.然后复制给集群所有节点 +- block: + - name: 获取 master[0] 节点由kubeasz 创建的 /etc/hosts 地址解析 + shell: 'grep "generated by kubeasz" /etc/hosts|sort|uniq' + register: RESOLVE_K8S_NODENAME + delegate_to: "{{ groups.kube_master[0] }}" + + - name: 删除 master[0] 节点由kubeasz 创建的 /etc/hosts 地址解析 + lineinfile: + dest: /etc/hosts + state: absent + regexp: "generated by kubeasz" + delegate_to: "{{ groups.kube_master[0] }}" + + - name: 设置 k8s_nodename 在所有节点的 /etc/hosts 地址解析 + blockinfile: + path: /etc/hosts + block: | + {{ RESOLVE_K8S_NODENAME.stdout }} + marker: "### {mark} KUBEASZ MANAGED BLOCK" + delegate_to: "{{ item }}" + with_items: ["{{ groups.kube_master }}", "{{ groups.kube_node }}"] run_once: true - -#- debug: var="RESOLVE_K8S_NODENAME.stdout" -# run_once: true - -- name: 设置 /etc/hosts 地址解析 - blockinfile: - path: /etc/hosts - block: | - {{ RESOLVE_K8S_NODENAME.stdout }} - when: "inventory_hostname != groups.kube_master[0]"