---
- name: Ensure kubeadm initialization
  become: yes
  command: "kubeadm init --token 2f1a31.00f66dec74fd53f3 --apiserver-advertise-address=172.42.42.1"
  tags:
    - k8s

- name: Copy config to /Vagrant for other VMs
  become: yes
  copy:
    src: /etc/kubernetes/admin.conf
    dest: /vagrant/admin.conf
    owner: ubuntu
    group: ubuntu
    mode: 0600

- name: Copy config to home directory
  become: yes
  copy:
    src: /etc/kubernetes/admin.conf
    dest: /home/ubuntu/admin.conf
    owner: ubuntu
    group: ubuntu
    mode: 0600

- name: Update Environment
  become: yes
  lineinfile:
    path: /home/ubuntu/.bashrc
    regexp: '^export KUBECONFIG='
    line: 'export KUBECONFIG=/home/ubuntu/admin.conf'
    state: present

- name: Set --proxy-mode flag in kube-proxy daemonset (workaround for https://github.com/kubernetes/kubernetes/issues/34101)
  become: yes
  shell: "kubectl --kubeconfig=/home/ubuntu/admin.conf -n kube-system get ds -l 'k8s-app==kube-proxy' -o json | jq '.items[0].spec.template.spec.containers[0].command |= .+ [\"--proxy-mode=userspace\"]' | kubectl --kubeconfig=/home/ubuntu/admin.conf apply -f - && kubectl --kubeconfig=/home/ubuntu/admin.conf -n kube-system delete pods -l 'k8s-app==kube-proxy'"
  register: proxy
  until: proxy.rc == 0
  retries: 60
  delay: 10
  tags:
    - k8s

- name: Ensure Network Start Script
  become: yes
  copy:
    src: files/{{ item }}
    dest: /usr/local/bin/{{ item }}
    owner: root
    group: root
    mode: 0755
  with_items:
    - "start-weave"
    - "start-calico"
    - "start-canal"
