pez-infra/ansible/roles/node_exporter/tasks/main.yml
Rasmus "Pez" Wejlgaard 853386ce2f
fix: remove custom node_exporter, standardise on package version (#40)
london-b had both a custom node_exporter.service and the
package-managed prometheus-node-exporter.service installed.
Both tried to bind port 9100, causing the package version to fail.

- Add cleanup tasks to remove custom /etc/systemd/system/node_exporter.service
  and /usr/local/bin/node_exporter if present
- Add node_exporter_extra_collectors variable for configurable collectors
- Configure london-b with systemd/processes/sysctl/ethtool/zfs collectors
  matching its previous custom setup

Resolves PESO-109
2026-04-03 01:50:13 +01:00

101 lines
3.6 KiB
YAML

---
# Install node_exporter for Prometheus monitoring.
# Uses system packages on Linux, pkg on FreeBSD.
# Optionally binds to Tailscale IP on public-facing hosts.
# ── Cleanup old custom installs ──────────────────────────────
- name: Stop and disable custom node_exporter service if present
ansible.builtin.service:
name: node_exporter
state: stopped
enabled: false
failed_when: false
when: ansible_facts["os_family"] == "Debian"
- name: Remove custom node_exporter service file
ansible.builtin.file:
path: /etc/systemd/system/node_exporter.service
state: absent
when: ansible_facts["os_family"] == "Debian"
notify: Reload systemd
- name: Remove custom node_exporter binary
ansible.builtin.file:
path: /usr/local/bin/node_exporter
state: absent
when: ansible_facts["os_family"] == "Debian"
# ── Install ──────────────────────────────────────────────────
- name: Install prometheus-node-exporter (Debian)
ansible.builtin.apt:
name: prometheus-node-exporter
state: present
when: ansible_facts["os_family"] == "Debian"
- name: Install prometheus-node-exporter (Alpine)
community.general.apk:
name: prometheus-node-exporter
state: present
when: ansible_facts["os_family"] == "Alpine"
# ── Configure (Debian) ──────────────────────────────────────
- name: Build ARGS for prometheus-node-exporter
ansible.builtin.set_fact:
_node_exporter_args: >-
{{ (node_exporter_extra_collectors | map('regex_replace', '^(.*)$', '--collector.\1') | list)
+ (['--web.listen-address=' + ansible_host + ':9100'] if node_exporter_bind_tailscale | bool else []) }}
when: ansible_facts["os_family"] == "Debian"
- name: Configure prometheus-node-exporter ARGS (Debian)
ansible.builtin.lineinfile:
path: /etc/default/prometheus-node-exporter
regexp: '^ARGS='
line: 'ARGS="{{ _node_exporter_args | join(" ") }}"'
when:
- ansible_facts["os_family"] == "Debian"
- (_node_exporter_args | length > 0)
notify: Restart node-exporter (Debian)
- name: Enable and start node-exporter (Debian)
ansible.builtin.service:
name: prometheus-node-exporter
state: started
enabled: true
when: ansible_facts["os_family"] == "Debian"
- name: Enable and start node-exporter (Alpine)
ansible.builtin.service:
name: node-exporter
state: started
enabled: true
when: ansible_facts["os_family"] == "Alpine"
# ── FreeBSD ──────────────────────────────────────────────────
- name: Install node_exporter (FreeBSD)
community.general.pkgng:
name: node_exporter
state: present
when: ansible_facts["os_family"] == "FreeBSD"
- name: Enable node_exporter (FreeBSD)
ansible.builtin.lineinfile:
path: /etc/rc.conf
regexp: '^node_exporter_enable='
line: 'node_exporter_enable="YES"'
when: ansible_facts["os_family"] == "FreeBSD"
- name: Configure listen address (FreeBSD)
ansible.builtin.lineinfile:
path: /etc/rc.conf
regexp: '^node_exporter_listen_address='
line: 'node_exporter_listen_address="{{ ansible_host }}:9100"'
when:
- ansible_facts["os_family"] == "FreeBSD"
- node_exporter_bind_tailscale | bool
notify: Restart node_exporter (FreeBSD)
- name: Start node_exporter (FreeBSD)
ansible.builtin.service:
name: node_exporter
state: started
when: ansible_facts["os_family"] == "FreeBSD"