Skip to content

Commit 945f393

Browse files
authored
feat: Add fail2ban_exporter role (#294)
* feat: Add fail2ban_exporter role --------- Signed-off-by: anviar <oleg.kluchkin+github@gmail.com>
1 parent d044d88 commit 945f393

File tree

23 files changed

+630
-0
lines changed

23 files changed

+630
-0
lines changed

roles/fail2ban_exporter/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p><img src="https://www.circonus.com/wp-content/uploads/2015/03/sol-icon-itOps.png" alt="graph logo" title="graph" align="right" height="60" /></p>
2+
3+
# Ansible Role: fail2ban_exporter
4+
5+
## Description
6+
7+
Deploy prometheus [fail2ban_exporter](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter) using ansible.
8+
9+
## Requirements
10+
11+
- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it)
12+
- gnu-tar on Mac deployer host (`brew install gnu-tar`)
13+
- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`)
14+
15+
## Role Variables
16+
17+
All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml).
18+
Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/fail2ban_exporter_role.html) for description and default values of the variables.
19+
20+
## Example
21+
22+
### Demo site
23+
24+
We provide demo site for full monitoring solution based on prometheus and grafana. Repository with code and links to running instances is [available on github](https://github.com/prometheus/demo-site) and site is hosted on [DigitalOcean](https://digitalocean.com).
25+
26+
## Local Testing
27+
28+
The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable to for your system. Running your tests is as simple as executing `molecule test`.
29+
30+
## Continuous Intergation
31+
32+
Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have a quite large test matrix which can take more time than local testing, so please be patient.
33+
34+
## Contributing
35+
36+
See [contributor guideline](CONTRIBUTING.md).
37+
38+
## Troubleshooting
39+
40+
See [troubleshooting](TROUBLESHOOTING.md).
41+
42+
## License
43+
44+
This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
fail2ban_exporter_version: 0.10.1
3+
fail2ban_exporter_binary_local_dir: ""
4+
fail2ban_exporter_binary_url: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/\
5+
fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
6+
fail2ban_exporter_checksums_url: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/\
7+
fail2ban_exporter_{{ fail2ban_exporter_version }}_checksums.txt"
8+
fail2ban_exporter_skip_install: false
9+
10+
fail2ban_exporter_web_listen_address: "0.0.0.0:9191"
11+
fail2ban_exporter_socket: "/var/run/fail2ban/fail2ban.sock"
12+
fail2ban_exporter_binary_install_dir: "/usr/local/bin"
13+
14+
fail2ban_exporter_username: ""
15+
fail2ban_exporter_password: ""
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
- name: Restart fail2ban_exporter
3+
listen: "restart fail2ban_exporter"
4+
become: true
5+
ansible.builtin.systemd:
6+
daemon_reload: true
7+
name: fail2ban_exporter
8+
state: restarted
9+
when:
10+
- not ansible_check_mode
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
# yamllint disable rule:line-length
3+
argument_specs:
4+
main:
5+
short_description: "Prometheus fail2ban_exporter"
6+
description:
7+
- "Deploy prometheus L(fail2ban exporter,https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter) using ansible"
8+
author:
9+
- "Prometheus Community"
10+
options:
11+
fail2ban_exporter_version:
12+
description: "fail2ban_exporter package version. Also accepts latest as parameter."
13+
default: "0.10.1"
14+
fail2ban_exporter_skip_install:
15+
description: "fail2ban_exporter installation tasks gets skipped when set to true."
16+
type: bool
17+
default: false
18+
fail2ban_exporter_binary_local_dir:
19+
description:
20+
- "Enables the use of local packages instead of those distributed on gitlab."
21+
- "The parameter may be set to a directory where the C(fail2ban_exporter) binary is stored on the host where ansible is run."
22+
- "This overrides the I(fail2ban_exporter_version) parameter"
23+
fail2ban_exporter_binary_url:
24+
description: "URL of the fail2ban_exporter binaries .tar.gz file"
25+
default: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
26+
fail2ban_exporter_checksums_url:
27+
description: "URL of the fail2ban_exporter checksums file"
28+
default: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/fail2ban_exporter_{{ fail2ban_exporter_version }}_checksums.txt"
29+
fail2ban_exporter_web_listen_address:
30+
description: "Address on which fail2ban exporter will listen"
31+
default: "0.0.0.0:9191"
32+
fail2ban_exporter_binary_install_dir:
33+
description:
34+
- "I(Advanced)"
35+
- "Directory to install fail2ban_exporter binary"
36+
default: "/usr/local/bin"
37+
fail2ban_exporter_socket:
38+
description:
39+
- "Path to the fail2ban server socket"
40+
default: "/var/run/fail2ban/fail2ban.sock"
41+
fail2ban_exporter_username:
42+
description:
43+
- "I(Advanced)"
44+
- "Username to use to protect endpoints with basic auth"
45+
fail2ban_exporter_password:
46+
description:
47+
- "I(Advanced)"
48+
- "Password to use to protect endpoints with basic auth"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
galaxy_info:
3+
author: "Prometheus Community"
4+
description: "Prometheus fail2ban_exporter"
5+
license: "Apache"
6+
min_ansible_version: "2.9"
7+
platforms:
8+
- name: "Ubuntu"
9+
versions:
10+
- "focal"
11+
- "jammy"
12+
- name: "Debian"
13+
versions:
14+
- "bullseye"
15+
- "buster"
16+
- name: "EL"
17+
versions:
18+
- "7"
19+
- "8"
20+
- "9"
21+
- name: "Fedora"
22+
versions:
23+
- "37"
24+
- '38'
25+
galaxy_tags:
26+
- "monitoring"
27+
- "prometheus"
28+
- "exporter"
29+
- "metrics"
30+
- "system"
31+
- "fail2ban"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
provisioner:
3+
inventory:
4+
group_vars:
5+
all:
6+
fail2ban_exporter_binary_local_dir: "/tmp/fail2ban_exporter-linux-amd64_local"
7+
fail2ban_exporter_web_listen_address: "127.0.1.1:9191"
8+
go_arch: amd64
9+
fail2ban_exporter_version: 0.10.1
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
- name: Run local preparation
3+
hosts: localhost
4+
gather_facts: false
5+
tasks:
6+
- name: Download fail2ban_exporter binary to local folder
7+
become: false
8+
ansible.builtin.get_url:
9+
url: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/\
10+
fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
11+
dest: "/tmp/fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
12+
mode: 0644
13+
register: _download_binary
14+
until: _download_binary is succeeded
15+
retries: 5
16+
delay: 2
17+
check_mode: false
18+
19+
- name: Create local directory for binary
20+
ansible.builtin.file:
21+
path: "{{ fail2ban_exporter_binary_local_dir }}"
22+
state: directory
23+
mode: '0755'
24+
25+
- name: Unpack fail2ban_exporter binary
26+
become: false
27+
ansible.builtin.unarchive:
28+
src: "/tmp/fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
29+
dest: "{{ fail2ban_exporter_binary_local_dir }}"
30+
creates: "{{ fail2ban_exporter_binary_local_dir }}/fail2ban_exporter"
31+
check_mode: false
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from __future__ import (absolute_import, division, print_function)
2+
__metaclass__ = type
3+
4+
import os
5+
import testinfra.utils.ansible_runner
6+
7+
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
8+
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
9+
10+
11+
def test_service(host):
12+
s = host.service("fail2ban_exporter")
13+
try:
14+
assert s.is_running
15+
except AssertionError:
16+
# Capture service logs
17+
journal_output = host.run('journalctl -u fail2ban_exporter --since "1 hour ago"')
18+
print("\n==== journalctl -u fail2ban_exporter Output ====\n")
19+
print(journal_output)
20+
print("\n============================================\n")
21+
raise # Re-raise the original assertion error
22+
23+
24+
def test_protecthome_property(host):
25+
s = host.service("fail2ban_exporter")
26+
p = s.systemd_properties
27+
assert p.get("ProtectHome") == "yes"
28+
29+
30+
def test_socket(host):
31+
sockets = [
32+
"tcp://127.0.1.1:9191"
33+
]
34+
for socket in sockets:
35+
s = host.socket(socket)
36+
assert s.is_listening
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
provisioner:
3+
inventory:
4+
group_vars:
5+
all:
6+
fail2ban_exporter_web_listen_address: "127.0.0.1:9191"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from __future__ import (absolute_import, division, print_function)
2+
__metaclass__ = type
3+
4+
import os
5+
import testinfra.utils.ansible_runner
6+
7+
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
8+
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
9+
10+
11+
def test_files(host):
12+
files = [
13+
"/etc/systemd/system/fail2ban_exporter.service",
14+
"/usr/local/bin/fail2ban_exporter"
15+
]
16+
for file in files:
17+
f = host.file(file)
18+
assert f.exists
19+
assert f.is_file
20+
21+
22+
def test_service(host):
23+
s = host.service("fail2ban_exporter")
24+
try:
25+
assert s.is_running
26+
except AssertionError:
27+
# Capture service logs
28+
journal_output = host.run('journalctl -u fail2ban_exporter --since "1 hour ago"')
29+
print("\n==== journalctl -u fail2ban_exporter Output ====\n")
30+
print(journal_output)
31+
print("\n============================================\n")
32+
raise # Re-raise the original assertion error
33+
34+
35+
def test_protecthome_property(host):
36+
s = host.service("fail2ban_exporter")
37+
p = s.systemd_properties
38+
assert p.get("ProtectHome") == "yes"
39+
40+
41+
def test_socket(host):
42+
sockets = [
43+
"tcp://127.0.0.1:9191"
44+
]
45+
for socket in sockets:
46+
s = host.socket(socket)
47+
assert s.is_listening

0 commit comments

Comments
 (0)