refactor(snmp_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:13:47 +00:00
parent b90cb5bfe4
commit 1173d1ada7
No known key found for this signature in database
GPG key ID: 75FAE37CBA8C13C2
8 changed files with 82 additions and 137 deletions

View file

@ -1,10 +1,8 @@
---
snmp_exporter_version: 0.26.0
snmp_exporter_binary_local_dir: ""
snmp_exporter_binary_url: "https://github.com/{{ _snmp_exporter_repo }}/releases/download/v{{ snmp_exporter_version }}/\
snmp_exporter-{{ snmp_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
snmp_exporter-{{ snmp_exporter_version }}.{{ ansible_system | lower }}-{{ _snmp_exporter_go_ansible_arch }}.tar.gz"
snmp_exporter_checksums_url: "https://github.com/{{ _snmp_exporter_repo }}/releases/download/v{{ snmp_exporter_version }}/sha256sums.txt"
snmp_exporter_skip_install: false
snmp_exporter_web_listen_address: "0.0.0.0:9116"
snmp_exporter_log_level: info
@ -12,6 +10,9 @@ snmp_exporter_log_level: info
snmp_exporter_config_file: ""
snmp_exporter_binary_install_dir: "/usr/local/bin"
snmp_exporter_config_dir: "/etc/snmp_exporter"
# Local path to stash the archive and its extraction
snmp_exporter_archive_path: /tmp
snmp_exporter_local_cache_path: "/tmp/snmp_exporter-{{ ansible_system | lower }}-{{ _snmp_exporter_go_ansible_arch }}/{{ snmp_exporter_version }}"
snmp_exporter_system_user: snmp-exp
snmp_exporter_system_group: "{{ snmp_exporter_system_user }}"

View file

@ -11,18 +11,9 @@ argument_specs:
snmp_exporter_version:
description: "SNMP exporter package version. Also accepts latest as parameter."
default: "0.26.0"
snmp_exporter_skip_install:
description: "SNMP exporter installation tasks gets skipped when set to true."
type: bool
default: false
snmp_exporter_binary_local_dir:
description:
- "Enables the use of local packages instead of those distributed on github."
- "The parameter masnmp set to a directory where the C(snmp_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(snmp_exporter_version) parameter"
snmp_exporter_binary_url:
description: "URL of the snmp exporter binaries .tar.gz file"
default: "https://github.com/{{ _snmp_exporter_repo }}/releases/download/v{{ snmp_exporter_version }}/snmp_exporter-{{ snmp_exporter_version }}.linux-{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}.tar.gz"
default: "https://github.com/{{ _snmp_exporter_repo }}/releases/download/v{{ snmp_exporter_version }}/snmp_exporter-{{ snmp_exporter_version }}.{{ ansible_system | lower }}-{{ _snmp_exporter_go_ansible_arch }}.tar.gz"
snmp_exporter_checksums_url:
description: "URL of the snmp exporter checksums file"
default: "https://github.com/{{ _snmp_exporter_repo }}/releases/download/v{{ snmp_exporter_version }}/sha256sums.txt"
@ -41,6 +32,19 @@ argument_specs:
- "I(Advanced)"
- "Directory to install snmp_exporter binary"
default: "/usr/local/bin"
snmp_exporter_archive_path:
description: 'Local path to stash the archive and its extraction'
default: "/tmp"
snmp_exporter_system_user:
description:
- "I(Advanced)"
- "snmp_exporter system user"
default: snmp-exp
snmp_exporter_system_group:
description:
- "I(Advanced)"
- "System group for snmp_exporter"
default: snmp-exp
snmp_exporter_local_cache_path:
description: "Local path to stash the archive and its extraction"
default: "/tmp/snmp_exporter-{{ ansible_system | lower }}-{{ _snmp_exporter_go_ansible_arch }}/{{ snmp_exporter_version }}"
snmp_exporter_config_dir:
description: "Path to directory with snmp_exporter configuration"
default: "/etc/snmp_exporter"

View file

@ -1,33 +1,29 @@
---
- name: Create configuration directory
ansible.builtin.file:
path: "/etc/snmp_exporter"
state: directory
owner: root
group: root
mode: 0755
- name: Copy the SNMP Exporter systemd service file
ansible.builtin.template:
src: snmp_exporter.service.j2
dest: /etc/systemd/system/snmp_exporter.service
owner: root
group: root
mode: 0644
notify:
- restart snmp_exporter
- name: Configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: configure.yml
vars:
_common_system_user: "{{ snmp_exporter_system_user }}"
_common_system_group: "{{ snmp_exporter_system_group }}"
_common_config_dir: "{{ snmp_exporter_config_dir }}"
tags:
- snmp_exporter
- configure
- snmp_exporter_configure
- name: Copy configuration file
ansible.builtin.template:
src: "{{ snmp_exporter_config_file | default(__snmp_exporter_config_file, true) }}"
dest: /etc/snmp_exporter/snmp.yml
src: "{{ snmp_exporter_config_file | default(snmp_exporter_local_cache_path ~ '/snmp.yml', true) }}"
dest: "{{ snmp_exporter_config_dir }}/snmp.yml"
owner: root
group: root
mode: 0644
no_log: "{{ false if (lookup('env', 'CI')) or (lookup('env', 'MOLECULE_PROVISIONER_NAME')) else true }}"
notify:
- reload snmp_exporter
vars:
__snmp_exporter_config_file: "{{ (snmp_exporter_binary_local_dir) |
ternary(snmp_exporter_binary_local_dir ~ '/snmp.yml',
snmp_exporter_archive_path ~ '/snmp_exporter-' ~ snmp_exporter_version ~ '.linux-' ~ go_arch ~ '/snmp.yml') }}"
become: true
tags:
- snmp_exporter
- configure
- snmp_exporter_configure

View file

@ -1,51 +0,0 @@
---
- name: Get binary
when:
- snmp_exporter_binary_local_dir | length == 0
- not snmp_exporter_skip_install
block:
- name: Download snmp_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ snmp_exporter_binary_url }}"
dest: "{{ snmp_exporter_archive_path }}/snmp_exporter-{{ snmp_exporter_version }}.linux-{{ go_arch }}.tar.gz"
checksum: "sha256:{{ __snmp_exporter_checksum }}"
mode: '0644'
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
- name: Unpack snmp_exporter binary
become: false
ansible.builtin.unarchive:
src: "{{ snmp_exporter_archive_path }}/snmp_exporter-{{ snmp_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "{{ snmp_exporter_archive_path }}"
creates: "{{ snmp_exporter_archive_path }}/snmp_exporter-{{ snmp_exporter_version }}.linux-{{ go_arch }}/snmp_exporter"
delegate_to: localhost
check_mode: false
- name: Propagate snmp_exporter binaries
ansible.builtin.copy:
src: "{{ snmp_exporter_archive_path }}/snmp_exporter-{{ snmp_exporter_version }}.linux-{{ go_arch }}/snmp_exporter"
dest: "{{ snmp_exporter_binary_install_dir }}/snmp_exporter"
mode: 0755
owner: root
group: root
notify: restart snmp_exporter
when: not ansible_check_mode
- name: Propagate locally distributed snmp_exporter binary
ansible.builtin.copy:
src: "{{ snmp_exporter_binary_local_dir }}/snmp_exporter"
dest: "{{ snmp_exporter_binary_install_dir }}/snmp_exporter"
mode: 0755
owner: root
group: root
when:
- snmp_exporter_binary_local_dir | length > 0
- not snmp_exporter_skip_install
notify: restart snmp_exporter

View file

@ -2,33 +2,41 @@
- name: Preflight
ansible.builtin.include_tasks:
file: preflight.yml
apply:
tags:
- snmp_exporter_install
- snmp_exporter_configure
- snmp_exporter_run
tags:
- snmp_exporter_install
- snmp_exporter_configure
- snmp_exporter_run
- name: Install
ansible.builtin.include_tasks:
file: install.yml
apply:
become: true
tags:
- snmp_exporter_install
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: install.yml
vars:
_common_local_cache_path: "{{ snmp_exporter_local_cache_path }}"
_common_binaries: "{{ _snmp_exporter_binaries }}"
_common_binary_install_dir: "{{ snmp_exporter_binary_install_dir }}"
_common_binary_url: "{{ snmp_exporter_binary_url }}"
_common_checksums_url: "{{ snmp_exporter_checksums_url }}"
_common_system_group: "{{ snmp_exporter_system_group }}"
_common_system_user: "{{ snmp_exporter_system_user }}"
_common_config_dir: "{{ snmp_exporter_config_dir }}"
_common_binary_unarchive_opts: ['--strip-components=1']
tags:
- snmp_exporter_install
- name: SELinux
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: selinux.yml
vars:
_common_selinux_port: "{{ snmp_exporter_web_listen_address | urlsplit('port') }}"
when: ansible_selinux.status == "enabled"
tags:
- snmp_exporter_configure
- name: Configure
ansible.builtin.include_tasks:
file: configure.yml
apply:
become: true
tags:
- snmp_exporter_configure
tags:
- snmp_exporter_configure

View file

@ -1,4 +1,8 @@
---
- 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:
@ -30,24 +34,9 @@
retries: 10
when:
- snmp_exporter_version == "latest"
- snmp_exporter_binary_local_dir | length == 0
- not snmp_exporter_skip_install
- name: Get snmp_exporter binary checksum
when:
- snmp_exporter_binary_local_dir | length == 0
- not snmp_exporter_skip_install
block:
- name: Get checksum list from github
ansible.builtin.set_fact:
__snmp_exporter_checksums: "{{ lookup('url', snmp_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}"
run_once: true
until: __snmp_exporter_checksums is search('linux-' + go_arch + '.tar.gz')
retries: 10
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__snmp_exporter_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __snmp_exporter_checksums }}"
when:
- "('linux-' + go_arch + '.tar.gz') in item"
tags:
- snmp_exporter
- install
- snmp_exporter_install
- download
- snmp_exporter_download

View file

@ -8,7 +8,7 @@ Type=simple
User=nobody
Group={{ 'nogroup' if ansible_os_family == 'Debian' else 'nobody' }}
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/bin/snmp_exporter \
ExecStart={{ snmp_exporter_binary_install_dir }}/snmp_exporter \
{% if snmp_exporter_version is version('0.21.0', '>=') and
snmp_exporter_web_listen_address is iterable and
snmp_exporter_web_listen_address is not mapping and
@ -20,7 +20,7 @@ ExecStart=/usr/local/bin/snmp_exporter \
--web.listen-address={{ snmp_exporter_web_listen_address }} \
{% endif %}
--log.level={{ snmp_exporter_log_level }} \
--config.file=/etc/snmp_exporter/snmp.yml
--config.file={{ snmp_exporter_config_dir }}/snmp.yml
KillMode=process

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