mirror of
https://gitlab.archlinux.org/archlinux/infrastructure.git
synced 2026-11-29 20:34:57 +01:00
Add a ban action that writes `deny <ip>;` lines into the nginx stream deny snippet, so bans actually match real client traffic behind the proxies. The action does not reload nginx itself, there instead is a separate systemd path unit does that with a cooldown period. The `nginx-limit-req` jail switches to this banaction when `nginx_stream_deny` is set on the host. Related-to: https://gitlab.archlinux.org/archlinux/infrastructure/-/work_items/766 Assisted-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Christian Heusel <christian@heusel.eu>
143 lines
3.2 KiB
YAML
143 lines
3.2 KiB
YAML
- name: Install fail2ban
|
|
package:
|
|
name: "fail2ban"
|
|
state: "present"
|
|
notify:
|
|
- Restart fail2ban
|
|
|
|
- name: Install conntrack-tools
|
|
package:
|
|
name: "conntrack-tools"
|
|
state: "present"
|
|
|
|
- name: Create systemd unit override path
|
|
file:
|
|
path: "/etc/systemd/system/fail2ban.service.d"
|
|
state: "directory"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: '0755'
|
|
|
|
- name: Install systemd unit override file
|
|
template:
|
|
src: "fail2ban.service.j2"
|
|
dest: "/etc/systemd/system/fail2ban.service.d/override.conf"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: '0644'
|
|
|
|
- name: Install local config files
|
|
template:
|
|
src: "{{ item }}.j2"
|
|
dest: "/etc/fail2ban/{{ item }}"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: '0644'
|
|
loop:
|
|
- "fail2ban.local"
|
|
- "jail.local"
|
|
notify:
|
|
- Restart fail2ban
|
|
|
|
- name: Install firewallcmd-ipset-allports.conf
|
|
template:
|
|
src: "firewallcmd-ipset-allports.conf.j2"
|
|
dest: "/etc/fail2ban/action.d/firewallcmd-ipset-allports.conf"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: '0644'
|
|
notify:
|
|
- Restart fail2ban
|
|
|
|
- name: Install nginx-stream-deny action
|
|
when: nginx_stream_deny | default(false)
|
|
template:
|
|
src: "nginx-stream-deny.conf.j2"
|
|
dest: "/etc/fail2ban/action.d/nginx-stream-deny.conf"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: '0644'
|
|
notify:
|
|
- Restart fail2ban
|
|
|
|
- name: Install sshd jail
|
|
when: fail2ban_jails.sshd
|
|
template:
|
|
src: "sshd.jail.j2"
|
|
dest: "/etc/fail2ban/jail.d/sshd.local"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: '0644'
|
|
notify:
|
|
- Reload fail2ban jails
|
|
|
|
- name: Install postfix jail
|
|
when: fail2ban_jails.postfix
|
|
template:
|
|
src: "postfix.jail.j2"
|
|
dest: "/etc/fail2ban/jail.d/postfix.local"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: '0644'
|
|
notify:
|
|
- Reload fail2ban jails
|
|
|
|
- name: Install dovecot jail
|
|
when: fail2ban_jails.dovecot
|
|
template:
|
|
src: "dovecot.jail.j2"
|
|
dest: "/etc/fail2ban/jail.d/dovecot.local"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: '0644'
|
|
notify:
|
|
- Reload fail2ban jails
|
|
|
|
- name: Install nginx-limit-req jail
|
|
when: fail2ban_jails.nginx_limit_req
|
|
template:
|
|
src: "nginx-limit-req.jail.j2"
|
|
dest: "/etc/fail2ban/jail.d/nginx-limit-req.local"
|
|
owner: "root"
|
|
group: "root"
|
|
mode: '0644'
|
|
notify:
|
|
- Reload fail2ban jails
|
|
|
|
- name: Install fail2ban ipset for firewalld
|
|
copy: src="{{ item }}" dest=/etc/firewalld/ipsets/ owner=root group=root mode=0644
|
|
register: result
|
|
loop:
|
|
- fail2ban.xml
|
|
- fail2ban6.xml
|
|
|
|
- name: Restart firewalld
|
|
systemd_service: name=firewalld state=stopped
|
|
when: result.changed
|
|
|
|
- name: Restart firewalld
|
|
systemd_service: name=firewalld state=started
|
|
when: result.changed
|
|
|
|
- name: Add fail2ban ipset to the firewalld drop zone
|
|
ansible.posix.firewalld: source="ipset:{{ item }}" zone=drop permanent=true state=enabled
|
|
register: result
|
|
loop:
|
|
- fail2ban
|
|
- fail2ban6
|
|
|
|
- name: Restart firewalld
|
|
systemd_service: name=firewalld state=stopped
|
|
when: result.changed
|
|
|
|
- name: Restart firewalld
|
|
systemd_service: name=firewalld state=started
|
|
when: result.changed
|
|
|
|
- name: Start and enable service
|
|
systemd_service:
|
|
name: "fail2ban.service"
|
|
enabled: true
|
|
state: started
|
|
daemon-reload: true
|