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
58 lines
2.0 KiB
YAML
58 lines
2.0 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
|
|
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]
|