iac/ansible/roles/monitoring_agent/tasks/main.yaml
Sergei Poljanski 56dd0255dc
All checks were successful
deploy / ansible (push) Successful in 8m12s
monitoring: watch peers, devices, accounts and logins
Monitoring covered availability only: a new WireGuard peer, a machine
on the private network, an extra account or key, or a login from
outside the VPN would have gone unnoticed.

security-metrics runs every minute on both hosts and exports what it
finds for node_exporter: WireGuard peers and where they connect from,
addresses answering a ping sweep of the VPC, login accounts, sudo and
docker members, listening ports, containers with host-level access,
and fingerprints of the account, sudoers, sshd, WireGuard and
authorized_keys files. Each item is compared with a baseline in the
role defaults and host_vars and exported with known="true|false", so a
deliberate change has to update the baseline with it. On ops it also
counts Forgejo users, keys and runners.

Grafana gets a Security dashboard and an alert group for anything
unknown, a changed access file, an SSH login from outside WireGuard,
failed-login bursts on SSH, Forgejo, Grafana and the asxp.io admin, a
new container, and for logs or the collector itself going quiet, which
would otherwise blind the rest.

It never reads `wg show dump`, which prints preshared keys.

Assisted-by: Claude:fable-5-2
2026-11-15 21:59:16 +04:00

55 lines
1.7 KiB
YAML

---
# node_exporter, cAdvisor and Alloy on every host. Metrics listen on the
# host's VPC address only; Alloy ships Docker logs and the journal to Loki
# on asxpio-ops over the VPC.
- name: Create the node_exporter textfile directory
ansible.builtin.file:
path: "{{ monitoring_textfile_dir }}"
state: directory
mode: "0755"
- name: Deploy the monitoring agents
ansible.builtin.include_role:
name: docker_service
vars:
service_name: monitoring-agent
service_base_path: "{{ services_base_path }}/monitoring-agent"
service_templates:
- { src: docker-compose.yml.j2, dest: docker-compose.yml }
- { src: config.alloy.j2, dest: config.alloy }
health_check_enabled: true
health_check_host: "{{ vpc_ip }}"
health_check_port: "{{ monitoring_node_exporter_port }}"
# Security state (WireGuard peers, private network, users, keys, ports,
# containers) as metrics; the baseline is in defaults and host_vars.
- name: Install security-metrics
ansible.builtin.copy:
src: security-metrics.py
dest: /usr/local/sbin/security-metrics
mode: "0700"
- name: Write the security baseline
ansible.builtin.template:
src: security-metrics.json.j2
dest: /etc/security-metrics.json
mode: "0600"
- name: Install the security-metrics units
ansible.builtin.copy:
src: "{{ item }}"
dest: "/etc/systemd/system/{{ item }}"
mode: "0644"
loop: [security-metrics.service, security-metrics.timer]
notify: Reload systemd
- name: Flush handlers
ansible.builtin.meta: flush_handlers
- name: Enable the security-metrics timer
ansible.builtin.systemd:
name: security-metrics.timer
enabled: true
state: started
# A dry run has not installed the unit yet
ignore_errors: "{{ ansible_check_mode }}"