---

- name: Handle consul ports in the firewall
  iptables_raw:
    name: consul_port_{{ item }}
    state: "{{ (('tcp' in consul_services[item].proto or 'udp' in consul_services[item].proto) and consul_services[item].src_ip | length > 0) | ternary('present', 'absent') }}"
    rules: |
      {% if 'tcp' in consul_services[item].proto %}
      -A INPUT -m state --state NEW -p tcp --dport {{ consul_services[item].port }} -s {{ consul_services[item].src_ip | join(',') }} -j ACCEPT
      {% endif %}
      {% if 'udp' in consul_services[item].proto %}
      -A INPUT -m state --state NEW -p udp --dport {{ consul_services[item].port }} -s {{ consul_services[item].src_ip | join(',') }} -j ACCEPT
      {% endif %}
  loop: "{{ consul_services.keys() | list }}"
  tags: firewall,consul
