refactor(smartctl_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:36 +00:00
parent a6700b7726
commit 0da12f7cdc
No known key found for this signature in database
GPG key ID: 75FAE37CBA8C13C2
12 changed files with 57 additions and 265 deletions

View file

@ -1,10 +1,8 @@
---
smartctl_exporter_version: 0.12.0
smartctl_exporter_binary_local_dir: ""
smartctl_exporter_binary_url: "https://github.com/{{ _smartctl_exporter_repo }}/releases/download/v{{ smartctl_exporter_version }}/\
smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch }}.tar.gz"
smartctl_exporter-{{ smartctl_exporter_version }}.{{ ansible_system | lower }}-{{ _smartctl_exporter_go_ansible_arch }}.tar.gz"
smartctl_exporter_checksums_url: "https://github.com/{{ _smartctl_exporter_repo }}/releases/download/v{{ smartctl_exporter_version }}/sha256sums.txt"
smartctl_exporter_skip_install: false
smartctl_exporter_smartctl_path: "/usr/sbin/smartctl"
smartctl_exporter_smartctl_interval: "60s"
@ -28,6 +26,7 @@ smartctl_exporter_log_format: "logfmt"
smartctl_exporter_binary_install_dir: "/usr/local/bin"
smartctl_exporter_system_group: "smartctl-exp"
smartctl_exporter_system_user: "{{ smartctl_exporter_system_group }}"
smartctl_exporter_config_dir: "/etc/smartctl_exporter"
# Local path to stash the archive and its extraction
smartctl_exporter_archive_path: /tmp
smartctl_exporter_local_cache_path: "/tmp/smartctl_exporter-{{ ansible_system | lower }}-{{ _smartctl_exporter_go_ansible_arch }}/{{ smartctl_exporter_version }}"

View file

@ -11,18 +11,9 @@ argument_specs:
smartctl_exporter_version:
description: "Smartctl exporter package version. Also accepts latest as parameter."
default: "0.12.0"
smartctl_exporter_skip_install:
description: "Smartctl exporter installation tasks gets skipped when set to true."
type: bool
default: false
smartctl_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(smartctl_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(smartctl_exporter_version) parameter"
smartctl_exporter_binary_url:
description: "URL of the Smartctl exporter binaries .tar.gz file"
default: "https://github.com/{{ _smartctl_exporter_repo }}/releases/download/v{{ smartctl_exporter_version }}/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{ go_arch }}.tar.gz"
default: "https://github.com/{{ _smartctl_exporter_repo }}/releases/download/v{{ smartctl_exporter_version }}/smartctl_exporter-{{ smartctl_exporter_version }}.{{ ansible_system | lower }}-{{ _smartctl_exporter_go_ansible_arch }}.tar.gz"
smartctl_exporter_checksums_url:
description: "URL of the Smartctl exporter checksums file"
default: "https://github.com/{{ _smartctl_exporter_repo }}/releases/download/v{{ smartctl_exporter_version }}/sha256sums.txt"
@ -90,6 +81,9 @@ argument_specs:
- "I(Advanced)"
- "Smartctl exporter user"
default: "smartctl-exp"
smartctl_exporter_archive_path:
description: 'Local path to stash the archive and its extraction'
default: "/tmp"
smartctl_exporter_local_cache_path:
description: "Local path to stash the archive and its extraction"
default: "/tmp/smartctl_exporter-{{ ansible_system | lower }}-{{ _smartctl_exporter_go_ansible_arch }}/{{ smartctl_exporter_version }}"
smartctl_exporter_config_dir:
description: "Path to directory with smartctl_exporter configuration"
default: "/etc/smartctl_exporter"

View file

@ -5,7 +5,7 @@ provisioner:
inventory:
group_vars:
all:
smartctl_exporter_binary_local_dir: "/tmp/smartctl_exporter-linux-amd64"
smartctl_exporter_local_cache_path: "/tmp/smartctl_exporter-linux-amd64"
smartctl_exporter_web_listen_address: "127.0.0.1:8080"
smartctl_exporter_smartctl_interval: "30s"
smartctl_exporter_smartctl_rescan: "5m"
@ -18,8 +18,6 @@ provisioner:
http2: true
smartctl_exporter_basic_auth_users:
randomuser: examplepassword
go_arch: amd64
smartctl_exporter_version: 0.11.0
smartctl_exporter_binary_url: "https://github.com/prometheus-community/smartctl_exporter/releases/download/v{{\
\ smartctl_exporter_version }}/smartctl_exporter-{{ smartctl_exporter_version }}.linux-{{\
\ go_arch }}.tar.gz"
\ smartctl_exporter_version }}/smartctl_exporter-{{ smartctl_exporter_version }}.linux-amd64.tar.gz"

View file

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

View file

@ -1,36 +0,0 @@
---
- name: Copy the smartctl_exporter systemd service file
ansible.builtin.template:
src: smartctl_exporter.service.j2
dest: /etc/systemd/system/smartctl_exporter.service
owner: root
group: root
mode: 0644
notify: restart smartctl_exporter
- name: Create smartctl_exporter config directory
ansible.builtin.file:
path: "/etc/smartctl_exporter"
state: directory
owner: root
group: root
mode: u+rwX,g+rwX,o=rX
- name: Copy the smartctl_exporter web config file
ansible.builtin.template:
src: web_config.yaml.j2
dest: /etc/smartctl_exporter/web_config.yaml
owner: root
group: root
mode: 0644
notify: restart smartctl_exporter
- name: Allow smartctl_exporter port in SELinux on RedHat OS family
community.general.seport:
ports: "{{ smartctl_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"

View file

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

View file

@ -2,51 +2,49 @@
- name: Preflight
ansible.builtin.include_tasks:
file: preflight.yml
apply:
tags:
- smartctl_exporter_install
- smartctl_exporter_configure
- smartctl_exporter_run
tags:
- smartctl_exporter_install
- smartctl_exporter_configure
- smartctl_exporter_run
- name: Install
ansible.builtin.include_tasks:
file: install.yml
apply:
become: true
tags:
- smartctl_exporter_install
when:
( not __smartctl_exporter_is_installed.stat.exists ) or
( (__smartctl_exporter_current_version_output.stderr_lines | length > 0)
and (__smartctl_exporter_current_version_output.stderr_lines[0].split(" ")[2] != smartctl_exporter_version) ) or
( (__smartctl_exporter_current_version_output.stdout_lines | length > 0)
and (__smartctl_exporter_current_version_output.stdout_lines[0].split(" ")[2] != smartctl_exporter_version) ) or
( smartctl_exporter_binary_local_dir | length > 0 )
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: install.yml
vars:
_common_local_cache_path: "{{ smartctl_exporter_local_cache_path }}"
_common_binaries: "{{ _smartctl_exporter_binaries }}"
_common_binary_install_dir: "{{ smartctl_exporter_binary_install_dir }}"
_common_binary_url: "{{ smartctl_exporter_binary_url }}"
_common_checksums_url: "{{ smartctl_exporter_checksums_url }}"
_common_system_group: "{{ smartctl_exporter_system_group }}"
_common_system_user: "{{ smartctl_exporter_system_user }}"
_common_config_dir: "{{ smartctl_exporter_config_dir }}"
_common_binary_unarchive_opts: ['--strip-components=1']
tags:
- smartctl_exporter_install
- name: SELinux
ansible.builtin.include_tasks:
file: selinux.yml
apply:
become: true
tags:
- smartctl_exporter_configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: selinux.yml
vars:
_common_selinux_port: "{{ smartctl_exporter_web_listen_address | urlsplit('port') }}"
when: ansible_selinux.status == "enabled"
tags:
- smartctl_exporter_configure
- name: Configure
ansible.builtin.include_tasks:
file: configure.yml
apply:
become: true
tags:
- smartctl_exporter_configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: configure.yml
vars:
_common_system_user: "{{ smartctl_exporter_system_user }}"
_common_system_group: "{{ smartctl_exporter_system_group }}"
_common_config_dir: "{{ smartctl_exporter_config_dir }}"
_common_tls_server_config: "{{ smartctl_exporter_tls_server_config }}"
_common_http_server_config: "{{ smartctl_exporter_http_server_config }}"
_common_basic_auth_users: "{{ smartctl_exporter_basic_auth_users }}"
tags:
- smartctl_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:
@ -61,23 +45,6 @@
- "__smartctl_exporter_cert_file.stat.exists"
- "__smartctl_exporter_key_file.stat.exists"
- name: Check if smartctl_exporter is installed
ansible.builtin.stat:
path: "{{ smartctl_exporter_binary_install_dir }}/smartctl_exporter"
register: __smartctl_exporter_is_installed
check_mode: false
tags:
- smartctl_exporter_install
- name: Gather currently installed smartctl_exporter version (if any)
ansible.builtin.command: "{{ smartctl_exporter_binary_install_dir }}/smartctl_exporter --version"
changed_when: false
register: __smartctl_exporter_current_version_output
check_mode: false
when: __smartctl_exporter_is_installed.stat.exists
tags:
- smartctl_exporter_install
- name: Discover latest version
ansible.builtin.set_fact:
smartctl_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _smartctl_exporter_repo }}/releases/latest', headers=_github_api_headers,
@ -87,24 +54,9 @@
retries: 10
when:
- smartctl_exporter_version == "latest"
- smartctl_exporter_binary_local_dir | length == 0
- not smartctl_exporter_skip_install
- name: Get smartctl_exporter binary checksum
when:
- smartctl_exporter_binary_local_dir | length == 0
- not smartctl_exporter_skip_install
block:
- name: Get checksum list from github
ansible.builtin.set_fact:
__smartctl_exporter_checksums: "{{ lookup('url', smartctl_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}"
run_once: true
until: __smartctl_exporter_checksums is search('linux-' + go_arch + '.tar.gz')
retries: 10
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__smartctl_exporter_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __smartctl_exporter_checksums }}"
when:
- "('linux-' + go_arch + '.tar.gz') in item"
tags:
- smartctl_exporter
- install
- smartctl_exporter_install
- download
- smartctl_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

@ -20,7 +20,7 @@ ExecStart={{ smartctl_exporter_binary_install_dir }}/smartctl_exporter \
'--web.listen-address={{ smartctl_exporter_web_listen_address }}' \
'--web.telemetry-path={{ smartctl_exporter_web_telemetry_path }}' \
{% if smartctl_exporter_tls_server_config | length > 0 or smartctl_exporter_http_server_config | length > 0 or smartctl_exporter_basic_auth_users | length > 0 %}
'--web.config.file=/etc/smartctl_exporter/web_config.yaml' \
'--web.config.file={{ smartctl_exporter_config_dir }}/web_config.yml' \
{% endif %}
'--log.level={{ smartctl_exporter_log_level }}' \
'--log.format={{ smartctl_exporter_log_format }}'

View file

@ -1,18 +0,0 @@
---
{{ ansible_managed | comment }}
{% if smartctl_exporter_tls_server_config | length > 0 %}
tls_server_config:
{{ smartctl_exporter_tls_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if smartctl_exporter_http_server_config | length > 0 %}
http_server_config:
{{ smartctl_exporter_http_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if smartctl_exporter_basic_auth_users | length > 0 %}
basic_auth_users:
{% for k, v in smartctl_exporter_basic_auth_users.items() %}
{{ k }}: {{ v | string | password_hash('bcrypt', ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' | shuffle(seed=inventory_hostname) | join)[:22], rounds=9) }}
{% endfor %}
{% endif %}

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) }}"
_smartctl_exporter_go_ansible_arch: "{{ {'i386': '386',
'x86_64': 'amd64',
'aarch64': 'arm64',
'armv7l': 'armv7',
'armv6l': 'armv6'}.get(ansible_architecture, ansible_architecture) }}"
_smartctl_exporter_repo: "prometheus-community/smartctl_exporter"
_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}"
_smartctl_exporter_binaries: ['smartctl_exporter']