60 lines
2.2 KiB
YAML
60 lines
2.2 KiB
YAML
# ------------------------------------------------------------------------------
|
|
# FILE: roles/control-plane/tasks/main.yml
|
|
# ------------------------------------------------------------------------------
|
|
- name: Check if cluster is already initialized
|
|
ansible.builtin.stat:
|
|
path: /etc/kubernetes/admin.conf
|
|
register: kube_init_stat
|
|
|
|
- name: Initialize the Kubernetes cluster
|
|
when: not kube_init_stat.stat.exists
|
|
ansible.builtin.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: false
|
|
ansible.builtin.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
|
|
ansible.builtin.fetch:
|
|
src: /etc/kubernetes/admin.conf
|
|
dest: "{{ ansible_env.HOME }}/.kube/config"
|
|
flat: true
|
|
|
|
- name: Install Calico CNI
|
|
become: false
|
|
ansible.builtin.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
|
|
ansible.builtin.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
|
|
ansible.builtin.command: kubeadm init phase upload-certs --upload-certs
|
|
register: control_plane_cert_key
|
|
|
|
- name: Store control plane join command
|
|
ansible.builtin.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['fastpass_control_plane'][0]
|
|
- not kube_init_stat.stat.exists
|
|
ansible.builtin.command: "{{ hostvars[groups['fastpass_control_plane'][0]].control_plane_join_command }}"
|