Skip to content

Commit

Permalink
check installed node_exporter version instead of always downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
Grotax committed Mar 26, 2024
1 parent 7ab7d32 commit 302a3e7
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 61 deletions.
1 change: 1 addition & 0 deletions roles/batman/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- name: Install basic packages
ansible.builtin.apt:
name: dkms

- name: Check for currently installed batman-adv
ansible.builtin.script: files/get_batman_version.py
args:
Expand Down
20 changes: 20 additions & 0 deletions roles/prometheus/files/get_node_exporter_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#! python3

import subprocess
import sys
import re

# compile regex for installed node_exporter version
pattern = re.compile(r"node_exporter, version (\d\.\d\.\d)")

# check current node_exporter
node_exporter = subprocess.check_output(["/opt/node_exporter/node_exporter", "--version"]).decode("utf-8")

# use regex to find installed version
result = pattern.match(node_exporter)

if result:
# print only the version, without newline otherwise ansible will store the newline
print(result[1], end="")
else:
print("not_installed", end="")
64 changes: 64 additions & 0 deletions roles/prometheus/tasks/download.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
- name: Detected Architecture
ansible.builtin.debug:
var: go_arch

- name: Download node_exporter
ansible.builtin.get_url:
url: >
https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz

Check failure on line 8 in roles/prometheus/tasks/download.yml

View workflow job for this annotation

GitHub Actions / build

yaml[line-length]

Line too long (165 > 160 characters)
dest: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz
mode: '0644'
owner: root
group: root

- name: Unpack node_exporter
ansible.builtin.unarchive:
src: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz
dest: /opt
remote_src: true
mode: '0644'
owner: root
group: root
when: not ansible_check_mode

- name: Delete node_exporter archive
ansible.builtin.file:
path: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz
state: absent

- name: Stop node_exporter service
ansible.builtin.systemd:
daemon-reload: true
enabled: true
name: node_exporter
state: stopped

- name: Move node exporter to target directory
ansible.builtin.copy:
remote_src: true
src: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}/
dest: /opt/node_exporter/
force: true
mode: '0744'
owner: prometheus
group: prometheus

- name: Delete node_exporter directory
ansible.builtin.file:
path: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}/
state: absent

- name: Adjust node_exporter permissions
ansible.builtin.file:
path: /opt/node_exporter/node_exporter
owner: prometheus
group: prometheus
mode: '0744'
state: file

- name: Start node_exporter service
ansible.builtin.systemd:
daemon-reload: true
enabled: true
name: node_exporter
state: restarted
72 changes: 11 additions & 61 deletions roles/prometheus/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,17 @@
system: true
create_home: false

- name: Detected Architecture
ansible.builtin.debug:
var: go_arch

- name: Download node_exporter
ansible.builtin.get_url:
url: >
https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz
dest: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz
mode: '0644'
owner: root
group: root

- name: Unpack node_exporter
ansible.builtin.unarchive:
src: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz
dest: /opt
remote_src: true
mode: '0644'
owner: root
group: root
when: not ansible_check_mode

- name: Delete node_exporter archive
ansible.builtin.file:
path: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz
state: absent

- name: Stop node_exporter service
ansible.builtin.systemd:
daemon-reload: true
enabled: true
name: node_exporter
state: stopped
- name: Check for currently installed node_exporter
ansible.builtin.script: files/get_node_exporter_version.py
args:
executable: python3
register: installed_node_exporter_version

- name: Move node exporter to target directory
ansible.builtin.copy:
remote_src: true
src: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}/
dest: /opt/node_exporter/
force: true
mode: '0744'
owner: prometheus
group: prometheus

- name: Delete node_exporter directory
ansible.builtin.file:
path: /opt/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}/
state: absent
- name: Print node_exporter version and target
ansible.builtin.debug:
msg: "Currently installed node_exporter: {{ installed_node_exporter_version.stdout }} ,target is: {{ node_exporter_version }}"

- name: Adjust node_exporter permissions
ansible.builtin.file:
path: /opt/node_exporter/node_exporter
owner: prometheus
group: prometheus
mode: '0744'
state: file
- name: Check if node_exporter-adv needs to be installed or upgraded
ansible.builtin.include_tasks: download.yml
when: installed_node_exporter_version.stdout != node_exporter_version

Check failure on line 28 in roles/prometheus/tasks/main.yml

View workflow job for this annotation

GitHub Actions / build

yaml[empty-lines]

Too many blank lines (1 > 0)
- name: Start node_exporter service
ansible.builtin.systemd:
daemon-reload: true
enabled: true
name: node_exporter
state: restarted

0 comments on commit 302a3e7

Please sign in to comment.