From 2b293e9835db9fe5e9e10a74c757ee350191a147 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 26 Oct 2026 10:23:28 +0000 Subject: [PATCH] crowdsec: disabled_scenarios removes hub scenarios replaced by local ones, absent_files removes retired local files --- crowdsec/defaults/main.yml | 4 ++++ crowdsec/tasks/main.yml | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/crowdsec/defaults/main.yml b/crowdsec/defaults/main.yml index 82d7278..09de33e 100644 --- a/crowdsec/defaults/main.yml +++ b/crowdsec/defaults/main.yml @@ -12,6 +12,10 @@ crowdsec: scenarios: {} # path below /etc/crowdsec -> content, for local parsers and scenarios. local_files: {} + # name -> installed hub scenario removed from the agent (null drops one); needed where a local scenario replaces it. + disabled_scenarios: {} + # name -> path below /etc/crowdsec of a retired local file, removed when present (null drops one). + absent_files: {} # name -> CIDR (single addresses as /32 or /128) never acted upon. Joined by # the public_ips (same format) of every inventory host, then whitelisted by # this agent and, when lapi_allowlist is set, allowlisted on the LAPI. diff --git a/crowdsec/tasks/main.yml b/crowdsec/tasks/main.yml index 3e834b2..20eaa98 100644 --- a/crowdsec/tasks/main.yml +++ b/crowdsec/tasks/main.yml @@ -171,6 +171,24 @@ when: crowdsec_missing | length > 0 notify: Restart crowdsec +# Read again: the collections installed above bring their scenarios along. +- name: List the installed hub scenarios + ansible.builtin.command: cscli hub list -o json + register: crowdsec_scenarios_json + changed_when: false + failed_when: crowdsec_scenarios_json.rc != 0 and not ansible_check_mode + check_mode: false + +# --force also removes items that belong to an installed collection. A plain "cscli hub upgrade" keeps them removed; +# "cscli hub upgrade --force" would bring them back. +- name: Remove the hub scenarios replaced by local ones + ansible.builtin.command: "cscli scenarios remove {{ item.value }} --force" + loop: "{{ crowdsec.disabled_scenarios | dict2items | rejectattr('value', 'none') }}" + loop_control: + label: "{{ item.value }}" + when: item.value in ((crowdsec_scenarios_json.stdout | default('{}', true) | from_json).scenarios | default([], true) | map(attribute='name')) + notify: Restart crowdsec + - name: Install the local parsers and scenarios ansible.builtin.copy: dest: "/etc/crowdsec/{{ item.key }}" @@ -181,6 +199,15 @@ label: "{{ item.key }}" notify: Restart crowdsec +- name: Remove retired local files + ansible.builtin.file: + path: "/etc/crowdsec/{{ item.value }}" + state: absent + loop: "{{ crowdsec.absent_files | dict2items | rejectattr('value', 'none') }}" + loop_control: + label: "{{ item.value }}" + notify: Restart crowdsec + - name: Enable the agent and the bouncer ansible.builtin.systemd: name: "{{ item }}"