From f7f581be40be0b4ca704ad32f629629dd34266c1 Mon Sep 17 00:00:00 2001 From: gjmzj Date: Fri, 23 Jun 2023 00:09:20 +0800 Subject: [PATCH] fix: set /etc/hosts entries for k8s_nodename --- playbooks/22.addnode.yml | 11 +++++++++++ playbooks/23.addmaster.yml | 11 +++++++++++ roles/prepare/tasks/main.yml | 30 ++++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/playbooks/22.addnode.yml b/playbooks/22.addnode.yml index 8a3eba8..abdfcb2 100644 --- a/playbooks/22.addnode.yml +++ b/playbooks/22.addnode.yml @@ -1,4 +1,15 @@ # 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 4b89ee2..a8c8ac4 100644 --- a/playbooks/23.addmaster.yml +++ b/playbooks/23.addmaster.yml @@ -1,4 +1,15 @@ # 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/prepare/tasks/main.yml b/roles/prepare/tasks/main.yml index 39d5497..2a23b89 100644 --- a/roles/prepare/tasks/main.yml +++ b/roles/prepare/tasks/main.yml @@ -42,12 +42,30 @@ regexp: 'easzlab.io.local' line: "{{ ansible_env.SSH_CLIENT.split(' ')[0] }} easzlab.io.local" -- name: 设置 k8s_nodename 在 master、node 节点 /etc/hosts 地址解析 +# 设置节点名称{{ K8S_NODENAME }} /etc/hosts 解析 +# 1.先确保第一个主节点做好解析 +# 2.然后复制给集群所有节点 +- name: 设置 k8s_nodename 在 master[0] 节点 /etc/hosts 地址解析 lineinfile: dest: /etc/hosts state: present - regexp: "{{ K8S_NODENAME }}" - line: "{{ inventory_hostname }} {{ K8S_NODENAME }}" - delegate_to: "{{ item }}" - with_items: ["{{ groups.kube_master }}", "{{ groups.kube_node }}"] - when: "inventory_hostname != K8S_NODENAME" \ No newline at end of file + regexp: "{{ K8S_NODENAME }} .* generated by kubeasz" + line: "{{ inventory_hostname }} {{ K8S_NODENAME }} # generated by kubeasz" + 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] }}" + 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]"