ansible/roles/prometheus-node-exporter/tasks/main.yml

128 lines
4 KiB
YAML
Raw Permalink Normal View History

---
#- name: Use a newer version of Node exporter for ubuntu 20.04
# block:
# - name: Set the default release
# lineinfile:
# path: /etc/apt/apt.conf.d/01-vendor-ubuntu
# regexp: '^APT::Default-Release '
# line: "APT::Default-Release \"{{ ansible_facts['lsb']['codename'] }}\";"
# - name: Pin node exporter
# copy:
# dest: /etc/apt/preferences.d/pin-prometheus-node-exporter
# content: |
# Package: prometheus-node-exporter
# Pin: release n={{ ansible_facts['lsb']['codename'] }}
# Pin-Priority: -10
# Package: prometheus-node-exporter
# Pin: release n=groovy
# Pin-Priority: 900
# - name: Add the repo from groovy
# apt_repository:
# repo: deb http://fr.archive.ubuntu.com/ubuntu groovy universe
# state: present
# when: ansible_facts['lsb']['id'] == 'Ubuntu' and ansible_facts['lsb']['codename'] == 'focal'
- name: Install Prometheus Node exporter
apt:
name:
- prometheus-node-exporter
- prometheus-node-exporter-collectors
state: latest
update_cache: true
2021-09-06 01:18:10 +02:00
install_recommends: false # Do not install smartmontools
2021-10-11 13:22:09 +02:00
- name: Install the local_x509 exporter
import_tasks: local_x509_collector.yml
2021-09-21 15:50:15 +02:00
- name: Ensure /etc/node_exporter exist
file:
path: /etc/node_exporter
state: directory
group: prometheus
owner: prometheus
mode: u=rwx,g=rx,o=rx
- name: Copy the config folder
template:
src: config.yaml
dest: /etc/node_exporter/config.yaml
group: prometheus
owner: prometheus
mode: u=rw,g=r,o=r
notify: Restart prometheus-node-exporter
- name: Copy the CA cert
copy:
content: "{{ ca_cert }}"
dest: /etc/node_exporter/ca.crt
notify: Restart prometheus-node-exporter
2021-09-21 16:16:02 +02:00
- name: Generate certificate
include_role:
name: generate-cert
vars:
directory: /etc/node_exporter/
cname: "node-exp-{{ lan_address }}"
owner: prometheus
group: prometheus
key_mode: u=rw,g=,o=
subject_alt_name: "IP:{{ lan_address }}"
# Need an equivalent to notify here
2021-09-21 16:16:02 +02:00
- name: Ensured the certificate is monitored
import_tasks: register-cert-to-monitoring.yml
vars:
target: "{{ lan_address }}:9100|node-exp-{{ lan_address }}|{{ ansible_facts['nodename'] }}"
2021-09-21 16:16:02 +02:00
2021-09-06 00:24:56 +02:00
- name: Setup the arguments for node-exporter
template:
src: prometheus-node-exporter
dest: /etc/default/prometheus-node-exporter
owner: root
group: root
2021-09-21 15:50:15 +02:00
mode: u=rw,g=r,o=r
2021-09-06 00:24:56 +02:00
notify: Restart prometheus-node-exporter
vars:
args:
- name: web.listen-address
value: "{{ lan_address }}:9100"
2021-09-21 16:16:02 +02:00
- name: web.config
value: /etc/node_exporter/config.yaml
2021-09-21 14:53:25 +02:00
- name: Add the node to the server targets
block:
- name: Get the list of targets of the server
slurp:
src: /etc/prometheus/targets/node-targets.json
register: server_node_target_file
delegate_to: "{{ appointed_prometheus_server }}"
- name: Set target variable
set_fact:
server_node_target: "{{ server_node_target_file['content'] | b64decode | from_json }}"
- name: Register the node to the prometheus server
block:
- name: Add the node to the targets
set_fact:
new_server_node_target: "[{{ server_node_target[0] | combine({'targets': [lan_address + '|' + ansible_facts['nodename']]}, list_merge='append_rp') }}]"
- name: Put the new target list
copy:
content: "{{ new_server_node_target | to_nice_json }}"
2022-07-22 13:47:50 +02:00
dest: /etc/prometheus/targets/node-targets.json
delegate_to: "{{ appointed_prometheus_server }}"
when: (lan_address + '|' + ansible_facts['nodename']) not in server_node_target.0.targets
2021-10-08 22:20:08 +02:00
- name: Add alert rules for node on the prometheus server
copy:
src: alerts-node.yml
dest: /etc/prometheus/alerts/node.yml
owner: prometheus
group: prometheus
mode: u=rw,g=r,o=r
delegate_to: "{{ appointed_prometheus_server }}"
notify: Restart appointed_prometheus_server