---
# main tasks for kubernetes role

# install required system packages
- name: install system packages
  apt:
    state: present
    name: "{{item}}"
    update_cache: yes
  with_items:
    - git
    - virtualbox

# install kubectl
- stat: path="{{kubernetes_kubectl_path}}"
  register: kubectl_exe

- name: download kubectl
  get_url:
    url: "{{ kubernetes_kubectl_url }}"
    dest: "{{kubernetes_kubectl_path}}"
    mode: 0755
  when: kubectl_exe.stat.exists == False

# get vagrant file from git
- name: fetch vagrantfile
  git:
    repo: https://github.com/coreos/coreos-kubernetes.git
    dest: /opt/home.xai-corp.net/coreos-kubernetes

- stat: path="{{kubernates_vagrant_config_path}}/config.rb"
  register: k8s_config

- name: create k8s config
  command: cp {{kubernates_vagrant_config_path}}/config.rb.sample {{kubernates_vagrant_config_path}}/config.rb

# update vagrant box
- name: update vagrant box
  command: vagrant box update
  args:
    chdir: "{{kubernates_vagrant_config_path}}"

- name: vagrant up
  command: vagrant up
  args:
    chdir: "{{kubernates_vagrant_config_path}}"

- name: configure kubectl
  command: "{{ item }}"
  with_items:
    - kubectl config set-cluster vagrant-multi-cluster --server=https://172.17.4.99:443 --certificate-authority=${PWD}/ssl/ca.pem
    - kubectl config set-credentials vagrant-multi-admin --certificate-authority=${PWD}/ssl/ca.pem --client-key=${PWD}/ssl/admin-key.pem --client-certificate=${PWD}/ssl/admin.pem
    - kubectl config set-context vagrant-multi --cluster=vagrant-multi-cluster --user=vagrant-multi-admin
    - kubectl config use-context vagrant-multi
