feat: Add fail2ban_exporter role (#294)

* feat: Add fail2ban_exporter role

---------

Signed-off-by: anviar <oleg.kluchkin+github@gmail.com>
This commit is contained in:
Oleg Klyuchkin 2024-02-22 16:43:31 +03:00 committed by GitHub
parent d044d8894b
commit 945f39366c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 630 additions and 0 deletions

View file

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

View file

@ -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: ""

View file

@ -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

View file

@ -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"

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
fail2ban_exporter_web_listen_address: "127.0.0.1:9191"

View file

@ -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

View file

@ -0,0 +1,6 @@
---
provisioner:
inventory:
group_vars:
all:
fail2ban_exporter_version: latest

View file

@ -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

View file

@ -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"

View file

@ -0,0 +1,51 @@
---
- name: Get binary
when:
- fail2ban_exporter_binary_local_dir | length == 0
- not fail2ban_exporter_skip_install
block:
- name: Download fail2ban_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ fail2ban_exporter_binary_url }}"
dest: "/tmp/fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
checksum: "sha256:{{ __fail2ban_exporter_checksum }}"
mode: '0644'
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
- name: Unpack fail2ban_exporter binary
become: false
ansible.builtin.unarchive:
src: "/tmp/fail2ban_exporter_{{ fail2ban_exporter_version }}_linux_{{ go_arch }}.tar.gz"
dest: "/tmp"
creates: "/tmp/fail2ban_exporter"
delegate_to: localhost
check_mode: false
- name: Propagate fail2ban_exporter binaries
ansible.builtin.copy:
src: "/tmp/fail2ban_exporter"
dest: "{{ fail2ban_exporter_binary_install_dir }}/fail2ban_exporter"
mode: 0755
owner: root
group: root
notify: restart fail2ban_exporter
when: not ansible_check_mode
- name: Propagate locally distributed fail2ban_exporter binary
ansible.builtin.copy:
src: "{{ fail2ban_exporter_binary_local_dir }}/fail2ban_exporter"
dest: "{{ fail2ban_exporter_binary_install_dir }}/fail2ban_exporter"
mode: 0755
owner: root
group: root
when:
- fail2ban_exporter_binary_local_dir | length > 0
- not fail2ban_exporter_skip_install
notify: restart fail2ban_exporter

View file

@ -0,0 +1,63 @@
---
- name: Preflight
ansible.builtin.include_tasks:
file: preflight.yml
apply:
tags:
- fail2ban_exporter_install
- fail2ban_exporter_configure
- fail2ban_exporter_run
tags:
- fail2ban_exporter_install
- fail2ban_exporter_configure
- fail2ban_exporter_run
- name: Install
ansible.builtin.include_tasks:
file: install.yml
apply:
become: true
tags:
- fail2ban_exporter_install
when:
( not __fail2ban_exporter_is_installed.stat.exists ) or
( (__fail2ban_exporter_current_version_output.stderr_lines | length > 0)
and (__fail2ban_exporter_current_version_output.stderr_lines[0] != fail2ban_exporter_version) ) or
( (__fail2ban_exporter_current_version_output.stdout_lines | length > 0)
and (__fail2ban_exporter_current_version_output.stdout_lines[0] != fail2ban_exporter_version) ) or
( fail2ban_exporter_binary_local_dir | length > 0 )
tags:
- fail2ban_exporter_install
- name: SELinux
ansible.builtin.include_tasks:
file: selinux.yml
apply:
become: true
tags:
- fail2ban_exporter_configure
when: ansible_selinux.status == "enabled"
tags:
- fail2ban_exporter_configure
- name: Configure
ansible.builtin.include_tasks:
file: configure.yml
apply:
become: true
tags:
- fail2ban_exporter_configure
tags:
- fail2ban_exporter_configure
- name: Ensure fail2ban_exporter is enabled on boot
become: true
ansible.builtin.systemd:
daemon_reload: true
name: fail2ban_exporter
enabled: true
state: started
when:
- not ansible_check_mode
tags:
- fail2ban_exporter_run

View file

@ -0,0 +1,85 @@
---
- name: Assert usage of systemd as an init system
ansible.builtin.assert:
that: ansible_service_mgr == 'systemd'
msg: "This role only works with systemd"
- name: Install package fact dependencies
become: true
ansible.builtin.package:
name: "{{ _pkg_fact_req }}"
state: present
when: (_pkg_fact_req)
vars:
_pkg_fact_req: "{% if (ansible_pkg_mgr == 'apt') %}\
{{ ('python-apt' if ansible_python_version is version('3', '<') else 'python3-apt') }}
{% else %}\
{% endif %}"
- name: Gather package facts
ansible.builtin.package_facts:
when: "not 'packages' in ansible_facts"
- name: Assert that used version supports listen address type
ansible.builtin.assert:
that:
- >-
fail2ban_exporter_web_listen_address is string
- name: Naive assertion of proper listen address
ansible.builtin.assert:
that:
- >-
[fail2ban_exporter_web_listen_address] |
flatten |
reject('match', '.+:\\d+$') |
list |
length == 0
- name: Check if fail2ban_exporter is installed
ansible.builtin.stat:
path: "{{ fail2ban_exporter_binary_install_dir }}/fail2ban_exporter"
register: __fail2ban_exporter_is_installed
check_mode: false
tags:
- fail2ban_exporter_install
- name: Gather currently installed fail2ban_exporter version (if any)
ansible.builtin.command: "{{ fail2ban_exporter_binary_install_dir }}/fail2ban_exporter --version"
changed_when: false
register: __fail2ban_exporter_current_version_output
check_mode: false
when: __fail2ban_exporter_is_installed.stat.exists
tags:
- fail2ban_exporter_install
- name: Discover latest version
ansible.builtin.set_fact:
fail2ban_exporter_version: "{{ (lookup('url', 'https://gitlab.com/api/v4/projects/{{ _gitlab_projectid }}/releases',
split_lines=False) | from_json)[0].get('tag_name') | replace('v', '') }}"
run_once: true
until: fail2ban_exporter_version is version('0.0.0', '>=')
retries: 10
when:
- fail2ban_exporter_version == "latest"
- fail2ban_exporter_binary_local_dir | length == 0
- not fail2ban_exporter_skip_install
- name: Get fail2ban_exporter binary checksum
when:
- fail2ban_exporter_binary_local_dir | length == 0
- not fail2ban_exporter_skip_install
block:
- name: Get checksum list from gitlab
ansible.builtin.set_fact:
__fail2ban_exporter_checksums: "{{ lookup('url', fail2ban_exporter_checksums_url, wantlist=True) | list }}"
run_once: true
until: __fail2ban_exporter_checksums is search('linux_' + go_arch + '.tar.gz')
retries: 10
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__fail2ban_exporter_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __fail2ban_exporter_checksums }}"
when:
- "item.endswith('fail2ban_exporter_' + fail2ban_exporter_version + '_linux_' + go_arch + '.tar.gz')"

View file

@ -0,0 +1,23 @@
---
- name: Install selinux python packages [RedHat]
ansible.builtin.package:
name: "{{ ['libselinux-python', 'policycoreutils-python']
if ansible_python_version is version('3', '<') else
['python3-libselinux', 'python3-policycoreutils'] }}"
state: present
register: _install_selinux_packages
until: _install_selinux_packages is success
retries: 5
delay: 2
when: ansible_os_family | lower == "redhat"
- name: Install selinux python packages [clearlinux]
ansible.builtin.package:
name: sysadmin-basic
state: present
register: _install_selinux_packages
until: _install_selinux_packages is success
retries: 5
delay: 2
when:
- ansible_distribution | lower == "clearlinux"

View file

@ -0,0 +1,40 @@
{{ ansible_managed | comment }}
[Unit]
Description=Prometheus fail2ban_exporter
After=network-online.target
[Service]
Type=simple
User=root
Group=root
ExecStart={{ fail2ban_exporter_binary_install_dir }}/fail2ban_exporter \
--web.listen-address={{ fail2ban_exporter_web_listen_address }} \
{% if fail2ban_exporter_username | length > 0 -%}
--web.basic-auth.username={{ fail2ban_exporter_username }}
{% endif %}
{% if fail2ban_exporter_password | length > 0 -%}
--web.basic-auth.password={{ fail2ban_exporter_password }}
{% endif %}
--collector.f2b.socket={{ fail2ban_exporter_socket }}
SyslogIdentifier=fail2ban_exporter
Restart=always
RestartSec=1
StartLimitInterval=0
ProtectHome=yes
NoNewPrivileges=yes
{% if (ansible_facts.packages.systemd | first).version is version('232', '>=') %}
ProtectSystem=strict
ProtectControlGroups=true
ProtectKernelModules=true
ProtectKernelTunables=yes
{% else %}
ProtectSystem=full
{% endif %}
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1 @@
bcrypt

View file

@ -0,0 +1,10 @@
---
go_arch_map:
i386: '386'
x86_64: 'amd64'
aarch64: 'arm64'
armv7l: 'armv7'
armv6l: 'armv6'
go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}"
_gitlab_projectid: 24199687

View file

@ -0,0 +1,4 @@
#!/usr/bin/env bash
collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+")
source "$collection_root/tests/integration/molecule.sh"

View file

@ -0,0 +1,4 @@
#!/usr/bin/env bash
collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+")
source "$collection_root/tests/integration/molecule.sh"

View file

@ -0,0 +1,4 @@
#!/usr/bin/env bash
collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+")
source "$collection_root/tests/integration/molecule.sh"