I think it’s more flexible to use the template mechanics see template_module
This would allow to always generate on target the desired iptables, and more importantly, the order of rules
*filter
-I INPUT -p tcp --dport {{ sshport }} -m state --state NEW -m recent --set
-I INPUT -p tcp --dport {{ sshport }} -m state --state NEW -m recent --update --seconds 10 --hitcount 10 -j DROP
COMMIT
I’ve tried to use the community.general.iptables_state, but it didn’t make the iptables rules persistent, that’s why i choose to use the ansible.builtin.shell.
- name: save current state of the firewall in system file
community.general.iptables_state:
ip_version: ipv6
table: filter
state: saved
path: /etc/iptables/rules.v6