The three rejection cases only printed "Rejected as expected", so they would also pass when the role failed for another reason. Assert that the failure carries the validation message of fail2ban_ignoreip. Assisted-by: Claude Code
131 lines
5.0 KiB
YAML
131 lines
5.0 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 for the right reason
|
|
ansible.builtin.assert:
|
|
that: "'fail2ban_ignoreip must be' in ansible_failed_result.msg"
|
|
quiet: true
|
|
|
|
# argument_specs accepts a plain string for a list option and leaves it a string, which would render one character per entry.
|
|
# validate.yml refuses it; the verify step proves it never reached jail.local.
|
|
- name: Converge a scalar ignoreip
|
|
hosts: all
|
|
tasks:
|
|
- name: Rejected scalar ignoreip
|
|
block:
|
|
- name: Run the role with a plain string instead of a list
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.fail2ban
|
|
vars:
|
|
fail2ban_ignoreip: "203.0.113.50"
|
|
- name: Fail when the invalid input was accepted
|
|
ansible.builtin.fail:
|
|
msg: fail2ban accepted a scalar string for fail2ban_ignoreip
|
|
rescue:
|
|
- name: The role refused the input for the right reason
|
|
ansible.builtin.assert:
|
|
that: "'fail2ban_ignoreip must be' in ansible_failed_result.msg"
|
|
quiet: true
|
|
|
|
# A semicolon after whitespace starts an inline comment in Fail2Ban's config files, which would drop the rest of the ignoreip line
|
|
# without an error, so validate.yml refuses it. The verify step proves it never reached jail.local.
|
|
- name: Converge an ignoreip entry that would start a comment
|
|
hosts: all
|
|
tasks:
|
|
- name: Rejected comment character
|
|
block:
|
|
- name: Run the role with an entry that starts with a semicolon
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.fail2ban
|
|
vars:
|
|
fail2ban_ignoreip:
|
|
- 127.0.0.1/8
|
|
- ";198.51.100.99"
|
|
- name: Fail when the invalid input was accepted
|
|
ansible.builtin.fail:
|
|
msg: fail2ban accepted an ignoreip entry that starts with a semicolon
|
|
rescue:
|
|
- name: The role refused the input for the right reason
|
|
ansible.builtin.assert:
|
|
that: "'fail2ban_ignoreip must be' in ansible_failed_result.msg"
|
|
quiet: true
|