---
- name: Update repo cache (Debian)
  ansible.builtin.apt:
    update_cache: true
    cache_valid_time: 86400
  when:
    ansible_facts['os_family'] == "Debian"

- name: Install pwgen (Debian)
  ansible.builtin.apt:
    name: pwgen
    state: present
  when:
    ansible_facts['os_family'] == "Debian"

- name: Add and enable a new repository
  ansible.builtin.apt_repository:
    repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/packages.networkradius.com.asc] \
           http://packages.networkradius.com/freeradius-3.2/debian/bookworm bookworm main"
    state: present
    update_cache: true
  when:
    ansible_facts['os_family'] == "Debian"

- name: Install/update FreeRADIUS (Debian)
  ansible.builtin.apt:
    name: freeradius
    allow_unauthenticated: true
    state: present
  when:
    ansible_facts['os_family'] == "Debian"

# - name: Install EPEL repository (CentOS/RHEL)
#   dnf: name=epel-release state=latest
#   when: ansible_facts['os_family'] == "RedHat"

- name: Install pwgen (CentOS/RHEL)
  ansible.builtin.dnf:
    name: pwgen
    state: present
  when:
    ansible_facts['os_family'] == "RedHat"

# name: Enable CRB repository
#  community.general.dnf_config_manager:
#    name: crb
#    state: enabled
#  become: true
#  when:
#    ansible_facts['os_family'] == "RedHat"

- name: Set distribution version
  ansible.builtin.set_fact:
    releasever: "{{ ansible_facts['distribution_major_version'] }}"
  when:
    ansible_facts['os_family'] == "RedHat"

- name: Добавление GPG ключа репозитория
  ansible.builtin.rpm_key:
    state: present
    key: 'https://packages.inkbridgenetworks.com/pgp/packages.networkradius.com.asc'
  when:
    ansible_facts['os_family'] == "RedHat"

- name: Add and enable a custom repository
  ansible.builtin.yum_repository:
    name: freeradius
    description: FreeRadius Repo
    baseurl: 'http://packages.inkbridgenetworks.com/freeradius-3.2/rocky/{{ releasever }}/'
    gpgcheck: true
    enabled: true
  when:
    ansible_facts['os_family'] == "RedHat"

- name: Install/update FreeRADIUS (CentOS/RHEL)
  ansible.builtin.dnf:
    name:
      - freeradius
      - freeradius-utils
      # - freeradius-sqlite
    state: present
  when:
    ansible_facts['os_family'] == "RedHat"

- name: Enable FreeRADIUS
  ansible.builtin.systemd:
    name: "{{ fr_service_name }}.service"
    enabled: true
