refactor(redis_exporter): delegate common tasks to _common role

Signed-off-by: gardar <gardar@users.noreply.github.com>
This commit is contained in:
gardar 2024-10-15 17:12:07 +00:00
parent e5005614b2
commit a6700b7726
No known key found for this signature in database
GPG key ID: 75FAE37CBA8C13C2
10 changed files with 66 additions and 237 deletions

View file

@ -1,10 +1,8 @@
---
redis_exporter_version: 1.63.0
redis_exporter_binary_local_dir: ""
redis_exporter_binary_url: "https://github.com/{{ _redis_exporter_repo }}/releases/download/v{{ redis_exporter_version }}/\
redis_exporter-v{{ redis_exporter_version }}.linux-{{ go_arch }}.tar.gz"
redis_exporter-v{{ redis_exporter_version }}.{{ ansible_system | lower }}-{{ _redis_exporter_go_ansible_arch }}.tar.gz"
redis_exporter_checksums_url: "https://github.com/{{ _redis_exporter_repo }}/releases/download/v{{ redis_exporter_version }}/sha256sums.txt"
redis_exporter_skip_install: false
# https://github.com/oliver006/redis_exporter?tab=readme-ov-file#command-line-flags
redis_exporter_addr: "redis://localhost:6379"
@ -49,6 +47,7 @@ redis_exporter_config_command: "CONFIG"
redis_exporter_binary_install_dir: "/usr/local/bin"
redis_exporter_system_group: "redis-exp"
redis_exporter_system_user: "{{ redis_exporter_system_group }}"
redis_exporter_config_dir: "/etc/redis_exporter"
# Local path to stash the archive and its extraction
redis_exporter_archive_path: /tmp
redis_exporter_local_cache_path: "/tmp/redis_exporter-{{ ansible_system | lower }}-{{ _redis_exporter_go_ansible_arch }}/{{ redis_exporter_version }}"

View file

@ -11,18 +11,9 @@ argument_specs:
redis_exporter_version:
description: "redis_exporter package version. Also accepts latest as parameter."
default: "1.63.0"
redis_exporter_skip_install:
description: "redis_exporter installation tasks gets skipped when set to true."
type: bool
default: false
redis_exporter_binary_local_dir:
description:
- "Enables the use of local packages instead of those distributed on github."
- "The parameter may be set to a directory where the C(redis_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(redis_exporter_version) parameter"
redis_exporter_binary_url:
description: "URL of the redis_exporter binaries .tar.gz file"
default: "https://github.com/{{ _redis_exporter_repo }}/releases/download/v{{ redis_exporter_version }}/redis_exporter-v{{ redis_exporter_version }}.linux-{{ go_arch }}.tar.gz"
default: "https://github.com/{{ _redis_exporter_repo }}/releases/download/v{{ redis_exporter_version }}/redis_exporter-v{{ redis_exporter_version }}.{{ ansible_system | lower }}-{{ _redis_exporter_go_ansible_arch }}.tar.gz"
redis_exporter_checksums_url:
description: "URL of the redis_exporter checksums file"
default: "https://github.com/{{ _redis_exporter_repo }}/releases/download/v{{ redis_exporter_version }}/sha256sums.txt"
@ -179,6 +170,9 @@ argument_specs:
- "I(Advanced)"
- "redis_exporter user"
default: "redis-exp"
redis_exporter_archive_path:
description: 'Local path to stash the archive and its extraction'
default: "/tmp"
redis_exporter_local_cache_path:
description: "Local path to stash the archive and its extraction"
default: "/tmp/redis_exporter-{{ ansible_system | lower }}-{{ _redis_exporter_go_ansible_arch }}/{{ redis_exporter_version }}"
redis_exporter_config_dir:
description: "Path to directory with redis_exporter configuration"
default: "/etc/redis_exporter"

View file

@ -5,15 +5,13 @@ provisioner:
inventory:
group_vars:
all:
redis_exporter_binary_local_dir: "/tmp/redis_exporter-linux-amd64"
redis_exporter_local_cache_path: "/tmp/redis_exporter-linux-amd64"
redis_exporter_web_listen_address: "127.0.0.1:8080"
redis_exporter_tls_server_config:
cert_file: /etc/redis_exporter/tls.cert
key_file: /etc/redis_exporter/tls.key
redis_exporter_tls_server_cert_file: "{{ redis_exporter_tls_server_config.cert_file }}"
redis_exporter_tls_server_key_file: "{{ redis_exporter_tls_server_config.key_file }}"
go_arch: amd64
redis_exporter_version: 1.58.0
redis_exporter_binary_url: "https://github.com/oliver006/redis_exporter/releases/download/v{{\
\ redis_exporter_version }}/redis_exporter-v{{ redis_exporter_version }}.linux-{{\
\ go_arch }}.tar.gz"
\ redis_exporter_version }}/redis_exporter-v{{ redis_exporter_version }}.linux-amd64.tar.gz"

View file

@ -31,7 +31,6 @@ def test_user(host):
assert host.group("redis-exp").exists
assert "redis-exp" in host.user("redis-exp").groups
assert host.user("redis-exp").shell == "/usr/sbin/nologin"
assert host.user("redis-exp").home == "/"
def test_service(host):

View file

@ -1,37 +1,28 @@
---
- name: Copy the redis_exporter systemd service file
ansible.builtin.template:
src: redis_exporter.service.j2
dest: /etc/systemd/system/redis_exporter.service
owner: root
group: root
mode: 0644
notify: restart redis_exporter
- name: Create redis_exporter config directory
ansible.builtin.file:
path: "/etc/redis_exporter"
state: directory
owner: root
group: root
mode: u+rwX,g+rwX,o=rX
- name: Configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: configure.yml
vars:
_common_system_user: "{{ redis_exporter_system_user }}"
_common_system_group: "{{ redis_exporter_system_group }}"
_common_config_dir: "{{ redis_exporter_config_dir }}"
tags:
- redis_exporter
- configure
- redis_exporter_configure
- name: Copy the passwords file
ansible.builtin.copy:
dest: /etc/redis_exporter/passwords.json
content: "{{ redis_exporter_passwords | to_json(indent=2, sort_keys=True) }}"
owner: root
group: root
dest: "{{ redis_exporter_config_dir }}/passwords.json"
owner: "{{ redis_exporter_system_user }}"
group: "{{ redis_exporter_system_group }}"
mode: 0644
notify: restart redis_exporter
when: redis_exporter_passwords | length > 0
- name: Allow redis_exporter port in SELinux on RedHat OS family
community.general.seport:
ports: "{{ redis_exporter_web_listen_address.split(':')[-1] }}"
proto: tcp
setype: http_port_t
state: present
when:
- ansible_version.full is version_compare('2.4', '>=')
- ansible_selinux.status == "enabled"
become: true
tags:
- redis_exporter
- configure
- redis_exporter_configure

View file

@ -1,69 +0,0 @@
---
- name: Create the redis_exporter group
ansible.builtin.group:
name: "{{ redis_exporter_system_group }}"
state: present
system: true
when: redis_exporter_system_group != "root"
- name: Create the redis_exporter user
ansible.builtin.user:
name: "{{ redis_exporter_system_user }}"
groups: "{{ redis_exporter_system_group }}"
append: true
shell: /usr/sbin/nologin
system: true
create_home: false
home: /
when: redis_exporter_system_user != "root"
- name: Get binary
when:
- redis_exporter_binary_local_dir | length == 0
- not redis_exporter_skip_install
block:
- name: Download redis_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ redis_exporter_binary_url }}"
dest: "{{ redis_exporter_archive_path }}/redis_exporter-{{ redis_exporter_version }}.linux-{{ go_arch }}.tar.gz"
checksum: "sha256:{{ __redis_exporter_checksum }}"
mode: '0644'
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
- name: Unpack redis_exporter binary
become: false
ansible.builtin.unarchive:
src: "{{ redis_exporter_archive_path }}/redis_exporter-{{ redis_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "{{ redis_exporter_archive_path }}"
creates: "{{ redis_exporter_archive_path }}/redis_exporter-{{ redis_exporter_version }}.linux-{{ go_arch }}/redis_exporter"
delegate_to: localhost
check_mode: false
- name: Propagate redis_exporter binaries
ansible.builtin.copy:
src: "{{ redis_exporter_archive_path }}/redis_exporter-v{{ redis_exporter_version }}.linux-{{ go_arch }}/redis_exporter"
dest: "{{ redis_exporter_binary_install_dir }}/redis_exporter"
mode: 0755
owner: root
group: root
notify: restart redis_exporter
when: not ansible_check_mode
- name: Propagate locally distributed redis_exporter binary
ansible.builtin.copy:
src: "{{ redis_exporter_binary_local_dir }}/redis_exporter"
dest: "{{ redis_exporter_binary_install_dir }}/redis_exporter"
mode: 0755
owner: root
group: root
when:
- redis_exporter_binary_local_dir | length > 0
- not redis_exporter_skip_install
notify: restart redis_exporter

View file

@ -2,40 +2,34 @@
- name: Preflight
ansible.builtin.include_tasks:
file: preflight.yml
apply:
tags:
- redis_exporter_install
- redis_exporter_configure
- redis_exporter_run
tags:
- redis_exporter_install
- redis_exporter_configure
- redis_exporter_run
- name: Install
ansible.builtin.include_tasks:
file: install.yml
apply:
become: true
tags:
- redis_exporter_install
when:
( not __redis_exporter_is_installed.stat.exists ) or
( (__redis_exporter_current_version_output.stderr_lines | length > 0)
and (__redis_exporter_current_version_output.stderr_lines[0].split(" ")[2] != redis_exporter_version) ) or
( (__redis_exporter_current_version_output.stdout_lines | length > 0)
and (__redis_exporter_current_version_output.stdout_lines[0].split(" ")[2] != redis_exporter_version) ) or
( redis_exporter_binary_local_dir | length > 0 )
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: install.yml
vars:
_common_local_cache_path: "{{ redis_exporter_local_cache_path }}"
_common_binaries: "{{ _redis_exporter_binaries }}"
_common_binary_install_dir: "{{ redis_exporter_binary_install_dir }}"
_common_binary_url: "{{ redis_exporter_binary_url }}"
_common_checksums_url: "{{ redis_exporter_checksums_url }}"
_common_system_group: "{{ redis_exporter_system_group }}"
_common_system_user: "{{ redis_exporter_system_user }}"
_common_config_dir: "{{ redis_exporter_config_dir }}"
_common_binary_unarchive_opts: ['--strip-components=1']
tags:
- redis_exporter_install
- name: SELinux
ansible.builtin.include_tasks:
file: selinux.yml
apply:
become: true
tags:
- redis_exporter_configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: selinux.yml
vars:
_common_selinux_port: "{{ redis_exporter_web_listen_address | urlsplit('port') }}"
when: ansible_selinux.status == "enabled"
tags:
- redis_exporter_configure
@ -43,10 +37,6 @@
- name: Configure
ansible.builtin.include_tasks:
file: configure.yml
apply:
become: true
tags:
- redis_exporter_configure
tags:
- redis_exporter_configure

View file

@ -1,24 +1,8 @@
---
- name: Assert usage of systemd as an init system
ansible.builtin.assert:
that: ansible_service_mgr == 'systemd'
msg: "This role only works with systemd"
- name: Install package fact dependencies
become: true
ansible.builtin.package:
name: "{{ _pkg_fact_req }}"
state: present
when: (_pkg_fact_req)
vars:
_pkg_fact_req: "{% if (ansible_pkg_mgr == 'apt') %}\
{{ ('python-apt' if ansible_python_version is version('3', '<') else 'python3-apt') }}
{% else %}\
{% endif %}"
- name: Gather package facts
ansible.builtin.package_facts:
when: "not 'packages' in ansible_facts"
- name: Common preflight
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: preflight.yml
- name: Assert that used version supports listen address type
ansible.builtin.assert:
@ -63,23 +47,6 @@
- "__redis_exporter_cert_file.stat.exists"
- "__redis_exporter_key_file.stat.exists"
- name: Check if redis_exporter is installed
ansible.builtin.stat:
path: "{{ redis_exporter_binary_install_dir }}/redis_exporter"
register: __redis_exporter_is_installed
check_mode: false
tags:
- redis_exporter_install
- name: Gather currently installed redis_exporter version (if any)
ansible.builtin.command: "{{ redis_exporter_binary_install_dir }}/redis_exporter --version"
changed_when: false
register: __redis_exporter_current_version_output
check_mode: false
when: __redis_exporter_is_installed.stat.exists
tags:
- redis_exporter_install
- name: Discover latest version
ansible.builtin.set_fact:
redis_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _redis_exporter_repo }}/releases/latest', headers=_github_api_headers,
@ -89,24 +56,9 @@
retries: 10
when:
- redis_exporter_version == "latest"
- redis_exporter_binary_local_dir | length == 0
- not redis_exporter_skip_install
- name: Get redis_exporter binary checksum
when:
- redis_exporter_binary_local_dir | length == 0
- not redis_exporter_skip_install
block:
- name: Get checksum list from github
ansible.builtin.set_fact:
__redis_exporter_checksums: "{{ lookup('url', redis_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}"
run_once: true
until: __redis_exporter_checksums is search('linux-' + go_arch + '.tar.gz')
retries: 10
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__redis_exporter_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __redis_exporter_checksums }}"
when:
- "('linux-' + go_arch + '.tar.gz') in item"
tags:
- redis_exporter
- install
- redis_exporter_install
- download
- redis_exporter_download

View file

@ -1,23 +0,0 @@
---
- name: Install selinux python packages [RedHat]
ansible.builtin.package:
name: "{{ ['libselinux-python', 'policycoreutils-python']
if ansible_python_version is version('3', '<') else
['python3-libselinux', 'python3-policycoreutils'] }}"
state: present
register: _install_selinux_packages
until: _install_selinux_packages is success
retries: 5
delay: 2
when: ansible_os_family | lower == "redhat"
- name: Install selinux python packages [clearlinux]
ansible.builtin.package:
name: sysadmin-basic
state: present
register: _install_selinux_packages
until: _install_selinux_packages is success
retries: 5
delay: 2
when:
- ansible_distribution | lower == "clearlinux"

View file

@ -1,11 +1,9 @@
---
go_arch_map:
i386: '386'
x86_64: 'amd64'
aarch64: 'arm64'
armv7l: 'armv7'
armv6l: 'armv6'
go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}"
_redis_exporter_go_ansible_arch: "{{ {'i386': '386',
'x86_64': 'amd64',
'aarch64': 'arm64',
'armv7l': 'armv7',
'armv6l': 'armv6'}.get(ansible_architecture, ansible_architecture) }}"
_redis_exporter_repo: "oliver006/redis_exporter"
_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}"
_redis_exporter_binaries: ['redis_exporter']