---
# TODO(cristicalin): drop this file after 2.21
- name: CRI-O kubic repo name for debian os family
  set_fact:
    crio_kubic_debian_repo_name: "{{ ((ansible_distribution == 'Ubuntu') | ternary('x', '')) ~ ansible_distribution ~ '_' ~ ansible_distribution_version }}"
  when: ansible_os_family == "Debian"

- name: Remove legacy CRI-O kubic apt repo key
  apt_key:
    url: "https://{{ crio_download_base }}/{{ crio_kubic_debian_repo_name }}/Release.key"
    state: absent
  environment: "{{ proxy_env }}"
  when: crio_kubic_debian_repo_name is defined

- name: Remove legacy CRI-O kubic apt repo
  apt_repository:
    repo: "deb http://{{ crio_download_base }}/{{ crio_kubic_debian_repo_name }}/ /"
    state: absent
    filename: devel-kubic-libcontainers-stable
  when: crio_kubic_debian_repo_name is defined

- name: Remove legacy CRI-O kubic cri-o apt repo
  apt_repository:
    repo: "deb {{ crio_download_crio }}{{ crio_version }}/{{ crio_kubic_debian_repo_name }}/ /"
    state: absent
    filename: devel-kubic-libcontainers-stable-cri-o
  when: crio_kubic_debian_repo_name is defined

- name: Remove legacy CRI-O kubic yum repo
  yum_repository:
    name: devel_kubic_libcontainers_stable
    description: Stable Releases of Upstream github.com/containers packages (CentOS_$releasever)
    baseurl: http://{{ crio_download_base }}/CentOS_{{ ansible_distribution_major_version }}/
    state: absent
  when:
    - ansible_os_family == "RedHat"
    - ansible_distribution not in ["Amazon", "Fedora"]

- name: Remove legacy CRI-O kubic yum repo
  yum_repository:
    name: "devel_kubic_libcontainers_stable_cri-o_{{ crio_version }}"
    description: "CRI-O {{ crio_version }} (CentOS_$releasever)"
    baseurl: "{{ crio_download_crio }}{{ crio_version }}/CentOS_{{ ansible_distribution_major_version }}/"
    state: absent
  when:
    - ansible_os_family == "RedHat"
    - ansible_distribution not in ["Amazon", "Fedora"]

- name: Remove legacy CRI-O kubic yum repo
  yum_repository:
    name: devel_kubic_libcontainers_stable
    description: Stable Releases of Upstream github.com/containers packages
    baseurl: http://{{ crio_download_base }}/Fedora_{{ ansible_distribution_major_version }}/
    state: absent
  when:
    - ansible_distribution in ["Fedora"]
    - not is_ostree

- name: Remove legacy CRI-O kubic yum repo
  yum_repository:
    name: "devel_kubic_libcontainers_stable_cri-o_{{ crio_version }}"
    description: "CRI-O {{ crio_version }}"
    baseurl: "{{ crio_download_crio }}{{ crio_version }}/Fedora_{{ ansible_distribution_major_version }}/"
    state: absent
  when:
    - ansible_distribution in ["Fedora"]
    - not is_ostree

- name: Remove legacy CRI-O kubic yum repo
  yum_repository:
    name: devel_kubic_libcontainers_stable
    description: Stable Releases of Upstream github.com/containers packages
    baseurl: http://{{ crio_download_base }}/CentOS_7/
    state: absent
  when: ansible_distribution in ["Amazon"]

- name: Remove legacy CRI-O kubic yum repo
  yum_repository:
    name: "devel_kubic_libcontainers_stable_cri-o_{{ crio_version }}"
    description: "CRI-O {{ crio_version }}"
    baseurl: "{{ crio_download_crio }}{{ crio_version }}/CentOS_7/"
    state: absent
  when: ansible_distribution in ["Amazon"]

- name: Disable modular repos for CRI-O
  community.general.ini_file:
    path: "/etc/yum.repos.d/{{ item.repo }}.repo"
    section: "{{ item.section }}"
    option: enabled
    value: 0
    mode: 0644
  become: true
  when: is_ostree
  loop:
    - repo: "fedora-updates-modular"
      section: "updates-modular"
    - repo: "fedora-modular"
      section: "fedora-modular"

# Disable any older module version if we enabled them before
- name: Disable CRI-O ex module
  command: "rpm-ostree ex module disable cri-o:{{ item }}"
  become: true
  when:
    - is_ostree
    - ostree_version is defined and ostree_version.stdout is version('2021.9', '>=')
  with_items:
    - 1.22
    - 1.23
    - 1.24

- name: Cri-o | remove installed packages
  package:
    name: "{{ item }}"
    state: absent
  when: not is_ostree
  with_items:
    - cri-o
    - cri-o-runc
    - oci-systemd-hook
