Files
ansible/roles/docker/tasks/infrastructure/crowdsec.yml
T
PhilandClaude Opus 5.5 41e9428412 crowdsec: host sshd + Gitea logs, nftables firewall bouncer (SSH bans only for now), weekly hub update
- crowdsec image -> latest-debian (journalctl); reads host journal (sshd)
  and Gitea's log file (LePresidente/gitea); LAPI on 127.0.0.1:8080
- gitea also logs to /data/gitea/log/gitea.log (router lines console only)
- host: crowdsec-firewall-bouncer-nftables from packagecloud any/any, own
  nft tables on input+forward hooks; limited to local ssh detections and
  manual bans until crowdsec_firewall_all_decisions
- weekly cscli hub update/upgrade + SIGHUP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 13:26:00 -07:00

86 lines
2.7 KiB
YAML

# CrowdSec next to Caddy. Unban: docker exec crowdsec cscli decisions delete --ip <ip>
- name: make crowdsec directories
ansible.builtin.file:
path: "{{ item }}"
state: directory
loop:
- /opt/stacks/crowdsec
- /opt/stacks/crowdsec/acquis.d
- name: Template out the crowdsec compose file
ansible.builtin.template:
src: crowdsec-compose.yml.j2
dest: /opt/stacks/crowdsec/compose.yml
owner: root
mode: '0644'
- name: Template out the crowdsec .env file
ansible.builtin.template:
src: crowdsec-env.j2
dest: /opt/stacks/crowdsec/.env
owner: root
mode: '0640'
group: docker
diff: false # contains the bouncer key
- name: Template out the crowdsec acquisition files
ansible.builtin.template:
src: "crowdsec-acquis-{{ item }}.yaml.j2"
dest: "/opt/stacks/crowdsec/acquis.d/{{ item }}.yaml"
owner: root
mode: '0644'
loop: [caddy, appsec, sshd, gitea]
notify: restart crowdsec
- name: deploy crowdsec stack
community.docker.docker_compose_v2:
project_src: /opt/stacks/crowdsec
files:
- compose.yml
- name: wait for the crowdsec local API
ansible.builtin.command: docker exec crowdsec cscli lapi status
register: crowdsec_lapi
retries: 20
delay: 3
until: crowdsec_lapi.rc == 0
changed_when: false
when: not ansible_check_mode
# Our own networks are never banned (centralized allowlist, CrowdSec >= 1.6.8)
- name: read the crowdsec allowlist
ansible.builtin.command: docker exec crowdsec cscli allowlists inspect trusted -o json
register: crowdsec_allowlist_current
failed_when: false
changed_when: false
when: not ansible_check_mode
- name: create the crowdsec allowlist
ansible.builtin.command: docker exec crowdsec cscli allowlists create trusted -d "own networks (ansible)"
when: not ansible_check_mode and crowdsec_allowlist_current.rc != 0
- name: add missing entries to the crowdsec allowlist
ansible.builtin.command: >-
docker exec crowdsec cscli allowlists add trusted {{ item }} -d ansible
loop: "{{ crowdsec_allowlist }}"
when: >-
not ansible_check_mode and
(crowdsec_allowlist_current.rc != 0 or item not in crowdsec_allowlist_current.stdout)
# The entrypoint only updates hub items (parsers, scenarios, vpatch rules) on
# start; dockcheck restarts crowdsec only when a new image ships.
- name: weekly crowdsec hub update
ansible.builtin.cron:
name: crowdsec hub upgrade
weekday: "1"
hour: "4"
minute: "17"
user: root
job: >-
docker exec crowdsec cscli hub update >/dev/null &&
docker exec crowdsec cscli hub upgrade >/dev/null &&
docker kill -s HUP crowdsec >/dev/null
- name: firewall bouncer on the host
ansible.builtin.import_tasks: crowdsec-firewall-bouncer.yml