--- - 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