refactor(process_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:10:43 +00:00
parent 90086175e1
commit 2bd22378ba
No known key found for this signature in database
GPG key ID: 75FAE37CBA8C13C2
13 changed files with 73 additions and 265 deletions

View file

@ -1,11 +1,9 @@
---
process_exporter_version: 0.8.3
process_exporter_binary_local_dir: ""
process_exporter_binary_url: "https://github.com/{{ _process_exporter_repo }}/releases/download/v{{ process_exporter_version }}/\
process-exporter-{{ process_exporter_version }}.linux-{{ go_arch }}.tar.gz"
process-exporter-{{ process_exporter_version }}.{{ ansible_system | lower }}-{{ _process_exporter_go_ansible_arch }}.tar.gz"
process_exporter_checksums_url: "https://github.com/{{ _process_exporter_repo }}/releases/download/v{{ process_exporter_version }}/checksums.txt"
process_exporter_skip_install: false
process_exporter_archive_path: /tmp
process_exporter_local_cache_path: "/tmp/process_exporter-{{ ansible_system | lower }}-{{ _process_exporter_go_ansible_arch }}/{{ process_exporter_version }}"
process_exporter_web_listen_address: "0.0.0.0:9256"
@ -30,3 +28,4 @@ process_exporter_names: |
process_exporter_binary_install_dir: "/usr/local/bin"
process_exporter_system_group: "process-exp"
process_exporter_system_user: "{{ process_exporter_system_group }}"
process_exporter_config_dir: "/etc/process_exporter"

View file

@ -11,21 +11,9 @@ argument_specs:
process_exporter_version:
description: "Process exporter package version. Also accepts latest as parameter."
default: "0.8.3"
process_exporter_skip_install:
description: "Process exporter installation tasks gets skipped when set to true."
type: bool
default: false
process_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(process_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(process_exporter_version) parameter"
process_exporter_archive_path:
description: "Local path to stash the archive and its extraction"
default: "/tmp"
process_exporter_binary_url:
description: "URL of the Process exporter binaries .tar.gz file"
default: "https://github.com/{{ _process_exporter_repo }}/releases/download/v{{ process_exporter_version }}/process_exporter-{{ process_exporter_version }}.linux-{{ go_arch }}.tar.gz"
default: "https://github.com/{{ _process_exporter_repo }}/releases/download/v{{ process_exporter_version }}/process-exporter-{{ process_exporter_version }}.{{ ansible_system | lower }}-{{ _process_exporter_go_ansible_arch }}.tar.gz"
process_exporter_checksums_url:
description: "URL of the Process exporter checksums file"
default: "https://github.com/{{ _process_exporter_repo }}/releases/download/v{{ process_exporter_version }}/checksums.txt"
@ -67,3 +55,9 @@ argument_specs:
- "I(Advanced)"
- "Process exporter user"
default: "process-exp"
process_exporter_local_cache_path:
description: "Local path to stash the archive and its extraction"
default: "/tmp/process_exporter-{{ ansible_system | lower }}-{{ _process_exporter_go_ansible_arch }}/{{ process_exporter_version }}"
process_exporter_config_dir:
description: "Path to directory with process_exporter configuration"
default: "/etc/process_exporter"

View file

@ -5,7 +5,7 @@ provisioner:
inventory:
group_vars:
all:
process_exporter_binary_local_dir: "/tmp/process_exporter-linux-amd64"
process_exporter_local_cache_path: "/tmp/process_exporter-linux-amd64"
process_exporter_web_listen_address: "127.0.0.1:8080"
process_exporter_tls_server_config:
cert_file: /etc/process_exporter/tls.cert
@ -14,7 +14,6 @@ provisioner:
http2: true
process_exporter_basic_auth_users:
randomuser: examplepassword
go_arch: amd64
process_exporter_version: 0.7.10
process_exporter_names: |
{% raw %}
@ -29,5 +28,4 @@ provisioner:
- crond
{% endraw %}
process_exporter_binary_url: "https://github.com/ncabatoff/process-exporter/releases/download/v{{\
\ process_exporter_version }}/process-exporter-{{ process_exporter_version }}.linux-{{\
\ go_arch }}.tar.gz"
\ process_exporter_version }}/process-exporter-{{ process_exporter_version }}.linux-amd64.tar.gz"

View file

@ -19,7 +19,7 @@ def test_directories(host):
def test_files(host):
files = [
"/etc/systemd/system/process_exporter.service",
"/usr/local/bin/process_exporter",
"/usr/local/bin/process-exporter",
]
for file in files:
f = host.file(file)
@ -46,7 +46,6 @@ def test_user(host):
assert host.group("process-exp").exists
assert "process-exp" in host.user("process-exp").groups
assert host.user("process-exp").shell == "/usr/sbin/nologin"
assert host.user("process-exp").home == "/"
def test_service(host):

View file

@ -9,7 +9,7 @@ testinfra_hosts = get_target_hosts()
@pytest.mark.parametrize("files", [
"/etc/systemd/system/process_exporter.service",
"/usr/local/bin/process_exporter"
"/usr/local/bin/process-exporter"
])
def test_files(host, files):
f = host.file(files)

View file

@ -1,51 +1,32 @@
---
- name: Copy the process_exporter systemd service file
ansible.builtin.template:
src: process_exporter.service.j2
dest: /etc/systemd/system/process_exporter.service
owner: root
group: root
mode: 0644
notify: restart process_exporter
- name: Create process_exporter config directory
ansible.builtin.file:
path: "/etc/process_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: "{{ process_exporter_system_user }}"
_common_system_group: "{{ process_exporter_system_group }}"
_common_config_dir: "{{ process_exporter_config_dir }}"
_common_tls_server_config: "{{ process_exporter_tls_server_config }}"
_common_http_server_config: "{{ process_exporter_http_server_config }}"
_common_basic_auth_users: "{{ process_exporter_basic_auth_users }}"
tags:
- process_exporter
- configure
- process_exporter_configure
- name: Create/Update configuration file
ansible.builtin.template:
src: "config.yml.j2"
dest: "/etc/process_exporter/config.yml"
owner: root
group: root
owner: "{{ process_exporter_system_user }}"
group: "{{ process_exporter_system_group }}"
mode: 0644
when:
- process_exporter_names != []
notify: restart process_exporter
- name: Configure process_exporter web config
ansible.builtin.template:
src: "web_config.yml.j2"
dest: "/etc/process_exporter/web_config.yml"
owner: root
group: root
mode: 0644
when:
( process_exporter_tls_server_config | length > 0 ) or
( process_exporter_http_server_config | length > 0 ) or
( process_exporter_basic_auth_users | length > 0 )
notify: restart process_exporter
- name: Allow process_exporter port in SELinux on RedHat OS family
community.general.seport:
ports: "{{ process_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:
- process_exporter
- configure
- process_exporter_configure

View file

@ -1,69 +0,0 @@
---
- name: Create the process_exporter group
ansible.builtin.group:
name: "{{ process_exporter_system_group }}"
state: present
system: true
when: process_exporter_system_group != "root"
- name: Create the process_exporter user
ansible.builtin.user:
name: "{{ process_exporter_system_user }}"
groups: "{{ process_exporter_system_group }}"
append: true
shell: /usr/sbin/nologin
system: true
create_home: false
home: /
when: process_exporter_system_user != "root"
- name: Get binary
when:
- process_exporter_binary_local_dir | length == 0
- not process_exporter_skip_install
block:
- name: Download process_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ process_exporter_binary_url }}"
dest: "{{ process_exporter_archive_path }}/process_exporter-{{ process_exporter_version }}.linux-{{ go_arch }}.tar.gz"
checksum: "sha256:{{ __process_exporter_checksum }}"
mode: '0644'
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
- name: Unpack process_exporter binary
become: false
ansible.builtin.unarchive:
src: "{{ process_exporter_archive_path }}/process_exporter-{{ process_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "{{ process_exporter_archive_path }}"
creates: "{{ process_exporter_archive_path }}/process-exporter-{{ process_exporter_version }}.linux-{{ go_arch }}/process-exporter"
delegate_to: localhost
check_mode: false
- name: Propagate process_exporter binaries
ansible.builtin.copy:
src: "{{ process_exporter_archive_path }}/process-exporter-{{ process_exporter_version }}.linux-{{ go_arch }}/process-exporter"
dest: "{{ process_exporter_binary_install_dir }}/process_exporter"
mode: 0755
owner: root
group: root
notify: restart process_exporter
when: not ansible_check_mode
- name: Propagate locally distributed process_exporter binary
ansible.builtin.copy:
src: "{{ process_exporter_binary_local_dir }}/process-exporter"
dest: "{{ process_exporter_binary_install_dir }}/process_exporter"
mode: 0755
owner: root
group: root
when:
- process_exporter_binary_local_dir | length > 0
- not process_exporter_skip_install
notify: restart process_exporter

View file

@ -2,33 +2,34 @@
- name: Preflight
ansible.builtin.include_tasks:
file: preflight.yml
apply:
tags:
- process_exporter_install
- process_exporter_configure
- process_exporter_run
tags:
- process_exporter_install
- process_exporter_configure
- process_exporter_run
- name: Install
ansible.builtin.include_tasks:
file: install.yml
apply:
become: true
tags:
- process_exporter_install
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: install.yml
vars:
_common_local_cache_path: "{{ process_exporter_local_cache_path }}"
_common_binaries: "{{ _process_exporter_binaries }}"
_common_binary_install_dir: "{{ process_exporter_binary_install_dir }}"
_common_binary_url: "{{ process_exporter_binary_url }}"
_common_checksums_url: "{{ process_exporter_checksums_url }}"
_common_system_group: "{{ process_exporter_system_group }}"
_common_system_user: "{{ process_exporter_system_user }}"
_common_config_dir: "{{ process_exporter_config_dir }}"
_common_binary_unarchive_opts: ['--strip-components=1']
tags:
- process_exporter_install
- name: SELinux
ansible.builtin.include_tasks:
file: selinux.yml
apply:
become: true
tags:
- process_exporter_configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: selinux.yml
vars:
_common_selinux_port: "{{ process_exporter_web_listen_address | urlsplit('port') }}"
when: ansible_selinux.status == "enabled"
tags:
- process_exporter_configure
@ -36,10 +37,6 @@
- name: Configure
ansible.builtin.include_tasks:
file: configure.yml
apply:
become: true
tags:
- process_exporter_configure
tags:
- process_exporter_configure

View file

@ -1,47 +1,14 @@
---
- 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: Naive assertion of proper listen address
ansible.builtin.assert:
that:
- "':' in process_exporter_web_listen_address"
- name: Check if process_exporter is installed
ansible.builtin.stat:
path: "{{ process_exporter_binary_install_dir }}/process_exporter"
register: __process_exporter_is_installed
check_mode: false
tags:
- process_exporter_install
- name: Gather currently installed process_exporter version (if any)
ansible.builtin.command: "{{ process_exporter_binary_install_dir }}/process_exporter --version"
changed_when: false
register: __process_exporter_current_version_output
check_mode: false
when: __process_exporter_is_installed.stat.exists
tags:
- process_exporter_install
- name: Discover latest version
ansible.builtin.set_fact:
process_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _process_exporter_repo }}/releases/latest', headers=_github_api_headers,
@ -51,23 +18,9 @@
retries: 10
when:
- process_exporter_version == "latest"
- process_exporter_binary_local_dir | length == 0
- not process_exporter_skip_install
- name: Get process_exporter binary checksum
when:
- process_exporter_binary_local_dir | length == 0
- not process_exporter_skip_install
block:
- name: Get checksum list from github
ansible.builtin.set_fact:
__process_exporter_checksums: "{{ lookup('url', process_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}"
run_once: true
until: __process_exporter_checksums is search('linux-' + go_arch + '.tar.gz')
retries: 10
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__process_exporter_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __process_exporter_checksums }}"
when: "('linux-' + go_arch + '.tar.gz') in item"
tags:
- process_exporter
- install
- process_exporter_install
- download
- process_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

@ -8,12 +8,12 @@ After=network-online.target
Type=simple
User={{ process_exporter_system_user }}
Group={{ process_exporter_system_group }}
ExecStart={{ process_exporter_binary_install_dir }}/process_exporter \
ExecStart={{ process_exporter_binary_install_dir }}/process-exporter \
{% if process_exporter_names != [] -%}
'--config.path=/etc/process_exporter/config.yml' \
'--config.path={{ process_exporter_config_dir }}/config.yml' \
{% endif -%}
{% if process_exporter_tls_server_config | length > 0 or process_exporter_http_server_config | length > 0 or process_exporter_basic_auth_users | length > 0 %}
'--web.config.file=/etc/process_exporter/web_config.yml' \
'--web.config.file={{ process_exporter_config_dir }}/web_config.yml' \
{% endif %}
'--web.listen-address={{ process_exporter_web_listen_address }}' \
'--web.telemetry-path={{ process_exporter_web_telemetry_path }}'

View file

@ -1,19 +0,0 @@
---
{{ ansible_managed | comment }}
{% if process_exporter_tls_server_config | length > 0 %}
tls_server_config:
{{ process_exporter_tls_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if process_exporter_http_server_config | length > 0 %}
http_server_config:
{{ process_exporter_http_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if process_exporter_basic_auth_users | length > 0 %}
basic_auth_users:
{% for k, v in process_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) }}"
_process_exporter_go_ansible_arch: "{{ {'i386': '386',
'x86_64': 'amd64',
'aarch64': 'arm64',
'armv7l': 'armv7',
'armv6l': 'armv6'}.get(ansible_architecture, ansible_architecture) }}"
_process_exporter_repo: "ncabatoff/process-exporter"
_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}"
_process_exporter_binaries: ['process-exporter']