Skip to content

Commit 4d85e11

Browse files
committed
init project
0 parents  commit 4d85e11

File tree

14 files changed

+316
-0
lines changed

14 files changed

+316
-0
lines changed

defaults/main.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
redis_exporter_version: 1.3.5
3+
redis_exporter_binary_local_dir: ""
4+
redis_exporter_web_listen_address: "0.0.0.0:9121"
5+
redis_exporter_web_telemetry_path: "/metrics"
6+
7+
redis_exporter_system_group: "redis-exp"
8+
redis_exporter_system_user: "{{ redis_exporter_system_group }}"
9+
10+
redis_exporter_redis_address: "redis://localhost:6379"
11+
redis_exporter_redis_password: ""
12+
redis_exporter_redis_namespace: "redis"
13+
14+
redis_exporter_custom_option: "-redis-only-metrics"

handlers/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- name: restart redis_exporter
3+
become: true
4+
systemd:
5+
daemon_reload: true
6+
name: redis_exporter
7+
state: restarted

meta/main.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
galaxy_info:
2+
author: Anurak Jannawan
3+
description: ansible for install redis exporter
4+
company: Opsta Thailand Co.,ltd.
5+
license: MIT
6+
min_ansible_version: 2.9
7+
# platforms:
8+
# - name: Fedora
9+
# versions:
10+
# - all
11+
# - 25
12+
# - name: SomePlatform
13+
# versions:
14+
# - all
15+
# - 1.0
16+
# - 7
17+
# - 99.99
18+
galaxy_tags: []
19+
dependencies: []

tasks/configure.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
- name: Copy the Redis Exporter systemd service file
3+
template:
4+
src: redis_exporter.service.j2
5+
dest: /etc/systemd/system/redis_exporter.service
6+
owner: root
7+
group: root
8+
mode: 0644
9+
notify: restart redis_exporter
10+
11+
- name: Allow Redis Exporter port in SELinux on Redhat OS family
12+
seport:
13+
parts: "{{ redis_exporter_web_listen_addres.split(':')[-1] }}"
14+
proto: tcp
15+
setype: http_port_t
16+
state: present
17+
when:
18+
- ansible_version.full is version_compare('2.4', '>=')
19+
- ansible_selinux.status == "enabled"

tasks/install.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
- name: Install dependencies
3+
package:
4+
name: "{{ item }}"
5+
state: present
6+
register: _install_dep_package is success
7+
until: _install_dep_package is success
8+
retries: 5
9+
delay: 2
10+
with_items: "{{ redis_exporter_dependencies }}"
11+
12+
- name: Create the redis_exporter group
13+
group:
14+
name: "{{ redis_exporter_system_group }}"
15+
state: present
16+
system: true
17+
when: redis_exporter_system_group != "root"
18+
19+
- name: Create the redis_exporter user
20+
user:
21+
name: "{{ redis_exporter_system_user }}"
22+
groups: "{{ redis_exporter_system_group }}"
23+
append: true
24+
shell: /usr/bin/nologin
25+
system: true
26+
create_home: false
27+
home: /
28+
when: redis_exporter_system_user != "root"
29+
30+
- block:
31+
- name: Download redis_exporter binary to local folder
32+
become: false
33+
get_url:
34+
url: "https://github.com/oliver006/redis_exporter/releases/download/v{{ redis_exporter_version }}/redis_exporter-v{{ redis_exporter_version }}.linux-{{ go_arch }}.tar.gz"
35+
dest: "/tmp/redis_exporter-v{{ redis_exporter_version }}.linux-{{ go_arch }}.tar.gz"
36+
checksum: "sha256:{{ redis_exporter_checksum }}"
37+
register: _download_binary
38+
until: _download_binary is success
39+
retries: 5
40+
delay: 2
41+
delegate_to: localhost
42+
check_mode: false
43+
44+
- name: Unpack redis_exporter binary
45+
become: false
46+
unarchive:
47+
src: "/tmp/redis_exporter-v{{ redis_exporter_version }}.linux-{{ go_arch }}.tar.gz"
48+
dest: "/tmp"
49+
creates: "/tmp/redis_exporter-v{{ redis_exporter_version }}.linux-{{ go_arch }}/redis_exporter"
50+
delegate_to: localhost
51+
check_mode: false
52+
53+
- name: Propagate redis_exporter binaries
54+
copy:
55+
src: "/tmp/redis_exporter-v{{ redis_exporter_version }}.linux-{{ go_arch }}/redis_exporter"
56+
dest: "/usr/local/bin/redis_exporter"
57+
mode: 0755
58+
owner: root
59+
group: root
60+
notify: restart redis_exporter
61+
when: not ansible_check_mode
62+
when: redis_exporter_binary_local_dir | length == 0
63+
64+
- name: propagate locally distributed redis_exporter binary
65+
copy:
66+
src: "{{ redis_exporter_binary_local_dir }}/redis_exporter"
67+
dest: "/usr/local/bin/redis_exporter"
68+
mode: 0755
69+
owner: root
70+
group: root
71+
when: redis_exporter_binary_local_dir | length > 0
72+
notify: restart redis_exporter

tasks/main.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
- name: Gather variables for each operating system
3+
include_vars: "{{ item }}"
4+
with_first_found:
5+
- "{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml"
6+
- "{{ ansible_distribution | lower }}-{{ ansible_distribution_majar_version | lower }}.yml"
7+
- "{{ ansible_os_family | lower }}-{{ ansible_distribution_majar_version | lower }}.yml"
8+
- "{{ ansible_distribution_file_variety | lower }}.yml"
9+
- "{{ ansible_distribution | lower }}.yml"
10+
- "{{ ansible_os_family | lower }}.yml"
11+
tags:
12+
- redis_exporter_install
13+
- redis_exporter_configure
14+
- redis_exporter_run
15+
16+
- import_tasks: preflight.yml
17+
tags:
18+
- redis_exporter_install
19+
- redis_exporter_configure
20+
- redis_exporter_run
21+
22+
- import_tasks: install.yml
23+
become: true
24+
when: (not __redis_exporter_is_installed.stat.exists) or (__redis_exporter_current_version_output.stderr_lined[0].split(" ")[2] != redis_exporter_version)
25+
tags:
26+
- redis_exporter_install
27+
28+
- import_tasks: configure.yml
29+
become: true
30+
tags:
31+
- redis_exporter_configure
32+
33+
- name: Ensure Redis Exporter is enabled on boot
34+
become: true
35+
systemd:
36+
daemon_reload: true
37+
name: redis_exporter
38+
enabled: true
39+
state: started
40+
tags:
41+
- redis_exporter_run
42+

tasks/preflight.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
- name: Assert usage of systemd as a init system
3+
assert:
4+
that: ansible_service_mgr == 'systemd'
5+
msg: "This role only works with systemd"
6+
7+
- name: Get systemd version
8+
command: systemctl --version
9+
changed_when: false
10+
check_mode: false
11+
register: __systemd_version
12+
13+
- name: Set systemd version fact
14+
set_fact:
15+
redis_exporter_systemd_version: "{{ __systemd_version.stdout_lines[0] | regex_replace('^systemd\\s(\\d+).*$', '\\1') }}"
16+
17+
- name: Naive assertion of proper listen address
18+
assert:
19+
that:
20+
- "':' in redis_exporter_web_listen_address"
21+
- "'redis:' in redis_exporter_redis_address"
22+
23+
- name: Check if redis_exporter is installed
24+
stat:
25+
path: "/usr/local/bin/redis_exporter"
26+
register: __redis_exporter_is_installed
27+
check_mode: false
28+
tags:
29+
- redis_exporter_install
30+
31+
- name: Gather currently installed redis_exporter version (if any)
32+
command: "/usr/local/bin/redis_exporter --version"
33+
args:
34+
warn: false
35+
changed_when: false
36+
register: __redis_exporter_current_version_output
37+
check_mode: false
38+
when: __redis_exporter_is_installed.stat.exists
39+
tags:
40+
- redis_exporter_install
41+
42+
- block:
43+
- name: Get latest release
44+
uri:
45+
url: "https://api.github.com/repos/oliver006/redis_exporter/releases/latest"
46+
method: GET
47+
return_content: true
48+
status_code: 200
49+
body_format: json
50+
validate_certs: false
51+
user: "{{ lookup('env', 'GH_USER') | default(omit) }}"
52+
password: "{{ lookup('env', 'GH_TOKEN') | default(omit) }}"
53+
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
54+
register: _latest_release.status == 200
55+
retries: 5
56+
57+
- name: "Set redis_exporter version to {{ _latest_release.json.tag_name[1:] }}"
58+
set_fact:
59+
redis_exporter_version: "{{ _latest_release.json.tag_name[1:] }}"
60+
when:
61+
- redis_exporter_version == "lastest"
62+
- node_exporter_binary_local_dir | length == 0
63+
delegate_to: localhost
64+
run_once: true
65+
66+
- block:
67+
- name: Get checksum list from github
68+
set_fact:
69+
_checksums: "{{ lookup('url', 'https://github.com/oliver006/redis_exporter/releases/download/v' + redis_exporter_version + '/sha256sums.txt', wantlist=True) | list }}"
70+
run_once: true
71+
72+
- name: "Get checksum for {{ go_arch }} architecture"
73+
set_fact:
74+
redis_exporter_checksum: "{{ item.split(' ')[0] }}"
75+
with_items: "{{ _checksums }}"
76+
when:
77+
- "('linux-' + go_arch + '.tar.gz') in item"
78+
when: redis_exporter_binary_local_dir | length == 0
79+
80+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{{ ansible_managed | comment }}
2+
3+
[Unit]
4+
Description=Prometheus Node Exporter
5+
After=network-online.target
6+
StartLimitInterval=0
7+
8+
[Service]
9+
Type=simple
10+
User={{ redis_exporter_system_user }}
11+
Group={{ redis_exporter_system_group }}
12+
ExecStart=/usr/local/bin/redis_exporter \
13+
{% if redis_exporter_redis_password | length > 0 %}
14+
-redis.password "{{ redis_exporter_redis_password }}" \
15+
{% endif %}
16+
-redis.addr {{ redis_exporter_redis_address }} \
17+
{% if redis_exporter_custom_option | length > 0 %}
18+
{{ redis_exporter_custom_option }} \
19+
{% endif %}
20+
-namespace {{ redis_exporter_redis_namespace }} \
21+
-web.listen-address {{ redis_exporter_web_listen_address }} \
22+
-web.telemetry-path {{ redis_exporter_web_telemetry_path }}
23+
24+
SyslogIdentifier=redis_exporter
25+
Restart=always
26+
RestartSec=1
27+
28+
{% if redis_exporter_systemd_version | int >= 232 %}
29+
ProtectSystem=strict
30+
ProtectControlGroups=true
31+
ProtectKernelModules=true
32+
ProtectKernelTunables=yes
33+
{% else %}
34+
ProtectSystem=full
35+
{% endif %}
36+
37+
[Install]
38+
WantedBy=multi-user.target

vars/debian.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
redis_exporter_dependencies: []

vars/fedora.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
redis_exporter_dependencies:
3+
- python3-libselinux
4+
- python3-policycoreutils

0 commit comments

Comments
 (0)