--- # 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/.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]