Allow incoming SYN packets to all listening ports using iptables and Ansible

- name: Gather facts on listening ports
  listen_ports_facts:

- name: iptables - allow incoming SYN packets to all listening TCP ports
  iptables:
    chain: INPUT
    protocol: tcp
    destination_port: "{{ item }}"
    ctstate: NEW
    jump: ACCEPT
  loop: "{{ ansible_facts.tcp_listen  | map(attribute='port') | sort | unique | list }}"
  become: yes

- name: iptables - allow all incoming SYN packets to all listening UDP ports
  iptables:
    chain: INPUT
    protocol: udp
    destination_port: "{{ item }}"
    ctstate: NEW
    jump: ACCEPT
  loop: "{{ ansible_facts.udp_listen  | map(attribute='port') | sort | unique | list }}"
  become: yes