# ------------------------------------------------------------------------------ # FILE: roles/control-plane/tasks/main.yml # ------------------------------------------------------------------------------ - name: Check if cluster is already initialized stat: path: /etc/kubernetes/admin.conf register: kube_init_stat - name: Initialize the Kubernetes cluster when: not kube_init_stat.stat.exists command: > kubeadm init --pod-network-cidr=10.244.0.0/16 --control-plane-endpoint={{ inventory_hostname }} register: kubeadm_init - name: Create .kube directory for the user become: no file: path: "{{ ansible_env.HOME }}/.kube" state: directory mode: '0755' delegate_to: localhost run_once: true - name: Copy admin.conf to user's .kube directory on controller when: not kube_init_stat.stat.exists fetch: src: /etc/kubernetes/admin.conf dest: "{{ ansible_env.HOME }}/.kube/config" flat: yes - name: Install Calico CNI become: no command: kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.28.0/manifests/calico.yaml delegate_to: localhost run_once: true when: not kube_init_stat.stat.exists - name: Generate join command for control plane nodes when: not kube_init_stat.stat.exists command: kubeadm token create --print-join-command register: control_plane_join_command_raw - name: Generate certificate key for control plane join when: not kube_init_stat.stat.exists command: kubeadm init phase upload-certs --upload-certs register: control_plane_cert_key - name: Store control plane join command set_fact: control_plane_join_command: "{{ control_plane_join_command_raw.stdout }} --control-plane --certificate-key {{ control_plane_cert_key.stdout_lines[-1] }}" when: not kube_init_stat.stat.exists - name: Join other control plane nodes to the cluster when: - inventory_hostname != groups['control_plane'][0] - not kube_init_stat.stat.exists command: "{{ hostvars[groups['control_plane'][0]].control_plane_join_command }}"