Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add fail2ban_exporter role #294

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions roles/fail2ban_exporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<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>

# Ansible Role: fail2ban_exporter

## Description

Deploy prometheus [fail2ban_exporter](https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter) using ansible.

## Requirements

- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it)
- gnu-tar on Mac deployer host (`brew install gnu-tar`)
- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`)

## Role Variables

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).
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.

## Example

### Demo site

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).

## Local Testing

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`.

## Continuous Intergation

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.

## Contributing

See [contributor guideline](CONTRIBUTING.md).

## Troubleshooting

See [troubleshooting](TROUBLESHOOTING.md).

## License

This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details.
15 changes: 15 additions & 0 deletions roles/fail2ban_exporter/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
fail2ban_exporter_version: 0.10.1
fail2ban_exporter_binary_local_dir: ""
fail2ban_exporter_binary_url: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/\
fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
fail2ban_exporter_checksums_url: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/\
fail2ban_exporter_{{ fail2ban_exporter_version }}_checksums.txt"
fail2ban_exporter_skip_install: false

fail2ban_exporter_web_listen_address: "0.0.0.0:9191"
fail2ban_exporter_socket: "/var/run/fail2ban/fail2ban.sock"
fail2ban_exporter_binary_install_dir: "/usr/local/bin"

fail2ban_exporter_username: ""
fail2ban_exporter_password: ""
10 changes: 10 additions & 0 deletions roles/fail2ban_exporter/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Restart fail2ban_exporter
listen: "restart fail2ban_exporter"
become: true
ansible.builtin.systemd:
daemon_reload: true
name: fail2ban_exporter
state: restarted
when:
- not ansible_check_mode
48 changes: 48 additions & 0 deletions roles/fail2ban_exporter/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
# yamllint disable rule:line-length
argument_specs:
main:
short_description: "Prometheus fail2ban_exporter"
description:
- "Deploy prometheus L(fail2ban exporter,https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter) using ansible"
author:
- "Prometheus Community"
options:
fail2ban_exporter_version:
description: "fail2ban_exporter package version. Also accepts latest as parameter."
default: "0.10.1"
fail2ban_exporter_skip_install:
description: "fail2ban_exporter installation tasks gets skipped when set to true."
type: bool
default: false
fail2ban_exporter_binary_local_dir:
description:
- "Enables the use of local packages instead of those distributed on gitlab."
- "The parameter may be set to a directory where the C(fail2ban_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(fail2ban_exporter_version) parameter"
fail2ban_exporter_binary_url:
description: "URL of the fail2ban_exporter binaries .tar.gz file"
default: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
fail2ban_exporter_checksums_url:
description: "URL of the fail2ban_exporter checksums file"
default: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/fail2ban_exporter_{{ fail2ban_exporter_version }}_checksums.txt"
fail2ban_exporter_web_listen_address:
description: "Address on which fail2ban exporter will listen"
default: "0.0.0.0:9191"
fail2ban_exporter_binary_install_dir:
description:
- "I(Advanced)"
- "Directory to install fail2ban_exporter binary"
default: "/usr/local/bin"
fail2ban_exporter_socket:
description:
- "Path to the fail2ban server socket"
default: "/var/run/fail2ban/fail2ban.sock"
fail2ban_exporter_username:
description:
- "I(Advanced)"
- "Username to use to protect endpoints with basic auth"
fail2ban_exporter_password:
description:
- "I(Advanced)"
- "Password to use to protect endpoints with basic auth"
31 changes: 31 additions & 0 deletions roles/fail2ban_exporter/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
galaxy_info:
author: "Prometheus Community"
description: "Prometheus fail2ban_exporter"
license: "Apache"
min_ansible_version: "2.9"
platforms:
- name: "Ubuntu"
versions:
- "focal"
- "jammy"
- name: "Debian"
versions:
- "bullseye"
- "buster"
- name: "EL"
versions:
- "7"
- "8"
- "9"
- name: "Fedora"
versions:
- "37"
- '38'
galaxy_tags:
- "monitoring"
- "prometheus"
- "exporter"
- "metrics"
- "system"
- "fail2ban"
9 changes: 9 additions & 0 deletions roles/fail2ban_exporter/molecule/alternative/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
provisioner:
inventory:
group_vars:
all:
fail2ban_exporter_binary_local_dir: "/tmp/fail2ban_exporter-linux-amd64_local"
fail2ban_exporter_web_listen_address: "127.0.1.1:9191"
go_arch: amd64
fail2ban_exporter_version: 0.10.1
31 changes: 31 additions & 0 deletions roles/fail2ban_exporter/molecule/alternative/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- name: Run local preparation
hosts: localhost
gather_facts: false
tasks:
- name: Download fail2ban_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/releases/v{{ fail2ban_exporter_version }}/downloads/\
fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
dest: "/tmp/fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
mode: 0644
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
check_mode: false

- name: Create local directory for binary
ansible.builtin.file:
path: "{{ fail2ban_exporter_binary_local_dir }}"
state: directory
mode: '0755'

- name: Unpack fail2ban_exporter binary
become: false
ansible.builtin.unarchive:
src: "/tmp/fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
dest: "{{ fail2ban_exporter_binary_local_dir }}"
creates: "{{ fail2ban_exporter_binary_local_dir }}/fail2ban_exporter"
check_mode: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


def test_service(host):
s = host.service("fail2ban_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u fail2ban_exporter --since "1 hour ago"')
print("\n==== journalctl -u fail2ban_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("fail2ban_exporter")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


def test_socket(host):
sockets = [
"tcp://127.0.1.1:9191"
]
for socket in sockets:
s = host.socket(socket)
assert s.is_listening
6 changes: 6 additions & 0 deletions roles/fail2ban_exporter/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
fail2ban_exporter_web_listen_address: "127.0.0.1:9191"
47 changes: 47 additions & 0 deletions roles/fail2ban_exporter/molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


def test_files(host):
files = [
"/etc/systemd/system/fail2ban_exporter.service",
"/usr/local/bin/fail2ban_exporter"
]
for file in files:
f = host.file(file)
assert f.exists
assert f.is_file


def test_service(host):
s = host.service("fail2ban_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u fail2ban_exporter --since "1 hour ago"')
print("\n==== journalctl -u fail2ban_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("fail2ban_exporter")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


def test_socket(host):
sockets = [
"tcp://127.0.0.1:9191"
]
for socket in sockets:
s = host.socket(socket)
assert s.is_listening
6 changes: 6 additions & 0 deletions roles/fail2ban_exporter/molecule/latest/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
fail2ban_exporter_version: latest
43 changes: 43 additions & 0 deletions roles/fail2ban_exporter/molecule/latest/tests/test_latest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import testinfra.utils.ansible_runner
import pytest

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


@pytest.mark.parametrize("files", [
"/etc/systemd/system/fail2ban_exporter.service",
"/usr/local/bin/fail2ban_exporter"
])
def test_files(host, files):
f = host.file(files)
assert f.exists
assert f.is_file


def test_service(host):
s = host.service("fail2ban_exporter")
try:
assert s.is_running
except AssertionError:
# Capture service logs
journal_output = host.run('journalctl -u fail2ban_exporter --since "1 hour ago"')
print("\n==== journalctl -u fail2ban_exporter Output ====\n")
print(journal_output)
print("\n============================================\n")
raise # Re-raise the original assertion error


def test_protecthome_property(host):
s = host.service("fail2ban_exporter")
p = s.systemd_properties
assert p.get("ProtectHome") == "yes"


def test_socket(host):
s = host.socket("tcp://0.0.0.0:9191")
assert s.is_listening
19 changes: 19 additions & 0 deletions roles/fail2ban_exporter/tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
- name: Copy the fail2ban_exporter systemd service file
ansible.builtin.template:
src: fail2ban_exporter.service.j2
dest: /etc/systemd/system/fail2ban_exporter.service
owner: root
group: root
mode: 0644
notify: restart fail2ban_exporter

- name: Allow fail2ban_exporter port in SELinux on RedHat OS family
community.general.seport:
ports: "{{ fail2ban_exporter_web_listen_address.split(':')[-1] }}"
proto: tcp
setype: http_port_t
state: present
when:
- ansible_version.full is version_compare('2.4', '>=')
- ansible_selinux.status == "enabled"