---
# Start Kubernetes

- name: kubeadm reset
  command: kubeadm reset

- name: restart kubelet service
  systemd:
    name: kubelet
    state: restarted
    enabled: yes

- name: optionally set kubeconfig option
  set_fact:
    kubeconfig: '{{ (kubernetes_version >= "1.5.4") | ternary("--kubeconfig /etc/kubernetes/admin.conf", "") }}'
    kubernetes_version_option: '{{ (kubernetes_version >= "1.6") | ternary("kubernetes_version", "use-kubernetes-version") }}'

- name: kubeadm init on the master
  command: 'kubeadm init --{{ kubernetes_version_option }}=v{{ kubernetes_version }} --token={{ kubernetes_token }}'
  when: ' {{ play_hosts[0] == inventory_hostname }}'

- name: allow pods to be run on the master (if only node)
  command: 'kubectl {{ kubeconfig }} taint nodes --all {{ (kubernetes_version < "1.6") | ternary("dedicated-", "node-role.kubernetes.io/master:NoSchedule-") }}'
  when: '{{ play_hosts | length }} == 1'

- name:  kubeadm join on workers
  command: 'kubeadm join --token={{ kubernetes_token }} {{ hostvars[play_hosts[0]].private_ip }}{{ (kubernetes_version > "1.6") | ternary(":6443", "") }}'
  when: ' {{ play_hosts[0] != inventory_hostname }}'

- name: list kubernetes' pods
  command: kubectl {{ kubeconfig }} get pods --all-namespaces
  when: ' {{ play_hosts[0] == inventory_hostname }}'
  changed_when: false
  register: kubectl_get_pods
  tags:
    - output

- name: print outpout of `kubectl get pods --all-namespaces`
  debug: msg="{{ kubectl_get_pods.stdout_lines }}"
  when: ' {{ play_hosts[0] == inventory_hostname }}'
  tags:
    - output
