refactor(ipmi_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:05:25 +00:00
parent a3f317588d
commit f09635db73
No known key found for this signature in database
GPG key ID: 75FAE37CBA8C13C2
12 changed files with 76 additions and 272 deletions

View file

@ -1,10 +1,8 @@
--- ---
ipmi_exporter_version: 1.8.0 ipmi_exporter_version: 1.8.0
ipmi_exporter_binary_local_dir: ""
ipmi_exporter_binary_url: "https://github.com/{{ _ipmi_exporter_repo }}/releases/download/v{{ ipmi_exporter_version }}/\ ipmi_exporter_binary_url: "https://github.com/{{ _ipmi_exporter_repo }}/releases/download/v{{ ipmi_exporter_version }}/\
ipmi_exporter-{{ ipmi_exporter_version }}.linux-{{ go_arch }}.tar.gz" ipmi_exporter-{{ ipmi_exporter_version }}.{{ ansible_system | lower }}-{{ _ipmi_exporter_go_ansible_arch }}.tar.gz"
ipmi_exporter_checksums_url: "https://github.com/{{ _ipmi_exporter_repo }}/releases/download/v{{ ipmi_exporter_version }}/sha256sums.txt" ipmi_exporter_checksums_url: "https://github.com/{{ _ipmi_exporter_repo }}/releases/download/v{{ ipmi_exporter_version }}/sha256sums.txt"
ipmi_exporter_skip_install: false
ipmi_exporter_modules: ipmi_exporter_modules:
default: default:
@ -18,17 +16,17 @@ ipmi_exporter_modules:
ipmi_exporter_web_listen_address: "0.0.0.0:9290" ipmi_exporter_web_listen_address: "0.0.0.0:9290"
ipmi_exporter_tls_server_config: {} ipmi_exporter_tls_server_config: {}
ipmi_exporter_http_server_config: {} ipmi_exporter_http_server_config: {}
ipmi_exporter_basic_auth_users: {} ipmi_exporter_basic_auth_users: {}
ipmi_exporter_log_level: "info" ipmi_exporter_log_level: "info"
ipmi_exporter_log_format: "logfmt" ipmi_exporter_log_format: "logfmt"
ipmi_exporter_binary_install_dir: "/usr/local/bin" ipmi_exporter_binary_install_dir: "/usr/local/bin"
ipmi_exporter_system_group: "ipmi-exp" ipmi_exporter_system_user: "ipmi-exp"
ipmi_exporter_system_user: "{{ ipmi_exporter_system_group }}" ipmi_exporter_system_group: "{{ ipmi_exporter_system_user }}"
ipmi_exporter_config_dir: /etc/ipmi_exporter
# Local path to stash the archive and its extraction # Local path to stash the archive and its extraction
ipmi_exporter_archive_path: /tmp ipmi_exporter_local_cache_path: "/tmp/ipmi_exporter-{{ ansible_system | lower }}-{{ _ipmi_exporter_go_ansible_arch }}/{{ ipmi_exporter_version }}"

View file

@ -11,18 +11,9 @@ argument_specs:
ipmi_exporter_version: ipmi_exporter_version:
description: "ipmi_exporter package version. Also accepts latest as parameter." description: "ipmi_exporter package version. Also accepts latest as parameter."
default: "1.8.0" default: "1.8.0"
ipmi_exporter_skip_install:
description: "ipmi_exporter installation tasks gets skipped when set to true."
type: bool
default: false
ipmi_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(ipmi_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(ipmi_exporter_version) parameter"
ipmi_exporter_binary_url: ipmi_exporter_binary_url:
description: "URL of the ipmi_exporter binaries .tar.gz file" description: "URL of the ipmi_exporter binaries .tar.gz file"
default: "https://github.com/{{ _ipmi_exporter_repo }}/releases/download/v{{ ipmi_exporter_version }}/ipmi_exporter-{{ ipmi_exporter_version }}.linux-{{ go_arch }}.tar.gz" default: "https://github.com/{{ _ipmi_exporter_repo }}/releases/download/v{{ ipmi_exporter_version }}/ipmi_exporter-{{ ipmi_exporter_version }}.{{ ansible_system | lower }}-{{ _ipmi_exporter_go_ansible_arch }}.tar.gz"
ipmi_exporter_checksums_url: ipmi_exporter_checksums_url:
description: "URL of the ipmi_exporter checksums file" description: "URL of the ipmi_exporter checksums file"
default: "https://github.com/{{ _ipmi_exporter_repo }}/releases/download/v{{ ipmi_exporter_version }}/sha256sums.txt" default: "https://github.com/{{ _ipmi_exporter_repo }}/releases/download/v{{ ipmi_exporter_version }}/sha256sums.txt"
@ -68,6 +59,9 @@ argument_specs:
- "I(Advanced)" - "I(Advanced)"
- "ipmi_exporter user" - "ipmi_exporter user"
default: "ipmi-exp" default: "ipmi-exp"
ipmi_exporter_archive_path: ipmi_exporter_local_cache_path:
description: 'Local path to stash the archive and its extraction' description: "Local path to stash the archive and its extraction"
default: "/tmp" default: "/tmp/ipmi_exporter-{{ ansible_system | lower }}-{{ _ipmi_exporter_go_ansible_arch }}/{{ ipmi_exporter_version }}"
ipmi_exporter_config_dir:
description: "Path to directory with ipmi_exporter configuration"
default: "/etc/ipmi_exporter"

View file

@ -5,7 +5,7 @@ provisioner:
inventory: inventory:
group_vars: group_vars:
all: all:
ipmi_exporter_binary_local_dir: "/tmp/ipmi_exporter-linux-amd64" ipmi_exporter_local_cache_path: "/tmp/ipmi_exporter-linux-amd64"
ipmi_exporter_web_listen_address: "127.0.0.1:8080" ipmi_exporter_web_listen_address: "127.0.0.1:8080"
ipmi_exporter_tls_server_config: ipmi_exporter_tls_server_config:
cert_file: /etc/ipmi_exporter/tls.cert cert_file: /etc/ipmi_exporter/tls.cert
@ -14,8 +14,6 @@ provisioner:
http2: true http2: true
ipmi_exporter_basic_auth_users: ipmi_exporter_basic_auth_users:
randomuser: examplepassword randomuser: examplepassword
go_arch: amd64
ipmi_exporter_version: 1.6.1 ipmi_exporter_version: 1.6.1
ipmi_exporter_binary_url: "https://github.com/prometheus-community/ipmi_exporter/releases/download/v{{\ ipmi_exporter_binary_url: "https://github.com/prometheus-community/ipmi_exporter/releases/download/v{{\
\ ipmi_exporter_version }}/ipmi_exporter-{{ ipmi_exporter_version }}.linux-{{\ \ ipmi_exporter_version }}/ipmi_exporter-{{ ipmi_exporter_version }}.linux-amd64.tar.gz"
\ go_arch }}.tar.gz"

View file

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

View file

@ -1,47 +1,32 @@
--- ---
- name: Copy the ipmi_exporter systemd service file - name: "Common configure"
ansible.builtin.template: ansible.builtin.include_role:
src: ipmi_exporter.service.j2 name: prometheus.prometheus._common
dest: /etc/systemd/system/ipmi_exporter.service tasks_from: configure.yml
owner: root vars:
group: root _common_system_user: "{{ ipmi_exporter_system_user }}"
mode: 0644 _common_system_group: "{{ ipmi_exporter_system_group }}"
notify: restart ipmi_exporter _common_config_dir: "{{ ipmi_exporter_config_dir }}"
_common_tls_server_config: "{{ ipmi_exporter_tls_server_config }}"
- name: Create ipmi_exporter config directory _common_http_server_config: "{{ ipmi_exporter_http_server_config }}"
ansible.builtin.file: _common_basic_auth_users: "{{ ipmi_exporter_basic_auth_users }}"
path: "/etc/ipmi_exporter" tags:
state: directory - ipmi_exporter
owner: root - configure
group: root - ipmi_exporter_configure
mode: u+rwX,g+rwX,o=rX
- name: Copy the ipmi_exporter web config file
ansible.builtin.template:
src: web_config.yaml.j2
dest: /etc/ipmi_exporter/web_config.yaml
owner: root
group: root
mode: 0644
notify: restart ipmi_exporter
- name: Copy the ipmi_exporter config file - name: Copy the ipmi_exporter config file
ansible.builtin.template: ansible.builtin.template:
src: config.yaml.j2 src: config.yaml.j2
dest: /etc/ipmi_exporter/config.yaml dest: /etc/ipmi_exporter/config.yaml
owner: root owner: "{{ ipmi_exporter_system_user }}"
group: "{{ ipmi_exporter_system_group }}" group: "{{ ipmi_exporter_system_group }}"
mode: 0640 mode: 0640
become: true
no_log: "{{ false if (lookup('env', 'CI')) or (lookup('env', 'MOLECULE_PROVISIONER_NAME')) else true }}" no_log: "{{ false if (lookup('env', 'CI')) or (lookup('env', 'MOLECULE_PROVISIONER_NAME')) else true }}"
notify: notify:
- restart ipmi_exporter - restart ipmi_exporter
tags:
- name: Allow ipmi_exporter port in SELinux on RedHat OS family - ipmi_exporter
community.general.seport: - configure
ports: "{{ ipmi_exporter_web_listen_address.split(':')[-1] }}" - ipmi_exporter_configure
proto: tcp
setype: http_port_t
state: present
when:
- ansible_version.full is version_compare('2.4', '>=')
- ansible_selinux.status == "enabled"

View file

@ -1,74 +0,0 @@
---
- name: Create the ipmi_exporter group
ansible.builtin.group:
name: "{{ ipmi_exporter_system_group }}"
state: present
system: true
when: ipmi_exporter_system_group != "root"
- name: Create the ipmi_exporter user
ansible.builtin.user:
name: "{{ ipmi_exporter_system_user }}"
groups: "{{ ipmi_exporter_system_group }}"
append: true
shell: /usr/sbin/nologin
system: true
create_home: false
home: /
when: ipmi_exporter_system_user != "root"
- name: Get binary
when:
- ipmi_exporter_binary_local_dir | length == 0
- not ipmi_exporter_skip_install
block:
- name: Download ipmi_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ ipmi_exporter_binary_url }}"
dest: "{{ ipmi_exporter_archive_path }}/ipmi_exporter-{{ ipmi_exporter_version }}.linux-{{ go_arch }}.tar.gz"
checksum: "sha256:{{ __ipmi_exporter_checksum }}"
mode: '0644'
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
- name: Unpack ipmi_exporter binary
become: false
ansible.builtin.unarchive:
src: "{{ ipmi_exporter_archive_path }}/ipmi_exporter-{{ ipmi_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "{{ ipmi_exporter_archive_path }}"
creates: "{{ ipmi_exporter_archive_path }}/ipmi_exporter-{{ ipmi_exporter_version }}.linux-{{ go_arch }}/ipmi_exporter"
delegate_to: localhost
check_mode: false
- name: Propagate ipmi_exporter binaries
ansible.builtin.copy:
src: "{{ ipmi_exporter_archive_path }}/ipmi_exporter-{{ ipmi_exporter_version }}.linux-{{ go_arch }}/ipmi_exporter"
dest: "{{ ipmi_exporter_binary_install_dir }}/ipmi_exporter"
mode: 0755
owner: root
group: root
notify: restart ipmi_exporter
when: not ansible_check_mode
- name: Propagate locally distributed ipmi_exporter binary
ansible.builtin.copy:
src: "{{ ipmi_exporter_binary_local_dir }}/ipmi_exporter"
dest: "{{ ipmi_exporter_binary_install_dir }}/ipmi_exporter"
mode: 0755
owner: root
group: root
when:
- ipmi_exporter_binary_local_dir | length > 0
- not ipmi_exporter_skip_install
notify: restart ipmi_exporter
- name: Install freeipmi package
ansible.builtin.package:
name: freeipmi
state: present

View file

@ -2,40 +2,34 @@
- name: Preflight - name: Preflight
ansible.builtin.include_tasks: ansible.builtin.include_tasks:
file: preflight.yml file: preflight.yml
apply:
tags:
- ipmi_exporter_install
- ipmi_exporter_configure
- ipmi_exporter_run
tags: tags:
- ipmi_exporter_install - ipmi_exporter_install
- ipmi_exporter_configure - ipmi_exporter_configure
- ipmi_exporter_run - ipmi_exporter_run
- name: Install - name: Install
ansible.builtin.include_tasks: ansible.builtin.include_role:
file: install.yml name: prometheus.prometheus._common
apply: tasks_from: install.yml
become: true vars:
tags: _common_local_cache_path: "{{ ipmi_exporter_local_cache_path }}"
- ipmi_exporter_install _common_binaries: "{{ _ipmi_exporter_binaries }}"
when: _common_binary_install_dir: "{{ ipmi_exporter_binary_install_dir }}"
( not __ipmi_exporter_is_installed.stat.exists ) or _common_binary_url: "{{ ipmi_exporter_binary_url }}"
( (__ipmi_exporter_current_version_output.stderr_lines | length > 0) _common_checksums_url: "{{ ipmi_exporter_checksums_url }}"
and (__ipmi_exporter_current_version_output.stderr_lines[0].split(" ")[2] != ipmi_exporter_version) ) or _common_system_group: "{{ ipmi_exporter_system_group }}"
( (__ipmi_exporter_current_version_output.stdout_lines | length > 0) _common_system_user: "{{ ipmi_exporter_system_user }}"
and (__ipmi_exporter_current_version_output.stdout_lines[0].split(" ")[2] != ipmi_exporter_version) ) or _common_config_dir: "{{ ipmi_exporter_config_dir }}"
( ipmi_exporter_binary_local_dir | length > 0 ) _common_binary_unarchive_opts: ['--strip-components=1']
tags: tags:
- ipmi_exporter_install - ipmi_exporter_install
- name: SELinux - name: SELinux
ansible.builtin.include_tasks: ansible.builtin.include_role:
file: selinux.yml name: prometheus.prometheus._common
apply: tasks_from: selinux.yml
become: true vars:
tags: _common_selinux_port: "{{ ipmi_exporter_web_listen_address | urlsplit('port') }}"
- ipmi_exporter_configure
when: ansible_selinux.status == "enabled" when: ansible_selinux.status == "enabled"
tags: tags:
- ipmi_exporter_configure - ipmi_exporter_configure
@ -43,10 +37,6 @@
- name: Configure - name: Configure
ansible.builtin.include_tasks: ansible.builtin.include_tasks:
file: configure.yml file: configure.yml
apply:
become: true
tags:
- ipmi_exporter_configure
tags: tags:
- ipmi_exporter_configure - ipmi_exporter_configure

View file

@ -1,24 +1,10 @@
--- ---
- name: Assert usage of systemd as an init system - name: Common preflight
ansible.builtin.assert: ansible.builtin.include_role:
that: ansible_service_mgr == 'systemd' name: prometheus.prometheus._common
msg: "This role only works with systemd" tasks_from: preflight.yml
- name: Install package fact dependencies
become: true
ansible.builtin.package:
name: "{{ _pkg_fact_req }}"
state: present
when: (_pkg_fact_req)
vars: vars:
_pkg_fact_req: "{% if (ansible_pkg_mgr == 'apt') %}\ _common_dependencies: "{{ _ipmi_exporter_dependencies }}"
{{ ('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: Assert that used version supports listen address type - name: Assert that used version supports listen address type
ansible.builtin.assert: ansible.builtin.assert:
@ -61,23 +47,6 @@
- "__ipmi_exporter_cert_file.stat.exists" - "__ipmi_exporter_cert_file.stat.exists"
- "__ipmi_exporter_key_file.stat.exists" - "__ipmi_exporter_key_file.stat.exists"
- name: Check if ipmi_exporter is installed
ansible.builtin.stat:
path: "{{ ipmi_exporter_binary_install_dir }}/ipmi_exporter"
register: __ipmi_exporter_is_installed
check_mode: false
tags:
- ipmi_exporter_install
- name: Gather currently installed ipmi_exporter version (if any)
ansible.builtin.command: "{{ ipmi_exporter_binary_install_dir }}/ipmi_exporter --version"
changed_when: false
register: __ipmi_exporter_current_version_output
check_mode: false
when: __ipmi_exporter_is_installed.stat.exists
tags:
- ipmi_exporter_install
- name: Discover latest version - name: Discover latest version
ansible.builtin.set_fact: ansible.builtin.set_fact:
ipmi_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _ipmi_exporter_repo }}/releases/latest', headers=_github_api_headers, ipmi_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _ipmi_exporter_repo }}/releases/latest', headers=_github_api_headers,
@ -87,24 +56,9 @@
retries: 10 retries: 10
when: when:
- ipmi_exporter_version == "latest" - ipmi_exporter_version == "latest"
- ipmi_exporter_binary_local_dir | length == 0 tags:
- not ipmi_exporter_skip_install - ipmi_exporter
- install
- name: Get ipmi_exporter binary checksum - ipmi_exporter_install
when: - download
- ipmi_exporter_binary_local_dir | length == 0 - ipmi_exporter_download
- not ipmi_exporter_skip_install
block:
- name: Get checksum list from github
ansible.builtin.set_fact:
__ipmi_exporter_checksums: "{{ lookup('url', ipmi_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}"
run_once: true
until: __ipmi_exporter_checksums is search('linux-' + go_arch + '.tar.gz')
retries: 10
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__ipmi_exporter_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __ipmi_exporter_checksums }}"
when:
- "('linux-' + go_arch + '.tar.gz') in item"

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

@ -9,10 +9,10 @@ Type=simple
User={{ ipmi_exporter_system_user }} User={{ ipmi_exporter_system_user }}
Group={{ ipmi_exporter_system_group }} Group={{ ipmi_exporter_system_group }}
ExecStart={{ ipmi_exporter_binary_install_dir }}/ipmi_exporter \ ExecStart={{ ipmi_exporter_binary_install_dir }}/ipmi_exporter \
'--config.file=/etc/ipmi_exporter/config.yaml' \ '--config.file={{ ipmi_exporter_config_dir }}/config.yaml' \
'--web.listen-address={{ ipmi_exporter_web_listen_address }}' \ '--web.listen-address={{ ipmi_exporter_web_listen_address }}' \
{% if ipmi_exporter_tls_server_config | length > 0 or ipmi_exporter_http_server_config | length > 0 or ipmi_exporter_basic_auth_users | length > 0 %} {% if ipmi_exporter_tls_server_config | length > 0 or ipmi_exporter_http_server_config | length > 0 or ipmi_exporter_basic_auth_users | length > 0 %}
'--web.config.file=/etc/ipmi_exporter/web_config.yaml' \ '--web.config.file={{ ipmi_exporter_config_dir }}/web_config.yml' \
{% endif %} {% endif %}
'--log.level={{ ipmi_exporter_log_level }}' \ '--log.level={{ ipmi_exporter_log_level }}' \
'--log.format={{ ipmi_exporter_log_format }}' '--log.format={{ ipmi_exporter_log_format }}'

View file

@ -1,18 +0,0 @@
---
{{ ansible_managed | comment }}
{% if ipmi_exporter_tls_server_config | length > 0 %}
tls_server_config:
{{ ipmi_exporter_tls_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if ipmi_exporter_http_server_config | length > 0 %}
http_server_config:
{{ ipmi_exporter_http_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if ipmi_exporter_basic_auth_users | length > 0 %}
basic_auth_users:
{% for k, v in ipmi_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,12 @@
--- ---
go_arch_map: _ipmi_exporter_go_ansible_arch: "{{ {'i386': '386',
i386: '386' 'x86_64': 'amd64',
x86_64: 'amd64' 'aarch64': 'arm64',
aarch64: 'arm64' 'armv7l': 'armv7',
armv7l: 'armv7' 'armv6l': 'armv6'}.get(ansible_architecture, ansible_architecture) }}"
armv6l: 'armv6'
go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}"
_ipmi_exporter_repo: "prometheus-community/ipmi_exporter" _ipmi_exporter_repo: "prometheus-community/ipmi_exporter"
_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}" _github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}"
_ipmi_exporter_binaries: ['ipmi_exporter']
_ipmi_exporter_dependencies: "{{ (ansible_pkg_mgr == 'apt')
| ternary((['python-apt'] if ansible_python_version is version('3', '<') else ['python3-apt']),
[]) + ['freeipmi'] }}"