---
# Debian / Ubuntu specific:

- name: add apt key for the kubernetes repository
  apt_key:
    url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
    state: present
  register: apt_key_k8s_repo

- name: add kubernetes' apt repository (kubernetes-{{ ansible_distribution_release }})
  apt_repository:
    repo: deb http://apt.kubernetes.io/ kubernetes-{{ ansible_distribution_release }} main
    state: present
  register: apt_k8s_repo
  when: '"alpha" not in kubernetes_version and "beta" not in kubernetes_version'

- name: add kubernetes' apt repository (kubernetes-{{ ansible_distribution_release }}-unstable)
  apt_repository:
    repo: deb http://apt.kubernetes.io/ kubernetes-{{ ansible_distribution_release }}-unstable main
    state: present
  register: apt_k8s_repo
  when: '"alpha" in kubernetes_version or "beta" in kubernetes_version'

- name: update apt's cache
  apt:
    update_cache: yes
  when: apt_key_k8s_repo.changed or apt_k8s_repo.changed

- name: install kubelet and kubectl
  package:
    name: "{{ item }}"
    state: present
  with_items:
    - kubelet={{ kubernetes_version }}*
    - kubectl={{ kubernetes_version }}*
    - kubeadm={{ kubernetes_version }}*
    - kubernetes-cni={{ kubernetes_cni_version }}*
