argument_specs accepts a plain string for a list option and leaves it a string, and accepts a list holding a number or null. The pattern check then iterated a string per character and rendered "1 . 2 . 3 . 4", or died with a TypeError on null. Check the type first (assert stops at the first false item). Also refuse ";" and "%" in an entry: Fail2Ban's config parser treats ";" after whitespace as an inline comment, which drops the rest of the ignoreip line without an error, and "%" as interpolation syntax, which fails the whole config after jail.local is written. Add Molecule cases for a scalar string and a semicolon entry. Document what review found: service_jail does not read the variable, the [DEFAULT] value replaces an ignoreip set in jail.conf or jail.d/*.conf, a failed hostname lookup leaves the entry ineffective with a log warning (addresses and networks are safer), and --tags fail2ban_configure skips the validation. Assisted-by: Claude Code
86 lines
3.4 KiB
YAML
86 lines
3.4 KiB
YAML
---
|
|
# What matters: the daemon accepts the whole configuration, loads the base jail and
|
|
# both service jails (each from its own jail.d file, #433), the sshd jail does not
|
|
# fall back to the empty auth.log, and a rejected input left nothing behind. The
|
|
# file contents are the templates' own output and are not restated here.
|
|
- name: Verify
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Read jail.local
|
|
ansible.builtin.slurp:
|
|
src: /etc/fail2ban/jail.local
|
|
register: fail2ban_verify_jail_local
|
|
|
|
- name: The sshd jail reads the journal, not auth.log
|
|
ansible.builtin.assert:
|
|
that: "'/var/log/auth.log' not in (fail2ban_verify_jail_local.content | b64decode)"
|
|
quiet: true
|
|
|
|
- name: The jail.local file carries the configured ignoreip in [DEFAULT] and not the rejected values
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "'[DEFAULT]' in (fail2ban_verify_jail_local.content | b64decode)"
|
|
- "'ignoreip = 127.0.0.1/8 ::1 192.0.2.0/24' in (fail2ban_verify_jail_local.content | b64decode)"
|
|
- "'198.51.100.7' not in (fail2ban_verify_jail_local.content | b64decode)"
|
|
- "'203.0.113.9' not in (fail2ban_verify_jail_local.content | b64decode)"
|
|
- "'203.0.113.50' not in (fail2ban_verify_jail_local.content | b64decode)"
|
|
- "'198.51.100.99' not in (fail2ban_verify_jail_local.content | b64decode)"
|
|
quiet: true
|
|
|
|
- name: Stat the service jails and the rejected one
|
|
ansible.builtin.stat:
|
|
path: "{{ item }}"
|
|
loop:
|
|
- /etc/fail2ban/jail.d/svc-a.local
|
|
- /etc/fail2ban/jail.d/svc-b.local
|
|
- /etc/fail2ban/jail.d/svc-bad.local
|
|
- /etc/fail2ban/filter.d/svc-bad.conf
|
|
register: fail2ban_verify_files
|
|
|
|
- name: Each service has its own file and the rejected input left nothing behind
|
|
ansible.builtin.assert:
|
|
that:
|
|
- fail2ban_verify_files.results[0].stat.exists
|
|
- fail2ban_verify_files.results[1].stat.exists
|
|
- not fail2ban_verify_files.results[2].stat.exists
|
|
- not fail2ban_verify_files.results[3].stat.exists
|
|
quiet: true
|
|
|
|
- name: Test the configuration the daemon will load
|
|
ansible.builtin.command: fail2ban-client -t
|
|
changed_when: false
|
|
|
|
- name: Ask the running daemon for its jails
|
|
ansible.builtin.command: fail2ban-client status
|
|
register: fail2ban_verify_status
|
|
changed_when: false
|
|
|
|
- name: The daemon loaded the base and both service jails
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "'sshd' in fail2ban_verify_status.stdout"
|
|
- "'svc-a' in fail2ban_verify_status.stdout"
|
|
- "'svc-b' in fail2ban_verify_status.stdout"
|
|
- "'svc-bad' not in fail2ban_verify_status.stdout"
|
|
quiet: true
|
|
|
|
# [DEFAULT] must reach the running jails: the base jail from jail.local and a service jail from jail.d alike.
|
|
- name: Ask the daemon for the ignored addresses of a base jail and a service jail
|
|
ansible.builtin.command: "fail2ban-client get {{ item }} ignoreip"
|
|
loop:
|
|
- sshd
|
|
- svc-a
|
|
register: fail2ban_verify_ignoreip
|
|
changed_when: false
|
|
|
|
- name: Both jails ignore the configured network
|
|
ansible.builtin.assert:
|
|
that:
|
|
- "'192.0.2.0/24' in item.stdout"
|
|
- "'198.51.100.7' not in item.stdout"
|
|
quiet: true
|
|
loop: "{{ fail2ban_verify_ignoreip.results }}"
|
|
loop_control:
|
|
label: "{{ item.item }}"
|