Under --check the package task only reports a change, so on a fresh host the tasks after it ran against a missing binary, /etc directory or unit and failed. They now skip when the install would change in check mode. fail2ban registers __fail2ban_install and guards its template, service, config test, jail tasks and handler with the standard `not ansible_check_mode or ... is not changed` guard. ufw registers __ufw_install plus a command probe (ansible_check_mode is not set for a play-level check_mode inside include_role), combined once in vars/main.yml as __ufw_can_configure. flush_handlers is left unguarded: the handler it runs is guarded itself. A check-absent scenario per role purges the package, refreshes the APT cache and runs only `check`, so each fix is tested; one scenario covering both would make the ufw role a user of fail2ban and select the ufw and traefik scenarios on every fail2ban change. Fixes #478 Assisted-by: Claude Code
62 lines
2.3 KiB
YAML
62 lines
2.3 KiB
YAML
---
|
|
# Entry point for a service role that owns its own jail. Called with
|
|
# `include_role: name=mortis.kamino.fail2ban tasks_from=service_jail` and explicit vars; the
|
|
# jail lands in jail.d/<name>.local, never in the shared jail.local, so one
|
|
# play cannot overwrite another service's jail.
|
|
- name: Install Fail2Ban
|
|
ansible.builtin.import_tasks: install.yml
|
|
tags: [fail2ban, fail2ban_install]
|
|
|
|
- name: Validate service jail inputs
|
|
ansible.builtin.assert:
|
|
that:
|
|
- fail2ban_service_name is string
|
|
- fail2ban_service_jail is mapping
|
|
- fail2ban_service_filter is mapping
|
|
- fail2ban_service_filter.failregex is defined
|
|
- fail2ban_service_jail.logpath is defined or fail2ban_service_jail.journalmatch is defined
|
|
fail_msg: >-
|
|
fail2ban_service_jail and fail2ban_service_filter must be dicts. Pass the
|
|
variable as a single templated expression, not wrapped in a list or
|
|
embedded in a string.
|
|
tags: [fail2ban, fail2ban_service_jail]
|
|
|
|
- name: Deploy service filter definition
|
|
become: true
|
|
ansible.builtin.copy:
|
|
dest: "/etc/fail2ban/filter.d/{{ fail2ban_service_name }}.conf"
|
|
content: |
|
|
[Definition]
|
|
failregex = {{ fail2ban_service_filter.failregex }}
|
|
ignoreregex = {{ fail2ban_service_filter.ignoreregex | default('') }}
|
|
owner: root
|
|
group: root
|
|
mode: "0644"
|
|
notify: Reload Fail2Ban
|
|
# Under --check on a fresh host the package is not installed, so /etc/fail2ban is missing.
|
|
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
|
tags: [fail2ban, fail2ban_service_jail]
|
|
|
|
- name: Deploy service jail
|
|
become: true
|
|
ansible.builtin.template:
|
|
src: service_jail.local.j2
|
|
dest: "/etc/fail2ban/jail.d/{{ fail2ban_service_name }}.local"
|
|
owner: root
|
|
group: root
|
|
mode: "0644"
|
|
notify: Reload Fail2Ban
|
|
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
|
|
|
# Read-only, so it also runs in check mode. A failure leaves the broken file on
|
|
# disk and fails every run until fixed, instead of only the run that wrote it.
|
|
tags: [fail2ban, fail2ban_service_jail]
|
|
|
|
- name: Test Fail2Ban configuration
|
|
become: true
|
|
ansible.builtin.command: fail2ban-client -t
|
|
changed_when: false
|
|
check_mode: false
|
|
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
|
tags: [fail2ban, fail2ban_service_jail]
|