Files
Kamino/roles/fail2ban/molecule/default/converge.yml
T
DragonishandClaude Sonnet 5.5 57b15df1ac
Build / build (pull_request) Successful in 11s
Lint / lint (pull_request) Successful in 26s
Molecule / molecule (pull_request) Successful in 52s
feat(fail2ban): add fail2ban_ignoreip to exempt trusted addresses
Write fail2ban_ignoreip as ignoreip in the [DEFAULT] section of jail.local, so
it applies to every jail, the service jails in jail.d included. The default is
Fail2Ban's own (127.0.0.1/8 and ::1). Entries are validated (no whitespace,
\Z-anchored) because they are joined into one line.

The first run after upgrading adds the [DEFAULT] section and restarts Fail2Ban
once.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q72rsMCgwaR1sbYZangwbp
2026-11-15 11:55:17 +00:00

86 lines
3.1 KiB
YAML

---
- name: Converge the base role (jail.local, sshd jail on the systemd backend, one extra ignoreip entry)
hosts: all
roles:
- role: mortis.kamino.fail2ban
vars:
# 192.0.2.0/24 is TEST-NET-1 (RFC 5737), never routed. The loopback defaults are repeated: a list replaces the default.
fail2ban_ignoreip:
- 127.0.0.1/8
- "::1"
- 192.0.2.0/24
# The entry point service roles use. Two services must not overwrite each other:
# each gets its own jail.d file.
- name: Converge two service jails through the entry point
hosts: all
tasks:
- name: Service jail with a logpath
ansible.builtin.include_role:
name: mortis.kamino.fail2ban
tasks_from: service_jail
vars:
fail2ban_service_name: svc-a
fail2ban_service_jail:
port: "8080"
logpath: /var/log/svc-a.log
maxretry: 4
fail2ban_service_filter:
failregex: "^.*auth failure from <HOST>.*$"
- name: Service jail with a journalmatch
ansible.builtin.include_role:
name: mortis.kamino.fail2ban
tasks_from: service_jail
vars:
fail2ban_service_name: svc-b
fail2ban_service_jail:
port: "9090"
journalmatch: "_SYSTEMD_UNIT=ssh.service"
fail2ban_service_filter:
failregex: "^.*denied for <HOST>.*$"
# An input the entry point must reject. The block proves it is refused (the
# verify step proves no file was left behind); the play still passes, so the
# idempotence run stays clean.
- name: Missing failregex is rejected
block:
- name: Service jail without a failregex
ansible.builtin.include_role:
name: mortis.kamino.fail2ban
tasks_from: service_jail
vars:
fail2ban_service_name: svc-bad
fail2ban_service_jail:
logpath: /var/log/svc-a.log
fail2ban_service_filter: {}
- name: Fail when the invalid input was accepted
ansible.builtin.fail:
msg: service_jail accepted a filter without a failregex
rescue:
- name: The entry point refused the input
ansible.builtin.debug:
msg: Rejected as expected
# An ignoreip entry with whitespace must be refused before jail.local is written. The play still passes, so the idempotence
# run stays clean; the verify step proves the rejected value never reached the file.
- name: Converge a rejected ignoreip
hosts: all
tasks:
- name: Rejected ignoreip entry
block:
- name: Run the role with an entry that holds whitespace
ansible.builtin.include_role:
name: mortis.kamino.fail2ban
vars:
fail2ban_ignoreip:
- 127.0.0.1/8
- "198.51.100.7 203.0.113.9"
- name: Fail when the invalid input was accepted
ansible.builtin.fail:
msg: fail2ban accepted an ignoreip entry with whitespace
rescue:
- name: The role refused the input
ansible.builtin.debug:
msg: Rejected as expected