---
- name: Install the repository {{ agent_rhel_repo }}
  ansible.builtin.dnf:
    name: "{{ agent_rhel_repo }}"
    state: present
    disable_gpg_check: true
# when: "'zabbix-release' not in ansible_facts.packages"

- name: "DNF: Zabbix packages presence {{ agent_internal_install_package_list }}"
  become: true
  notify: Service restart
  until: agent_register_packages_state is not failed
  retries: 3
  delay: 3
  register: agent_register_packages_state
  ansible.builtin.dnf:
    name: "{{ agent_internal_install_package_list }}"
    state: "{{ agent_package_state }}"
    disablerepo: "{{ agent_repository_disable }}" # disable epel repos | should contain epel word in repo name
    # enablerepo: '{{ agent_repository.name }}'
    install_weak_deps: true
    # enable_plugin: priorities ### cant find the plugin by its name... seems bugged
  vars:
    ### if "agent_minor_version" is defined calculate "agent_internal_full_version".
    agent_internal_full_version: '{{ None if (agent_minor_version is not defined or not agent_minor_version) else agent_major_version | string + "." + agent_minor_version
      | string }}'
    ### change minor version of zabbix components (excluding agent2 plugins). Use single quotes for regex expression!!!
    agent_internal_component_package_list: "{{ agent_package_list if not agent_internal_full_version else agent_package_list | map('regex_replace', '^(zabbix-(get|sender|agent2|agent))$',
      '\\1-%s' % agent_internal_full_version) | list }}"
    ### change version of agent2 plugins. Use single quotes for regex expression!!!
    agent_internal_install_package_list: "{{ agent_internal_component_package_list if (agent_2_plugin_version is not defined or not agent_2_plugin_version or agent_variant
      | string != '2') else agent_internal_component_package_list | map('regex_replace', '^(zabbix-agent2-plugin-(mongodb|postgresql))$', '\\1-%s' % agent_2_plugin_version)
      | list }}"
  environment:
    http_proxy: "{{ agent_http_proxy | default(None) | default(omit) }}"
    https_proxy: "{{ agent_https_proxy | default(None) | default(omit) }}"
  tags: [deploy]
