---
- name: Manage firewall
  when:
    - logging_manage_firewall | bool
    - logging_tcp_ports or logging_udp_ports or logging_tls_tcp_ports or logging_tls_udp_ports
  block:
    - name: Initialize logging_firewall_ports
      set_fact:
        logging_firewall_ports: []

    - name: Add tcp ports to logging_firewall_ports
      set_fact:
        logging_firewall_ports: "{{ logging_firewall_ports | union([{'port': item, 'state': 'enabled'}]) }}"
      loop: "{{ (logging_tcp_ports + logging_tls_tcp_ports) | map('regex_replace', '$', '/tcp') | list }}"
      when: (logging_tcp_ports + logging_tls_tcp_ports) | length > 0

    - name: Add udp ports to logging_firewall_ports
      set_fact:
        logging_firewall_ports: "{{ logging_firewall_ports | union([{'port': item, 'state': 'enabled'}]) }}"
      loop: "{{ (logging_udp_ports + logging_tls_udp_ports) | map('regex_replace', '$', '/udp') | list }}"
      when: (logging_udp_ports + logging_tls_udp_ports) | length > 0

    - name: Manage firewall for specified ports
      include_role:
        name: fedora.linux_system_roles.firewall
      vars:
        firewall: "{{ logging_firewall_ports }}"
      when:
        - logging_firewall_ports | d([])
