---
- import_playbook: hook/pre_install.yaml

# Load default variables and perform prechecks on all hosts
- hosts:
    - all
  gather_facts: true
  roles:
    - defaults
    - precheck
    - native/root

# Download all required software and generate certificates on the localhost
- hosts:
    - localhost
  roles:
    - certs/init
    - download

# Initialize all nodes and install necessary software packages
- hosts:
    - etcd
    - k8s_cluster
    - image_registry
    - nfs
  roles:
    - native

# Install the etcd cluster
- hosts:
    - etcd
  roles:
    - etcd

# Install the private image registry
- hosts:
    - image_registry
  roles:
    - image-registry

# Install the Kubernetes cluster
- hosts:
    - k8s_cluster
  gather_facts: true
  roles:
    - cri
    - kubernetes/pre-kubernetes
    - kubernetes/init-kubernetes
    - role: kubernetes/join-kubernetes
      when: 
        - .init_kubernetes_node | ne .inventory_hostname 
        - .kubernetes_install_LoadState.stdout | eq "not-found"    
    - role: kubernetes/certs
      when: 
        - .kubernetes.certs.renew
        - .groups.kube_control_plane | default list | has .inventory_hostname
  post_tasks:
    - name: Add custom labels to the cluster nodes
      command: |
        {{- range $k, $v := .kubernetes.custom_labels }}
        /usr/local/bin/kubectl label --overwrite node {{ $.hostname }} {{ $k }}={{ $v }}
        {{- end }}
      when: .kubernetes.custom_label | empty | not

# Install Kubernetes cluster software components (CNI and storage class) on a random control plane node
- hosts:
    - kube_control_plane|random
  roles:
    - cni
    - storage-class

- import_playbook: hook/post_install.yaml