---
- name: Initialize Kubernetes master with kubeadm init.
  command: >
    kubeadm init
    --pod-network-cidr={{ kubernetes_pod_network.cidr }}
    --apiserver-advertise-address={{ kubernetes_apiserver_advertise_address | default(ansible_default_ipv4.address, true) }}
    {{ kubernetes_kubeadm_init_extra_opts }}
  register: kubeadmin_init
  when: not kubernetes_init_stat.stat.exists

- name: Print the init output to screen.
  debug:
    var: kubeadmin_init.stdout
    verbosity: 2
  when: not kubernetes_init_stat.stat.exists

- name: Ensure .kube directory exists.
  file:
    path: ~/.kube
    state: directory

- name: Symlink the kubectl admin.conf to ~/.kube/conf.
  file:
    src: /etc/kubernetes/admin.conf
    dest: ~/.kube/config
    state: link
    force: yes

- name: copy the kubectl config  to ~/.kube/ansible_config
  copy:
    src: /etc/kubernetes/admin.conf
    dest: ~/.kube/ansible_config
    remote_src: true

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

- name: Configure Calico networking and Metric Server
  include_tasks: 04-k8s-master-yaml.yml

- name: Kubectl Cheat Sheet
  lineinfile:
    path: ~/.bashrc
    line: "{{ item }}"
  with_items:
    - source <(kubectl completion bash)
    - alias k=kubectl
    - complete -o default -F __start_kubectl k
