Copied from the environment repository's roles/fail2ban and scrubbed for the Collection: galaxy namespace mortis, the molecule scenario calls the role by its FQCN (and takes its paths from the workflow, like docker's), README examples use mortis.kamino.fail2ban, repository-local issue references are dropped. mock_roles in .ansible-lint lists the new FQCN. Not verified locally: the molecule scenario (runs on the kamino-molecule runner in CI) and ansible-test sanity. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
37 lines
1.2 KiB
YAML
37 lines
1.2 KiB
YAML
---
|
|
# -t loads every jail, so a jail that cannot start (missing logpath, bad
|
|
# backend) fails here. -d only dumps the config and treats those as warnings.
|
|
- name: Test Fail2Ban configuration
|
|
become: true
|
|
ansible.builtin.command: fail2ban-client -t
|
|
register: fail2ban_test
|
|
failed_when: fail2ban_test.rc != 0
|
|
changed_when: false
|
|
check_mode: false
|
|
|
|
- name: Ensure Fail2Ban service is running
|
|
become: true
|
|
ansible.builtin.systemd:
|
|
name: fail2ban
|
|
enabled: "{{ fail2ban_service.enabled | default(true) }}"
|
|
state: "{{ fail2ban_service.state | default('started') }}"
|
|
|
|
# Handlers run at the end of the play; flush so the restart happens before the
|
|
# jail check below.
|
|
- name: Apply pending Fail2Ban handlers
|
|
ansible.builtin.meta: flush_handlers
|
|
|
|
- name: Verify enabled jails are loaded
|
|
become: true
|
|
ansible.builtin.command: "fail2ban-client status {{ item.name }}"
|
|
loop: "{{ fail2ban_jails | selectattr('enabled') }}"
|
|
loop_control:
|
|
label: "{{ item.name }}"
|
|
register: fail2ban_jail_status
|
|
until: fail2ban_jail_status.rc == 0
|
|
retries: 5
|
|
delay: 2
|
|
changed_when: false
|
|
check_mode: false
|
|
when: fail2ban_service.state | default('started') == 'started'
|