Files
cicd/ansible/roles/k8s-deploy-cluster/tasks/k8s-control-plane-setup.yml
2025-08-25 19:10:31 +03:00

86 lines
2.3 KiB
YAML

---
- name: Check if Kubernetes has already been initialized.
stat:
path: /etc/kubernetes/admin.conf
register: k8s_init_stat
- block:
- block:
- name: Create kubeadm-config.yaml
template:
src: kubeadm-config.yaml.j2
dest: "/etc/kubernetes/kubeadm-kubelet-config.yaml"
owner: root
group: root
mode: '0644'
- name: Initialize Kubernetes control plane with kubeadm init ( !!! WAITING !!! )
command: >
kubeadm init
--config /etc/kubernetes/kubeadm-kubelet-config.yaml
--upload-certs
register: k8s_init
- name: Ensure .kube directory exists.
file:
path: ~/.kube
state: directory
mode: 0755
- name: Symlink the kubectl admin.conf to ~/.kube/conf
file:
src: /etc/kubernetes/admin.conf
dest: ~/.kube/config
state: link
mode: 0644
- name: Configure Calico networking.
command: "kubectl apply -f {{ k8s_calico_manifest_file }}"
register: calico_result
- name: Initialize Kubernetes control plane
command: kubeadm init --upload-certs
register: k8s_init
args:
creates: /etc/kubernetes/manifests/kube-apiserver.yaml
- name: Upload certs to get certificate key
command: kubeadm init phase upload-certs --upload-certs
register: certs_out
- name: Create new join token (worker)
command: kubeadm token create --print-join-command
register: join_cmd
- name: Extract join command base (without --control-plane)
set_fact:
join_command_base: "{{ join_cmd.stdout.split('--control-plane')[0] | default('') | trim }}"
- name: Extract certificate key
set_fact:
certificate_key: "{{ (certs_out.stdout_lines | last) | default('') | trim }}"
- name: Full control-plane join command
set_fact:
controlplane_join_cmd: "{{ join_command_base }} --control-plane --certificate-key {{ certificate_key }}"
- name: Full worker join command
set_fact:
worker_join_cmd: "{{ join_command_base }}"
- name: Show join commands
debug:
msg:
controlplane: "{{ controlplane_join_cmd }}"
worker: "{{ worker_join_cmd }}"
when: hostvars[inventory_hostname].role_node == "control-first"
when: not k8s_init_stat.stat.exists
# when: k8s_init_stat.stat.exists