---
- name: Import GPG key_1
  apt_key:
    url: https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:{{ crio.version }}/{{ crio.os }}/Release.key
    state: present
  become: true

- name: Import GPG key_2
  apt_key:
    url: https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/{{ crio.os }}/Release.key
    state: present
  become: true

- name: Add crio repository_1
  apt_repository:
    repo: deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/{{crio.os }}/ /
    state: present
    filename: devel:kubic:libcontainers:stable.list

- name: Add crio repository_2
  apt_repository:
    repo: deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/{{ crio.version }}/{{ crio.os }}/ /
    state: present
    filename: devel:kubic:libcontainers:stable:cri-o:{{ crio.version }}.list

- name: Create crio configuration directory
  file:
    path: /etc/containers/registries.conf.d
    state: directory

- name: Configure crio
  template:
    src: myregistry.conf.j2
    dest: /etc/containers/registries.conf.d/myregistry.conf
  notify:
    - Restart crio service

- name: Install required packages
  apt:
    name: ['cri-o', 'cri-o-runc']
    state: present
    update_cache: yes
  notify:
    - Reload systemd configuration
    - Restart crio service

- meta: flush_handlers

- name: Enable crio service
  service:
    name: crio
    enabled: True
    state: started
   
- name: Install kubernetes
  block:
    - name: 'Add kubernetes repo key'
      apt_key:
        url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
        state: present
      become: true
    - name: Add kubernetes repository
      apt_repository:
        repo: deb http://apt.kubernetes.io kubernetes-xenial main
        state: present
        filename: 'kubernetes'
      become: true
    - name: Install kubernetes components
      apt: 
        name: ['kubelet={{kubernetes_version}}-*', 'kubeadm={{kubernetes_version}}-*', 'kubectl={{kubernetes_version}}-*']
        state: present
        update_cache: yes
        force: yes
        dpkg_options: force-downgrade

- name: Hold kubernetes packages
  dpkg_selections:
    name: "{{item}}"
    selection: hold
  with_items:
    - kubelet
    - kubectl
    - kubeadm

