summaryrefslogtreecommitdiffstats
path: root/roles/common/tasks/fail2ban.yml
blob: e56deafdf3ef4c53cbf543e223d541e5c1d27b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
- name: Install fail2ban
  apt: pkg=fail2ban

# Log into a dedicate directory so we can use ReadWriteDirectories in
# the .service file
- name: Create directory /var/log/fail2ban
  file: path=/var/log/fail2ban
        state=directory
        owner=root group=adm
        mode=0750

- name: Fix fail2ban logrotate snippet
  lineinfile: dest=/etc/logrotate.d/fail2ban
              state=present
              line="/var/log/fail2ban/*.log"
              insertbefore="^[^#]*\\s{$"
  tags:
    - logrotate

- name: Configure fail2ban (fail2ban.local)
  copy: src=etc/fail2ban/fail2ban.local
        dest=/etc/fail2ban/fail2ban.local
        owner=root group=root
        mode=0644
  register: r1
  notify:
    - Restart fail2ban

- name: Configure fail2ban (jail.local)
  template: src=etc/fail2ban/jail.local.j2
            dest=/etc/fail2ban/jail.local
            owner=root group=root
            mode=0644
  register: r2
  notify:
    - Restart fail2ban

- name: Configure fail2ban (action.d/nftables-allports.local)
  copy: src=etc/fail2ban/action.d/nftables-allports.local
        dest=/etc/fail2ban/action.d/nftables-allports.local
        owner=root group=root
        mode=0644
  register: r3
  notify:
    - Restart fail2ban

- name: Copy filters
  copy: src=etc/fail2ban/filter.d/
        dest=/etc/fail2ban/filter.d/
        owner=root group=root
        mode=0644
  register: r4
  notify:
    - Restart fail2ban

- name: Create directory /etc/systemd/system/fail2ban.service.d
  file: path=/etc/systemd/system/fail2ban.service.d
        state=directory
        owner=root group=root
        mode=0755

- name: Harden fail2ban.service
  copy: src=etc/systemd/system/fail2ban.service.d/override.conf
        dest=/etc/systemd/system/fail2ban.service.d/override.conf
        owner=root group=root
        mode=0644
  register: r5
  notify:
    - systemctl daemon-reload
    - Restart fail2ban

- name: Start fail2ban
  service: name=fail2ban state=started
  when: not (r1.changed or r2.changed or r3.changed or r4.changed or r5.changed)

- meta: flush_handlers

- name: Delete /var/lib/fail2ban/fail2ban.sqlite3
  file: path=/var/lib/fail2ban/fail2ban.sqlite3 state=absent