---
- name: Enable kubelet service
  systemd:
    name: kubelet
    enabled: true
    masked: false

- name: Check if Kubernetes has already been initialized.
  stat:
    path: /etc/kubernetes/admin.conf
  register: kubernetes_init_stat
          
# Set up master.
- include_tasks: 03-k8s-master.yml
  when: kubernetes_role == 'master'

# Set up nodes.
- name: Get the kubeadm join command from the Kubernetes master.
  command: kubeadm token create --print-join-command
  changed_when: false
  when: kubernetes_role == 'master'
  register: kubernetes_join_command_result

- name: Get kubeconfig
  fetch:
    src: /etc/kubernetes/admin.conf
    dest: "{{ ansible_kube_config }}"
    flat: yes
  when: kubernetes_role == 'master'

- name: Set the kubeadm join command globally.
  set_fact:
    kubernetes_join_command: >
      {{ kubernetes_join_command_result.stdout }}
      {{ kubernetes_join_command_extra_opts }}
  when: kubernetes_join_command_result.stdout is defined
  delegate_to: "{{ item }}"
  delegate_facts: true
  with_items: "{{ groups['all'] }}"

- include_tasks: 05-k8s-node.yml
  when: kubernetes_role == 'node'

