feat(fail2ban): add the fail2ban role
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
This commit is contained in:
1 parent
8ff66fb4e8
commit
1ea478b124
19 files changed
+560
No files matched your search
@@ -0,0 +1,10 @@
|
||||
---
|
||||
- name: Configure Fail2Ban jail.local
|
||||
become: true
|
||||
ansible.builtin.template:
|
||||
src: jail.local.j2
|
||||
dest: /etc/fail2ban/jail.local
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0600"
|
||||
notify: Restart Fail2Ban
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
- name: Install Fail2Ban package
|
||||
become: true
|
||||
ansible.builtin.package:
|
||||
name:
|
||||
- "{{ fail2ban_package_name }}"
|
||||
# Required for `backend = systemd` jails (journalmatch), e.g. gitea's.
|
||||
- python3-systemd
|
||||
state: present
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
- name: Install Fail2Ban
|
||||
ansible.builtin.import_tasks: install.yml
|
||||
tags: [fail2ban, fail2ban_install]
|
||||
|
||||
- name: Validate Fail2Ban jails
|
||||
ansible.builtin.import_tasks: validate.yml
|
||||
tags: [fail2ban, fail2ban_validate]
|
||||
|
||||
- name: Configure Fail2Ban
|
||||
ansible.builtin.import_tasks: configure.yml
|
||||
tags: [fail2ban, fail2ban_configure]
|
||||
|
||||
- name: Ensure Fail2Ban service
|
||||
ansible.builtin.import_tasks: service.yml
|
||||
tags: [fail2ban, fail2ban_service]
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
# -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'
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
# 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
|
||||
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
|
||||
|
||||
# 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
|
||||
tags: [fail2ban, fail2ban_service_jail]
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
- 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 }}"
|
||||
Reference in new issue
Block a user