Compare commits

..

30 Commits

Author SHA1 Message Date
ded4fb8270 chore 📦: Update file path for non-clustered environments in fail2ban.yml
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 15s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 5s
ai-reviews / Review PR (pull_request) Successful in 12s
PR check / Gitleaks (pull_request) Successful in 4s
PR check / lint tests (pull_request) Successful in 15s
PR check / labeler (pull_request) Successful in 3s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 2s
This commit updates the file path configuration for non-clustered environments in the `fail2ban.yml` file. This ensures that the correct paths are used when running fail2ban outside of a clustered environment.
2026-03-02 19:02:18 +01:00
38831f981a refactor ♻️: Check for firewall file existence before debugging
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 12s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 5s
ai-reviews / Review PR (pull_request) Successful in 11s
PR check / Gitleaks (pull_request) Successful in 4s
PR check / lint tests (pull_request) Successful in 15s
PR check / labeler (pull_request) Successful in 2s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 2s
Refactored the code to include a conditional check for the existence of the firewall file before proceeding with debugging. This ensures that the debugging process is only initiated when necessary, preventing unnecessary operations and potential errors.
2026-03-01 20:03:52 +01:00
d2761bd840 refactor ♻️: Refactor task names and improve formatting in fail2ban.yml
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 12s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 8s
ai-reviews / Review PR (pull_request) Successful in 13s
PR check / Gitleaks (pull_request) Successful in 5s
PR check / lint tests (pull_request) Successful in 16s
PR check / labeler (pull_request) Successful in 2s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 1s
This commit refactors the task names for better readability and consistency. Additionally, it improves the formatting of the YAML file to enhance maintainability.
2026-03-01 12:59:08 +01:00
c8fb6e4c80 feat : Add debug tasks for Proxmox firewall config path and contents
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 13s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 5s
ai-reviews / Review PR (pull_request) Successful in 15s
PR check / Gitleaks (pull_request) Successful in 5s
PR check / lint tests (pull_request) Failing after 17s
PR check / labeler (pull_request) Successful in 2s
PR check / handle_failures (pull_request) Successful in 1s
PR check / handle_success (pull_request) Has been skipped
This commit introduces new debug tasks to help diagnose issues related to the Proxmox firewall configuration path and its contents. These tasks will assist in verifying that the paths are correctly set up and that the necessary files are present.
2026-03-01 12:56:54 +01:00
54f3f761c8 chore 📦: Remove commented-out code in fail2ban.yml
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 14s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 5s
ai-reviews / Review PR (pull_request) Successful in 12s
PR check / Gitleaks (pull_request) Successful in 5s
PR check / lint tests (pull_request) Successful in 16s
PR check / labeler (pull_request) Successful in 2s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 2s
This commit removes unnecessary commented-out code from the fail2ban.yml file to clean up and simplify the configuration.
2026-03-01 12:05:48 +01:00
3054a97d15 chore 📦: Update build scripts for CI/CD pipeline
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 12s
Gitleaks Scan / gitleaks (push) Successful in 6s
Markdown Lint / markdown-lint (push) Successful in 6s
ai-reviews / Review PR (pull_request) Successful in 13s
PR check / Gitleaks (pull_request) Successful in 5s
PR check / lint tests (pull_request) Successful in 15s
PR check / labeler (pull_request) Successful in 3s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 1s
Refactored the build scripts to improve compatibility with the latest version of the CI/CD tooling and added new tests for edge cases.
2026-03-01 12:00:48 +01:00
752db2b57f style 💎: Fix table formatting and add code block for list_banned task
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 5s
ai-reviews / Review PR (pull_request) Successful in 37s
PR check / Gitleaks (pull_request) Successful in 5s
PR check / lint tests (pull_request) Successful in 15s
PR check / labeler (pull_request) Successful in 2s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 1s
This commit addresses the formatting issues in the table and adds a code block for the 'list_banned' task to improve readability and clarity.
2026-03-01 10:52:57 +01:00
0004d2bd2d refactor ♻️: Refactor task names and update module references in fail2ban.yml
Some checks failed
ansible-lint / Ansible Lint (push) Successful in 12s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Failing after 6s
This commit refactors the task names and updates module references in the `fail2ban.yml` file to improve clarity and consistency. It also includes minor text adjustments in `meta/fail2ban.md` to enhance readability and ensure accurate variable descriptions with updated default values.
2026-03-01 10:48:13 +01:00
e44f757b9e refactor ♻️: Refactor and reformat text for better readability
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 17s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Failing after 5s
This commit refactors the code by improving text formatting and structure to enhance readability, ensuring that the content and functionality remain unchanged.
2026-03-01 10:31:36 +01:00
94bcbbac5b docs 📝: Add Fail2Ban deployment and configuration documentation for Proxmox VE
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 8s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Failing after 7s
This commit adds a new file `meta/fail2ban.md` containing detailed documentation on how to deploy and configure Fail2Ban on Proxmox VE, including integration with the Proxmox firewall. The documentation aims to provide comprehensive guidance for users looking to enhance their server security by implementing Fail2Ban.
2026-03-01 10:23:11 +01:00
bc91383094 chore 📦: Update Fail2ban settings and add/restart/reload tasks
This commit updates the Fail2ban configuration to reduce bantime and findtime, and adds/fixes restart/reload tasks in handlers/main.yml. These changes aim to improve security and manageability of the fail2ban service.
2026-03-01 10:22:58 +01:00
7c304765a4 refactor ♻️: Refactor fail2ban.yml for Proxmox cluster detection and configuration
This commit refactors the fail2ban.yml file to include support for detecting a Proxmox cluster, ensuring that pmxcfs is mounted, installing Fail2Ban, and configuring appropriate jails. This enhances the security and management of the Proxmox environment by automating the setup and monitoring of failed login attempts.
2026-03-01 10:22:41 +01:00
9e90c67fe8 refactor ♻️: Refactor fail2ban.yml to use fw_compile_check.rc for failure checks
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 5s
This refactoring updates the fail2ban configuration to utilize a new script, `fw_compile_check.rc`, for handling failure checks. This change aims to streamline the process and improve reliability by centralizing the logic in a dedicated script.
2026-02-25 17:59:13 +01:00
734a9c031f style 💎: Fix indentation and variable registration in fail2ban.yml
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 5s
Refactored the YAML file to improve readability and ensure proper variable registration.
2026-02-25 17:52:11 +01:00
46cac8fc90 fix 🐛: Fix typo in firewall path variable
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 5s
ai-reviews / Review PR (pull_request) Successful in 12s
PR check / Gitleaks (pull_request) Successful in 4s
PR check / lint tests (pull_request) Successful in 16s
PR check / labeler (pull_request) Successful in 2s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 1s
Corrected a typographical error in the firewall path variable to ensure proper configuration and functionality.
2026-02-25 17:39:13 +01:00
fcbe78f3e5 feat : Add task for importing fail2ban configuration
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 5s
ai-reviews / Review PR (pull_request) Successful in 12s
PR check / Gitleaks (pull_request) Successful in 4s
PR check / lint tests (pull_request) Successful in 15s
PR check / labeler (pull_request) Successful in 3s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 2s
This commit introduces a new task that facilitates the import of fail2ban configuration files into the system. This enhancement allows for automated and consistent management of security settings, improving the overall security posture.
2026-02-25 17:33:39 +01:00
13b3a5066d refactor ♻️: Refactor task to extract and process Corosync ring addresses, determine their CIDRs, and update ignoreip in fail2ban config
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 5s
ai-reviews / Review PR (pull_request) Successful in 32s
PR check / Gitleaks (pull_request) Successful in 5s
PR check / lint tests (pull_request) Successful in 16s
PR check / labeler (pull_request) Successful in 3s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 1s
This refactoring extracts the logic for processing Corosync ring addresses and determining their CIDRs. It then updates the `ignoreip` setting in the fail2ban configuration accordingly. This change improves modularity and maintainability of the code.
2026-02-24 19:18:48 +01:00
79e14e7120 feat : Add IPSET registration and conditional validation for Proxmox firewall configuration
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 5s
This commit introduces new features to register IPSETs and update firewall reload notifications. It also conditionally validates Proxmox firewall configurations based on changes, enhancing the robustness of the system.
2026-02-24 19:02:24 +01:00
4107a3a953 refactor ♻️: Rename task 'Validate Proxmox firewall configuration' to 'fail2ban | Validate Proxmox firewall configuration'
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 5s
Refactored the task name to include 'fail2ban' for clarity and consistency with other similar tasks.
2026-02-24 18:57:44 +01:00
8a3f359f46 style 💎: Remove unnecessary blank line from fail2ban.yml
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 12s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 6s
This commit removes an unnecessary blank line from the `fail2ban.yml` file to clean up the code style and improve readability.
2026-02-24 18:56:47 +01:00
674f014be3 feat : Add conditional execution for reloading PVE firewall
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 7s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 5s
This commit introduces a new feature that allows conditional execution of the PVE firewall reload command based on certain conditions, enhancing flexibility and control over firewall management.
2026-02-24 18:56:02 +01:00
432ec97292 chore 📦: Update build scripts for CI/CD pipeline
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 6s
Updated the build scripts to ensure compatibility with the latest version of the CI/CD tooling and improved the deployment process.
2026-02-24 18:47:08 +01:00
80b3b82bf6 refactor ♻️: Refactor fail2ban tasks for better IPSet management
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 6s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 5s
This refactoring removes redundant 'blockinfile' and 'reload' commands in fail2ban tasks, ensuring that IPSet and drop rules are correctly placed. A new handler has been added to reload the PVE firewall after a fail2ban restart.
2026-02-24 18:46:12 +01:00
a120b1042b chore 📦: Add comment to disable risky file permissions check
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 13s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 5s
This commit adds a comment in the codebase to disable a file permissions check that was deemed too risky. This change aims to simplify the build process while ensuring that we are aware of the potential security implications.
2026-02-24 18:25:03 +01:00
e26b3f01f8 chore 📦: Remove redundant directory creation and update shell command for robustness
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 13s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 6s
This commit removes unnecessary directory creation steps in the build process, simplifying the setup. Additionally, it updates the shell command to be more robust and reliable.
2026-02-24 18:21:50 +01:00
8d40abc15b style 💎: Remove trailing whitespace from f2b_unban_ip comment
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 18s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 5s
Cleaned up the code by removing unnecessary trailing whitespace from a comment in the defaults/main.yml file.
2026-02-23 19:37:50 +01:00
d3527c14e4 docs 📝: Add Fail2Ban integration tasks to README and directory structure.
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 6s
Gitleaks Scan / gitleaks (push) Successful in 4s
Markdown Lint / markdown-lint (push) Successful in 6s
Updated the README with instructions on integrating Fail2Ban and modified the directory structure to accommodate new files related to this integration.
2026-02-23 19:36:36 +01:00
3afa853d09 feat : Add new variable f2b_unban_ip for specifying an IP to unban during playbook execution.
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 6s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 5s
Introduce a new variable `f2b_unban_ip` in the Ansible playbook to allow users to specify an IP address that should be unbanned using Fail2Ban. This feature enhances the flexibility of the playbook by enabling targeted IP management.
2026-02-23 18:35:10 +01:00
4fa35ca62d feat : Add Fail2ban integration with Proxmox Firewall
Some checks failed
ansible-lint / Ansible Lint (push) Failing after 7s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 7s
This commit adds Fail2ban settings to `defaults/main.yml`, a new task to restart the fail2ban service, and a task file for deploying Fail2Ban integrated with Proxmox Firewall. The new tasks include checks, validations, and configuration to enhance security by blocking malicious IP addresses.
2026-02-23 18:30:01 +01:00
eb548e8476 feat : Add new task to stop log2ram before extracting it
All checks were successful
ansible-lint / Ansible Lint (push) Successful in 12s
Gitleaks Scan / gitleaks (push) Successful in 5s
Markdown Lint / markdown-lint (push) Successful in 6s
ai-reviews / Review PR (pull_request) Successful in 14s
PR check / Gitleaks (pull_request) Successful in 4s
PR check / lint tests (pull_request) Successful in 15s
PR check / labeler (pull_request) Successful in 2s
PR check / handle_failures (pull_request) Has been skipped
PR check / handle_success (pull_request) Successful in 2s
Introduce a new step in the extraction process to ensure that log2ram is stopped before proceeding with the extraction, preventing potential data corruption or loss.
2026-02-16 20:52:32 +01:00
7 changed files with 476 additions and 0 deletions

View File

@@ -27,6 +27,7 @@
| Logrotate protection | ✅ | ✅ | ✅ |
| Powertop auto-tune | ✅ | ✅ | ✅ |
| Utilities | ✅ | ✅ | ✅ |
| Fail2Ban Integration | ✅ | ✅ | ✅ |
## 📂 Directory Structure
@@ -40,6 +41,7 @@ ansible_role_proxmox_provision/
├── meta/ # Role metadata
│ └── main.yml
├── tasks/ # Main role tasks
│ ├── fail2ban.yml # Fail2Ban integration tasks
│ ├── logrotate.yml # logrotate setup
│ ├── main.yml # Core tasks
│ ├── powertop.yml # powertop setup
@@ -69,6 +71,7 @@ proxmox_enable_powertop: true
## Logrotate
proxmox_logrotate_maxsize: "100M"
proxmox_logrotate_rotate: 7
...
```
## Example usage

View File

@@ -47,3 +47,16 @@ journald_runtime_max_use: "100M"
vm_dirty_ratio: 15
vm_dirty_background_ratio: 5
vm_swappiness: "{{ proxmox_swapiness }}"
# Fail2ban settings
f2b_bantime: 600 # 10 minutes
f2b_findtime: 1200 # 20 minutes
f2b_maxretry: 5
f2b_recidive_bantime: 3600 # 1 hours
f2b_recidive_findtime: 86400 # 24 hours
f2b_recidive_maxretry: 3
f2b_ipset_name: f2b-blacklist
f2b_bantime_increment: true
f2b_bantime_factor: 2
f2b_bantime_max: 86400
f2b_unban_ip: "" # ansible-playbook play.yml -e f2b_unban_ip=192.168.1.55

View File

@@ -31,3 +31,21 @@
- name: Reload systemd
ansible.builtin.systemd:
daemon_reload: true
- name: Reload fail2ban
ansible.builtin.systemd:
name: fail2ban
state: reloaded
enabled: true
- name: Restart fail2ban
ansible.builtin.systemd:
name: fail2ban
state: restarted
enabled: true
- name: Reload pve firewall
ansible.builtin.command: pve-firewall reload
when: fw_compile_check.rc == 0
changed_when: false

110
meta/fail2ban.md Normal file
View File

@@ -0,0 +1,110 @@
# Fail2Ban Integration with Proxmox Firewall
This Ansible playbook deploys and configures **Fail2Ban** on a Proxmox VE
environment, integrating it with the **Proxmox firewall** for cluster-aware
IP banning. It supports both single-node and clustered Proxmox setups.
---
## Features
- Detects Proxmox VE installation.
- Checks cluster filesystem (`pmxcfs`) and quorum before modifying firewall.
- Detects cluster membership via `corosync.conf`.
- Installs and configures Fail2Ban with:
- SSH protection
- Proxmox GUI / AD login protection
- Progressive ban escalation (recidive jail)
- Deploys a **cluster-aware Fail2Ban action** (`proxmox-fw`) for Proxmox
firewall integration.
- Ensures safe firewall updates without affecting Corosync ports (5404/5405).
- Supports single-node Fail2Ban using `iptables-multiport`.
- Enables and starts the Fail2Ban service.
- Provides tasks to list or manually unban IPs in the cluster.
---
## Requirements
- **Proxmox VE** (any supported version)
- **Ansible** ≥ 2.9
- Root or sudo access on target nodes
- Proxmox firewall enabled for cluster-wide banning (optional, but recommended)
---
## Variables
The playbook uses the following variables (can be defined in a `vars` file or
inventory group vars):
| Variable | Description | Default |
|-------------------------|---------------------------------|-----------------|
| `f2b_bantime` | Ban per tentativi falliti | `600s` |
| `f2b_findtime` | Finestra per contare fallimenti | `1200s` |
| `f2b_maxretry` | Tentativi prima del ban | `5` |
| `f2b_bantime_increment` | Abilita ban incrementale | `true` |
| `f2b_bantime_factor` | Fattore aumento ban | `2` |
| `f2b_bantime_max` | Durata massima del ban | `7d` |
| `f2b_recidive_bantime` | Ban per recidiva | `3600` |
| `f2b_recidive_findtime` | Finestra recidiva | `86400` |
| `f2b_recidive_maxretry` | Tentativi recidiva | `3` |
| `f2b_ipset_name` | Nome IPSet per IP bannati | `f2b-blacklist` |
| `f2b_unban_ip` | IP da sbloccare | `""` |
> All `clustered` and `pmxcfs_running` checks default to `false` to prevent
> errors on non-clustered or single-node setups.
---
## Usage
### 1. Apply the playbook
```bash
ansible-playbook -i inventory fail2ban-proxmox.yml
```
### 2. List current banned IPs
```bash
ansible-playbook \
-i inventory \
fail2ban-proxmox.yml \
-e "f2b_ipset_name=fail2ban" \
-t list_banned
```
### 3. Unban a specific IP
```bash
ansible-playbook -i inventory fail2ban-proxmox.yml -e "f2b_unban_ip=1.2.3.4"
```
## How It Works
- Detects Proxmox ensures the playbook runs only on Proxmox VE hosts.
- Cluster safety checks verifies /etc/pve/.members and corosync.conf
for quorum.
- Installs Fail2Ban ensures /etc/fail2ban/jail.local exists and applies
configuration.
- Cluster-aware action for clustered nodes, Fail2Ban bans are added to
Proxmox firewall and compiled immediately (pve-firewall compile).
- Single-node fallback uses iptables-multiport for nodes not in
a cluster.
- Corosync protection prevents firewall rules from dropping cluster
communication ports (5404/5405).
## Notes & Safety
- The playbook does not copy jail.conf, only manages jail.local.
- Firewall rules for clustered nodes are only modified if quorum exists.
- pve-firewall compile is called safely (>/dev/null 2>&1 || true)
to prevent playbook failure on minor compilation warnings.
- Manual unban is supported via f2b_unban_ip variable.
- Always verify that the Proxmox firewall is enabled when using
cluster-wide bans.
## License
MIT License

322
tasks/fail2ban.yml Normal file
View File

@@ -0,0 +1,322 @@
---
# -------------------------------------------------
# Deploy Fail2Ban integrated with Proxmox Firewall
# -------------------------------------------------
#################################################
# Detect Proxmox
#################################################
- name: fail2ban | Detect Proxmox
ansible.builtin.stat:
path: /usr/bin/pveversion
register: pve_installed
#################################################
# Ensure pmxcfs is mounted (cluster filesystem)
#################################################
- name: fail2ban | Check pmxcfs cluster filesystem
ansible.builtin.stat:
path: /etc/pve/.members
register: pmxcfs_running
when: pve_installed.stat.exists | default(false)
- name: fail2ban | Warn if pmxcfs not mounted (no quorum)
ansible.builtin.debug:
msg: >
/etc/pve is not mounted or node has no quorum.
Refusing to modify cluster firewall.
when:
- pve_installed.stat.exists | default(false)
- not pmxcfs_running.stat.exists
#################################################
# Detect cluster membership
#################################################
- name: fail2ban | Detect Proxmox cluster membership
ansible.builtin.stat:
path: /etc/pve/corosync.conf
register: clustered
when: pmxcfs_running.stat.exists | default(false)
- name: fail2ban | Warn if corosync.conf is missing
ansible.builtin.debug:
msg: >
node has no quorum.
Refusing to modify cluster firewall.
when:
- pve_installed.stat.exists | default(false)
- pmxcfs_running.stat.exists | default(false)
- not clustered.stat.exists
#################################################
# Install Fail2Ban
#################################################
- name: fail2ban | Install fail2ban
ansible.builtin.apt:
name: fail2ban
state: present
update_cache: true
#################################################
# Ensure jail.local exists (do NOT copy jail.conf)
#################################################
- name: fail2ban | Ensure jail.local exists
ansible.builtin.file:
path: /etc/fail2ban/jail.local
state: touch
owner: root
group: root
mode: '0644'
#################################################
# Configure Fail2Ban jails
#################################################
- name: fail2ban | Configure Fail2Ban jails
ansible.builtin.blockinfile:
dest: /etc/fail2ban/jail.local
marker: "# {mark} ANSIBLE MANAGED BLOCK - PROXMOX"
block: |
# jail.conf (default)
# jail.local (override defaults)
[DEFAULT]
bantime = {{ f2b_bantime }}
findtime = {{ f2b_findtime }}
maxretry = {{ f2b_maxretry }}
bantime.increment = {{ f2b_bantime_increment }}
bantime.factor = {{ f2b_bantime_factor }}
bantime.maxtime = {{ f2b_bantime_max }}
backend = systemd
banaction = {% if (clustered.stat.exists | default(false)) %} proxmox-fw{% else %} iptables-multiport{% endif %}
ignoreip = 127.0.0.1/8 192.168.2.0/24
#################################################
# SSH
#################################################
[sshd]
enabled = true
#################################################
# Proxmox GUI + AD authentication
#################################################
[proxmox]
enabled = true
port = https,http,8006
filter = proxmox
#################################################
# Progressive escalation (recidive)
#################################################
[recidive]
enabled = true
filter = recidive
logpath = /var/log/fail2ban.log
bantime = {{ f2b_recidive_bantime }}
findtime = {{ f2b_recidive_findtime }}
maxretry = {{ f2b_recidive_maxretry }}
banaction = {% if (clustered.stat.exists | default(false)) %} proxmox-fw{% else %} iptables-multiport{% endif %}
notify:
- Reload fail2ban
- name: fail2ban | Place Proxmox filter definition
ansible.builtin.copy:
dest: /etc/fail2ban/filter.d/proxmox.conf
content: |
[Definition]
failregex = pvedaemon\[.*authentication failure; rhost=<HOST> user=.* msg=.*
ignoreregex =
journalmatch = _SYSTEMD_UNIT=pvedaemon.service
owner: root
group: root
mode: '0644'
notify:
- Reload fail2ban
#################################################
# Determine Correct Firewall File
#################################################
- name: fail2ban | Get Proxmox node name
ansible.builtin.set_fact:
pve_node: "{{ ansible_hostname }}"
when: not clustered.stat.exists
- name: fail2ban | Set firewall config path
ansible.builtin.set_fact:
pve_firewall_config: >-
{{
'/etc/pve/firewall/cluster.fw'
if clustered.stat.exists
else '/etc/pve/nodes/' + pve_node + '/host.fw'
}}
when: pve_installed.stat.exists | default(false)
- name: fail2ban | Show firewall config path
ansible.builtin.debug:
msg: >
WARNING: Proxmox firewall config path is: {{ pve_firewall_config}}
when: pve_firewall_config is defined
#################################################
# Detect firewall configuration
#################################################
- name: fail2ban | Check firewall config exists
ansible.builtin.stat:
path: "{{ pve_firewall_config }}"
register: fw_stat
when: pve_firewall_config is defined
- name: fail2ban | Read firewall config
ansible.builtin.slurp:
src: "{{ pve_firewall_config }}"
register: fw_content
when: fw_stat.stat.exists | default(false)
- name: fail2ban | Debug config contents
ansible.builtin.debug:
msg: >
{{ fw_content }}
when: fw_stat.stat.exists | default(false)
- name: fail2ban | Determine if firewall enabled
ansible.builtin.set_fact:
pve_firewall_enabled: >-
{{
fw_stat.stat.exists and
(fw_content.content | b64decode)
is search('^enable:\s*1$', multiline=True)
}}
- name: fail2ban | Warn if firewall not enabled
ansible.builtin.debug:
msg: >
WARNING: Proxmox firewall is disabled in configuration.
Fail2Ban will not actively block traffic.
when: not pve_firewall_enabled
#################################################
# Validate firewall runtime state
#################################################
- name: fail2ban | Check firewall runtime status
ansible.builtin.command: pve-firewall status
register: pve_fw_status
changed_when: false
failed_when: false
when: pmxcfs_running.stat.exists | default(false)
- name: fail2ban | Abort if firewall daemon not running
ansible.builtin.debug:
msg: >
Proxmox firewall service is not running.
You can run: systemctl enable --now pve-firewall
when:
- pve_fw_status is defined
- pve_fw_status.rc != 0
- fw_stat.stat.exists | default(false)
- pmxcfs_running.stat.exists | default(false)
#################################################
# Corosync safety validation
#################################################
- name: fail2ban | Validate corosync firewall rules
ansible.builtin.command: pve-firewall compile
register: compiled_fw
changed_when: false
failed_when: compiled_fw.rc != 0
when: clustered.stat.exists | default(false)
- name: fail2ban | Fail if corosync ports are being dropped
ansible.builtin.debug:
msg: >
Firewall configuration appears to affect Corosync ports (5404/5405).
Refusing to continue to prevent cluster outage.
when:
- clustered.stat.exists | default(false)
- compiled_fw.stdout is search('5404.*DROP|5405.*DROP')
#################################################
# Deploy cluster-aware Fail2Ban action
#################################################
- name: fail2ban | Deploy proxmox-fw action
ansible.builtin.copy:
dest: /etc/fail2ban/action.d/proxmox-fw.conf
owner: root
group: root
mode: '0644'
content: |
[Definition]
fwfile = {{ pve_firewall_config }}
rule = DROP -source <ip> -log nolog
actionban = \
if [ -f <fwfile> ]; then \
grep -qF "<rule>" <fwfile> || echo "<rule>" >> <fwfile>; \
pve-firewall compile >/dev/null 2>&1 || true; \
fi
actionunban = \
if [ -f <fwfile> ]; then \
sed -i "\|<rule>|d" <fwfile>; \
pve-firewall compile >/dev/null 2>&1 || true; \
fi
actionstart =
actionstop =
when:
- clustered.stat.exists | default(false)
notify:
- Restart fail2ban
#################################################
# Enable services
#################################################
- name: fail2ban | Enable fail2ban
ansible.builtin.systemd:
name: fail2ban
enabled: true
state: started
# #################################################
# # List banned IPs cluster-wide
# #################################################
# - name: fail2ban | Get banned IPs from Proxmox IPSet
# ansible.builtin.command: pve-firewall ipset list {{ f2b_ipset_name }}
# register: banned_ips
# changed_when: false
# failed_when: false
# - name: fail2ban | Show banned IPs
# ansible.builtin.debug:
# msg: >
# Current banned IPs (cluster-wide):
# {{ banned_ips.stdout_lines | default([]) }}
# #################################################
# # Manual unban
# #################################################
# - name: fail2ban | Unban specific IP
# ansible.builtin.command: >
# pve-firewall ipset del {{ f2b_ipset_name }} {{ f2b_unban_ip }}
# when: f2b_unban_ip is defined and f2b_unban_ip | length > 0
# register: unban_result
# changed_when: "'removed' in unban_result.stdout or unban_result.rc == 0"
# failed_when: false
# - name: fail2ban | Report unban result
# ansible.builtin.debug:
# msg: "Unbanned IP {{ f2b_unban_ip }}"
# when: f2b_unban_ip | length > 0

View File

@@ -42,3 +42,6 @@
- name: Configure ram usage
ansible.builtin.import_tasks: ram.yml
- name: SetUp fail2ban
ansible.builtin.import_tasks: fail2ban.yml

View File

@@ -47,6 +47,13 @@
- name: ram | Logic to extract and install log2ram
when: download_archive.changed # noqa: no-handler
block:
- name: ram | Stop log2ram if present
ansible.builtin.systemd:
name: log2ram
state: stopped
become: true
failed_when: false
- name: ram | Extract log2ram
ansible.builtin.unarchive:
src: "/tmp/log2ram.tar.gz"