---
- name: Copy CentOS repo templates
  vars:
    repo_file_prefix: "{{ 'CentOS-Stream' if ansible_facts.distribution == 'CentOS' else 'Rocky' }}"
  template:
    src: "{{ item }}.j2"
    dest: /etc/yum.repos.d/{{ item }}
    owner: root
    group: root
    mode: 0664
  become: True
  loop:
    - "{{ repo_file_prefix }}-AppStream.repo"
    - "{{ repo_file_prefix }}-BaseOS.repo"
    - "{{ repo_file_prefix }}-Extras.repo"

- name: Remove old (pre CentOS 8.3) repo files
  file:
    path: /etc/yum.repos.d/{{ item }}
    state: absent
  become: True
  loop:
    - CentOS-AppStream.repo
    - CentOS-Base.repo
    - CentOS-Extras.repo
  when: ansible_facts.distribution == 'CentOS'

- name: Update cache
  dnf:
    name: []
    update_cache: yes
  become: True

# NOTE(mgoddard): Install epel-release to ensure it does not get installed
# later and override our repo file.
- name: Install epel-release
  dnf:
    name: epel-release
    state: installed
  become: True
  when: dnf_install_epel | bool

- name: Copy EPEL repo templates
  template:
    src: "{{ item }}.j2"
    dest: /etc/yum.repos.d/{{ item }}
    owner: root
    group: root
    mode: 0664
  become: True
  loop:
    - epel.repo
    - epel-modular.repo
  when: dnf_install_epel | bool

- name: Update cache
  dnf:
    name: []
    update_cache: yes
  become: True
  when: dnf_install_epel | bool
