---
- name: Kubernetes | include global vars for minions
  include_vars: "{{ansible_tmp_dir}}/generated_global_vars.yml"
  when: not master

- name: Kubernetes | include vars
  include_vars: "{{ ansible_distribution|lower }}.yml"

- name: Kubernetes | fetch network details
  include_tasks: set_ip_facts.yml

- debug:
    msg: "{{ansible_hostname}}"

- name: Kubernetes | Expecting all binaries to be already present
  set_fact:
    binaries_missing: false

- name: Kubernetes | Checking if binaries are already present on ansible machine
  local_action: stat path="{{ansible_tmp_dir}}/{{item}}"
  register: stat_bins_exists
  with_items:
    - "{{kubernetes_binaries.linux_common}}"
    - "{{kubernetes_binaries.linux_master}}"
    - "{{kubernetes_binaries.linux_minion}}"
    - "{{kubernetes_binaries.windows}}"

- name: Kubernetes | Checking all binaries
  set_fact:
    binaries_missing: true
  with_items:
    - "{{stat_bins_exists.results}}"
  loop_control:
    label: "{{item.item}}"
  when: not item.stat.exists

- debug:
    msg: "Binaries are missing: {{binaries_missing}}"

- name: Kubernetes | Get Kubernetes binaries
  include_tasks: ./download_k8s_binaries.yml
  when: binaries_missing

- name: Kubernetes | Fetch Kubernetes binaries to the ansible host
  include_tasks: ./fetch_k8s_binaries.yml
  when: binaries_missing

- name: Kubernetes | Distribute binaries
  include_tasks: ./distribute_binaries.yml

- name: Kubernetes | Prepare master
  include_tasks: ./prepare_master.yml
  when: master

- name: Kubernetes | Prepare minion
  include_tasks: ./prepare_minion.yml
  when: minion

- name: Kubernetes | Deploy CoreDNS
  include_tasks: ./deploy_coredns.yml
  when: master

- include_tasks: generate_global_vars.yml
  when: master
