Skip to content
This repository was archived by the owner on Mar 6, 2023. It is now read-only.

Commit 212db9f

Browse files
authored
import_tasks instead of include; bringing role up to ansible-prometheus standards; minor changes (#48)
[minor] modification to introduce some of good ideas seen in #47 and ansible-prometheus - add ansible_managed info to service file - use `import_tasks` instead of `include` - specify `become: true` on role level not on playbook level - split `main.yml` into smaller files: `install.yml` and `configure.yml` - use one task for installing dependecies - use vars files for distro-specific values - use `node_exporter_system_group` and `node_exporter_system_user` which shouldn't be user-defined (defined in `vars` not in `defaults`), but simplify role management - add tags - add 2 more preflight checks
1 parent 147a35d commit 212db9f

File tree

11 files changed

+172
-142
lines changed

11 files changed

+172
-142
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ All variables which can be overridden are stored in [defaults/main.yml](defaults
3434
Use it in a playbook as follows:
3535
```yaml
3636
- hosts: all
37-
become: yes
3837
roles:
3938
- cloudalchemy.node-exporter
4039
```

handlers/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: restart node exporter
2+
- name: restart node_exporter
33
become: true
44
systemd:
55
daemon_reload: true

meta/main.yml

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@ galaxy_info:
55
license: MIT
66
min_ansible_version: 2.4
77
platforms:
8-
- name: Ubuntu
9-
versions:
10-
- bionic
11-
- xenial
12-
- name: Debian
13-
versions:
14-
- jessie
15-
- stretch
16-
- name: EL
17-
versions:
18-
- 7
19-
- name: Fedora
20-
versions:
21-
- 27
8+
- name: Ubuntu
9+
versions:
10+
- bionic
11+
- xenial
12+
- name: Debian
13+
versions:
14+
- jessie
15+
- stretch
16+
- name: EL
17+
versions:
18+
- 7
19+
- name: Fedora
20+
versions:
21+
- 27
2222
galaxy_tags:
23-
- monitoring
24-
- prometheus
25-
- exporter
26-
- metrics
27-
- system
28-
23+
- monitoring
24+
- prometheus
25+
- exporter
26+
- metrics
27+
- system
2928

3029
dependencies: []

tasks/configure.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
- name: Create texfile collector dir
3+
file:
4+
path: "{{ node_exporter_textfile_dir }}"
5+
state: directory
6+
owner: "{{ node_exporter_system_user }}"
7+
group: "{{ node_exporter_system_group }}"
8+
recurse: true
9+
mode: 0755
10+
when: node_exporter_textfile_dir != ""
11+
12+
- name: Node exporter can read anything (omit file permissions)
13+
capabilities:
14+
path: '/usr/local/bin/node_exporter'
15+
capability: cap_dac_read_search+ep
16+
state: present
17+
when: not ansible_check_mode
18+
19+
- name: Allow Node Exporter port in SELinux on RedHat OS family
20+
seport:
21+
ports: "{{ node_exporter_web_listen_address.split(':')[1] }}"
22+
proto: tcp
23+
setype: http_port_t
24+
state: present
25+
when:
26+
- ansible_version.full is version_compare('2.4', '>=')
27+
- ansible_selinux.status == "enabled"

tasks/install.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
- name: Install dependencies
3+
package:
4+
name: "{{ item }}"
5+
state: present
6+
with_items: "{{ node_exporter_dependencies }}"
7+
8+
- name: Create the node_exporter group
9+
group:
10+
name: "{{ node_exporter_system_group }}"
11+
state: present
12+
system: true
13+
14+
- name: Create the node_exporter user
15+
user:
16+
name: "{{ node_exporter_system_user }}"
17+
groups: "{{ node_exporter_system_group }}"
18+
append: true
19+
shell: /usr/sbin/nologin
20+
system: true
21+
createhome: false
22+
home: /
23+
24+
- name: Download node_exporter binary to local folder
25+
become: false
26+
get_url:
27+
url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
28+
dest: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
29+
checksum: "sha256:{{ node_exporter_checksum }}"
30+
register: _download_binary
31+
until: _download_binary is succeeded
32+
retries: 5
33+
delay: 2
34+
delegate_to: localhost
35+
check_mode: false
36+
37+
- name: Unpack node_exporter binary
38+
become: false
39+
unarchive:
40+
src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
41+
dest: "/tmp"
42+
creates: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/node_exporter"
43+
delegate_to: localhost
44+
check_mode: false
45+
46+
- name: Propagate node_exporter binaries
47+
copy:
48+
src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/node_exporter"
49+
dest: "/usr/local/bin/node_exporter"
50+
mode: 0750
51+
owner: "{{ node_exporter_system_user }}"
52+
group: "{{ node_exporter_system_group }}"
53+
notify: restart node_exporter
54+
when: not ansible_check_mode
55+
56+
- name: Copy the Node Exporter systemd service file
57+
template:
58+
src: node_exporter.service.j2
59+
dest: /etc/systemd/system/node_exporter.service
60+
owner: root
61+
group: root
62+
mode: 0644
63+
notify: restart node_exporter

tasks/main.yml

Lines changed: 24 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,30 @@
11
---
2-
- include: preflight.yml
3-
4-
- name: Create the Node Exporter group
5-
group:
6-
name: "node-exp"
7-
state: present
8-
system: true
9-
10-
- name: Create the Node Exporter user
11-
user:
12-
name: "node-exp"
13-
groups: "node-exp"
14-
append: true
15-
shell: /usr/sbin/nologin
16-
system: true
17-
createhome: false
18-
home: /
19-
20-
- name: Download node_exporter binary to local folder
21-
become: false
22-
get_url:
23-
url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
24-
dest: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
25-
checksum: "sha256:{{ node_exporter_checksum }}"
26-
register: _download_binary
27-
until: _download_binary is succeeded
28-
retries: 5
29-
delay: 2
30-
delegate_to: localhost
31-
check_mode: false
32-
33-
- name: Unpack node_exporter binary
34-
become: false
35-
unarchive:
36-
src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
37-
dest: "/tmp"
38-
creates: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/node_exporter"
39-
delegate_to: localhost
40-
check_mode: false
41-
42-
- name: Propagate Node Exporter binaries
43-
copy:
44-
src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}/node_exporter"
45-
dest: "/usr/local/bin/node_exporter"
46-
mode: 0750
47-
owner: "node-exp"
48-
group: "node-exp"
49-
notify:
50-
- restart node exporter
51-
when: not ansible_check_mode
52-
53-
- name: Create texfile collector dir
54-
file:
55-
path: "{{ node_exporter_textfile_dir }}"
56-
state: directory
57-
owner: "node-exp"
58-
group: "node-exp"
59-
recurse: true
60-
mode: 0755
61-
when: node_exporter_textfile_dir != ""
62-
63-
- name: Install libcap on Debian systems
64-
package:
65-
name: "libcap2-bin"
66-
state: present
67-
when: ansible_os_family | lower == "debian"
68-
69-
- name: Node exporter can read anything (omit file permissions)
70-
capabilities:
71-
path: '/usr/local/bin/node_exporter'
72-
capability: cap_dac_read_search+ep
73-
state: present
74-
when:
75-
not ansible_check_mode
76-
77-
- name: Copy the Node Exporter systemd service file
78-
template:
79-
src: node_exporter.service.j2
80-
dest: /etc/systemd/system/node_exporter.service
81-
owner: root
82-
group: root
83-
mode: 0644
84-
notify:
85-
- restart node exporter
86-
87-
- name: Install dependencies on RedHat OS family
88-
package:
89-
name: "{{ item }}"
90-
state: present
91-
with_items:
92-
- libselinux-python
93-
- policycoreutils-python
94-
when:
95-
- ansible_os_family == "RedHat"
96-
97-
- name: Allow Node Exporter port in SELinux on RedHat OS family
98-
seport:
99-
ports: "{{ node_exporter_web_listen_address.split(':')[1] }}"
100-
proto: tcp
101-
setype: http_port_t
102-
state: present
103-
when:
104-
- ansible_version.full is version_compare('2.4', '>=')
105-
- ansible_selinux.status == "enabled"
2+
- name: Gather variables for each operating system
3+
include_vars: "{{ item }}"
4+
with_first_found:
5+
- "{{ ansible_distribution | lower }}.yml"
6+
- "{{ ansible_os_family | lower }}.yml"
7+
tags:
8+
- always
9+
10+
- import_tasks: preflight.yml
11+
tags:
12+
- always
13+
14+
- import_tasks: install.yml
15+
become: true
16+
tags:
17+
- install
18+
19+
- import_tasks: configure.yml
20+
become: true
21+
tags:
22+
- configure
10623

10724
- name: Ensure Node Exporter is enabled on boot
25+
become: true
10826
systemd:
10927
name: node_exporter
11028
enabled: true
29+
tags:
30+
- run

tasks/preflight.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
---
2-
- name: check collectors
2+
- name: Naive assertion of proper listen address
3+
assert:
4+
that:
5+
- "':' in node_exporter_web_listen_address"
6+
7+
- name: Fail on unsupported init systems
8+
fail:
9+
msg: "This module only works with systemd"
10+
when: ansible_service_mgr != 'systemd'
11+
12+
- name: Check collectors
313
fail:
414
msg: "Collector cannot be both disabled and enabled"
515
when: item in node_exporter_enabled_collectors

templates/node_exporter.service.j2

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
# {{ ansible_managed }}
2+
13
[Unit]
24
Description=Prometheus Node Exporter
35
After=network.target
46

57
[Service]
68
Type=simple
7-
User=node-exp
8-
Group=node-exp
9+
User={{ node_exporter_system_user }}
10+
Group={{ node_exporter_system_group }}
911
Nice=-5
1012
ExecStart=/usr/local/bin/node_exporter \
11-
--web.listen-address {{ node_exporter_web_listen_address }} \
12-
{% for c in node_exporter_enabled_collectors -%}
13-
{% if not c is mapping -%}
14-
--collector.{{ c }} \
15-
{% else -%}
16-
{% set name,opt = (c.items() | list)[0] -%}
17-
{% for k,v in opt.items() -%}
13+
{% for collector in node_exporter_enabled_collectors -%}
14+
{% if not collector is mapping %}
15+
--collector.{{ collector }} \
16+
{% else -%}
17+
{% set name, options = (collector.items()|list)[0] -%}
18+
{% for k,v in options|dictsort %}
1819
--collector.{{ name }}.{{ k }}={{ v }} \
19-
{% endfor -%}
20-
{% endif -%}
21-
{% endfor -%}
22-
{% for c in node_exporter_disabled_collectors -%}
23-
--no-collector.{{ c }} \
24-
{% endfor %}
20+
{% endfor -%}
21+
{% endif -%}
22+
{% endfor -%}
23+
{% for collector in node_exporter_disabled_collectors %}
24+
--no-collector.{{ collector }} \
25+
{% endfor %}
26+
--web.listen-address={{ node_exporter_web_listen_address }}
2527

2628
SyslogIdentifier=node_exporter
2729
Restart=always

vars/debian.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
node_exporter_dependencies:
3+
- libcap2-bin

vars/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ go_arch_map:
55
aarch64: 'arm64'
66
armv7l: 'armv7'
77
armv6l: 'armv6'
8+
9+
node_exporter_system_group: "node-exp"
10+
node_exporter_system_user: "{{ node_exporter_system_group }}"

0 commit comments

Comments
 (0)