aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/roles/firewall/templates/rules.v4.j2
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-08-20 23:02:06 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-08-20 23:02:08 +0200
commitc054d22c4818d916ac6dd9c8d7a7a2c614408e17 (patch)
treec0239045b1587cd950fd1d777f0c84fe5c23aa92 /roles/firewall/templates/rules.v4.j2
parentadd ansible_managed comment to templates (diff)
downloadinfra-ansible-c054d22c4818d916ac6dd9c8d7a7a2c614408e17.tar.gz
infra-ansible-c054d22c4818d916ac6dd9c8d7a7a2c614408e17.zip
append .j2 extension to templates
I saw a good point somewhere that it should help with syntax highlighting.
Diffstat (limited to 'roles/firewall/templates/rules.v4.j2')
-rw-r--r--roles/firewall/templates/rules.v4.j250
1 files changed, 50 insertions, 0 deletions
diff --git a/roles/firewall/templates/rules.v4.j2 b/roles/firewall/templates/rules.v4.j2
new file mode 100644
index 0000000..1bfeb94
--- /dev/null
+++ b/roles/firewall/templates/rules.v4.j2
@@ -0,0 +1,50 @@
+{{ ansible_managed | comment }}
+
+*filter
+
+# By default, drop incoming packets:
+:INPUT DROP [0:0]
+:FORWARD DROP [0:0]
+# By default, accept outgoing packets:
+:OUTPUT ACCEPT [0:0]
+
+# Accept packets for localhost:
+-A INPUT -i lo -j ACCEPT
+
+# Accept any packet for an open connection:
+-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
+-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
+
+# The SSH port is always open:
+{% set ssh_port = hostvars[inventory_hostname].ansible_port %}
+
+# Open TCP ports:
+{% set tcp_ports = [ssh_port] + firewall_ports_tcp + firewall_ports4_tcp %}
+{% set tcp_ports = tcp_ports | unique %}
+
+{% for port in tcp_ports %}
+{% set num = port.port if port.port is defined else port %}
+{% set src = '-s ' + port.source if port.source is defined else '' %}
+-A INPUT -p tcp {{ src }} --dport {{ num }} -m conntrack --ctstate NEW -j ACCEPT
+{% endfor %}
+
+# Open UDP ports:
+{% set udp_ports = firewall_ports_udp + firewall_ports4_udp %}
+{% set udp_ports = udp_ports | unique %}
+
+{% for port in udp_ports %}
+{% set num = port.port if port.port is defined else port %}
+{% set src = '-s ' + port.source if port.source is defined else '' %}
+-A INPUT -p udp {{ src }} --dport {{ num }} -j ACCEPT
+{% endfor %}
+
+# Any additional IPv4 rules:
+{{ firewall_rules4 | join('\n') }}
+
+# ICMP; allow only pings and rate-limit them:
+-A INPUT -p icmp --icmp-type echo-request -m hashlimit --hashlimit-upto 5/s --hashlimit-mode srcip --hashlimit-srcmask 32 --hashlimit-name icmp-echo-drop -j ACCEPT
+
+# Log denies (this must be at the bottom of the file):
+-A INPUT -m limit --limit 3/min -j LOG --log-prefix "iptables denied: " --log-level 4
+
+COMMIT