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
31 lines
1.8 KiB
YAML
31 lines
1.8 KiB
YAML
---
|
|
- name: Validate Fail2Ban jails
|
|
ansible.builtin.assert:
|
|
that:
|
|
- item.name is defined
|
|
- item.enabled is defined
|
|
- item.filter is defined
|
|
- item.logpath is defined or item.journalmatch is defined or item.backend | default('') == 'systemd'
|
|
- item.maxretry is defined
|
|
- item.bantime is defined
|
|
- item.findtime is defined
|
|
fail_msg: "Invalid Fail2Ban jail: {{ item }}"
|
|
loop: "{{ fail2ban_jails }}"
|
|
|
|
# argument_specs does not reject a scalar string (it accepts "1.2.3.4" and leaves it a string, which the filters below would
|
|
# then iterate per character), a mapping, or a list holding a number or null, so the type is checked here first. assert stops at
|
|
# the first false item, so the later ones never see a value they cannot handle.
|
|
# Entries are joined with spaces into one ignoreip line, so an entry holding whitespace would silently become two. Fail2Ban's
|
|
# config parser treats ";" after whitespace as an inline comment (the rest of the line is dropped without an error, and
|
|
# `fail2ban-client -t` cannot see it) and "%" as interpolation syntax (the whole config then fails to load, after jail.local is
|
|
# written), so neither is allowed. Addresses, networks and hostnames contain none of these. The pattern ends in \Z, not $:
|
|
# Python's $ also matches before a trailing newline.
|
|
- name: Validate the Fail2Ban ignoreip entries
|
|
ansible.builtin.assert:
|
|
that:
|
|
- fail2ban_ignoreip is sequence and fail2ban_ignoreip is not string and fail2ban_ignoreip is not mapping
|
|
- fail2ban_ignoreip | select('string') | list | length == fail2ban_ignoreip | length
|
|
- fail2ban_ignoreip | select('match', '^[^\s;%]+\Z') | list | length == fail2ban_ignoreip | length
|
|
fail_msg: "fail2ban_ignoreip must be a list of non-empty strings without whitespace, semicolons or percent signs: {{ fail2ban_ignoreip }}"
|
|
quiet: true
|