# SPDX-License-Identifier: BSD-3-Clause
---
- name: Create EPEL yum repo and Enable EPEL
  when:
    - ansible_distribution in ['RedHat', 'CentOS']
    - ansible_distribution_major_version in ['7', '8']
  block:
    - name: Create EPEL {{ ansible_distribution_major_version }}
      vars:
        __epel_7:
          "https://dl.fedoraproject.org/pub/archive/epel/7/x86_64/\
          Packages/e/epel-release-7-14.noarch.rpm"
        __epel: https://dl.fedoraproject.org/pub/epel/epel-release-latest-{{
            ansible_distribution_major_version }}.noarch.rpm
        __epel_url: "{{
          __epel_7 if ansible_distribution_major_version == '7'
          else __epel }}"
      command:
        cmd: rpm -iv {{ __epel_url }}
        # noqa command-instead-of-module
        creates: /etc/yum.repos.d/epel.repo
      register: __epel_status
      # sometimes the rpm download returns a 403 - I think it is when too
      # many parallel jobs attempt the download from the same controller in
      # too short a period of time, so the epel server throttles additional
      # downloads - use a retry here to mitigate
      until: __epel_status is success
      retries: 6
      delay: 10

    - name: Install yum-utils package
      package:
        state: present
        name: yum-utils
      when: ansible_distribution_major_version == '7'

    - name: Enable EPEL 7
      command: yum-config-manager --enable epel
      when: ansible_distribution_major_version == '7'
      changed_when: false

    - name: Enable EPEL 8
      command: dnf config-manager --set-enabled epel
      when: ansible_distribution_major_version == '8'
      changed_when: false

- name: Enable EPEL 6
  copy:
    dest: /etc/yum.repos.d/epel.repo
    content: |
      [epel]
      name=Extra Packages for Enterprise Linux 6 - $basearch
      baseurl=https://archives.fedoraproject.org/pub/archive/epel/6/$basearch
      enabled=1
      gpgcheck=0
    mode: "0644"
  when:
    - ansible_distribution in ['RedHat', 'CentOS']
    - ansible_distribution_major_version == '6'
