refactor(mongodb_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:06:59 +00:00
parent 4dac93aa74
commit c214571821
No known key found for this signature in database
GPG key ID: 75FAE37CBA8C13C2
12 changed files with 55 additions and 267 deletions

View file

@ -1,15 +1,12 @@
--- ---
mongodb_exporter_version: 0.41.1 mongodb_exporter_version: 0.41.1
mongodb_exporter_binary_local_dir: ""
mongodb_exporter_binary_url: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/\ mongodb_exporter_binary_url: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/\
mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz" mongodb_exporter-{{ mongodb_exporter_version }}.{{ ansible_system | lower }}-{{ _mongodb_exporter_go_ansible_arch }}.tar.gz"
mongodb_exporter_checksums_url: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/\ mongodb_exporter_checksums_url: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/\
mongodb_exporter_{{ mongodb_exporter_version }}_checksums.txt" mongodb_exporter_{{ mongodb_exporter_version }}_checksums.txt"
mongodb_exporter_skip_install: false
mongodb_exporter_web_listen_address: "0.0.0.0:9216" mongodb_exporter_web_listen_address: "0.0.0.0:9216"
mongodb_exporter_web_telemetry_path: "/metrics" mongodb_exporter_web_telemetry_path: "/metrics"
mongodb_exporter_uri: "mongodb://127.0.0.1:27017/admin?ssl=false" mongodb_exporter_uri: "mongodb://127.0.0.1:27017/admin?ssl=false"
# specify a list of collectors or "all" # specify a list of collectors or "all"
mongodb_exporter_collectors: [] mongodb_exporter_collectors: []
@ -32,9 +29,9 @@ mongodb_exporter_basic_auth_users: {}
mongodb_exporter_log_level: "error" mongodb_exporter_log_level: "error"
mongodb_exporter_binary_install_dir: "/usr/local/bin" mongodb_exporter_binary_install_dir: "/usr/local/bin"
mongodb_exporter_system_group: "{{ mongodb_exporter_system_user }}"
mongodb_exporter_system_user: "mongodb-exp" mongodb_exporter_system_user: "mongodb-exp"
mongodb_exporter_system_group: "{{ mongodb_exporter_system_user }}"
mongodb_exporter_config_dir: "/etc/mongodb_exporter" mongodb_exporter_config_dir: "/etc/mongodb_exporter"
# Local path to stash the archive and its extraction # Local path to stash the archive and its extraction
mongodb_exporter_archive_path: /tmp mongodb_exporter_local_cache_path: "/tmp/mongodb_exporter-{{ ansible_system | lower }}-{{ _mongodb_exporter_go_ansible_arch }}/{{ mongodb_exporter_version }}"

View file

@ -11,18 +11,9 @@ argument_specs:
mongodb_exporter_version: mongodb_exporter_version:
description: "mongodb_exporter package version. Also accepts latest as parameter." description: "mongodb_exporter package version. Also accepts latest as parameter."
default: "0.41.1" default: "0.41.1"
mongodb_exporter_skip_install:
description: "mongodb_exporter installation tasks gets skipped when set to true."
type: bool
default: false
mongodb_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(mongodb_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(mongodb_exporter_version) parameter"
mongodb_exporter_binary_url: mongodb_exporter_binary_url:
description: "URL of the mongodb_exporter binaries .tar.gz file" description: "URL of the mongodb_exporter binaries .tar.gz file"
default: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz" default: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/mongodb_exporter-{{ mongodb_exporter_version }}.{{ ansible_system | lower }}-{{ _mongodb_exporter_go_ansible_arch }}.tar.gz"
mongodb_exporter_checksums_url: mongodb_exporter_checksums_url:
description: "URL of the mongodb_exporter checksums file" description: "URL of the mongodb_exporter checksums file"
default: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/mongodb_exporter_{{ mongodb_exporter_version }}_checksums.txt" default: "https://github.com/{{ _mongodb_exporter_repo }}/releases/download/v{{ mongodb_exporter_version }}/mongodb_exporter_{{ mongodb_exporter_version }}_checksums.txt"
@ -33,7 +24,7 @@ argument_specs:
description: "Path under which to expose metrics" description: "Path under which to expose metrics"
default: "/metrics" default: "/metrics"
mongodb_exporter_config_dir: mongodb_exporter_config_dir:
description: "The path where exporter configuration is stored" description: "Path to directory with mongodb_exporter configuration"
default: "/etc/mongodb_exporter" default: "/etc/mongodb_exporter"
mongodb_exporter_tls_server_config: mongodb_exporter_tls_server_config:
description: description:
@ -118,6 +109,6 @@ argument_specs:
- "I(Advanced)" - "I(Advanced)"
- "mongodb_exporter user" - "mongodb_exporter user"
default: "mongodb-exp" default: "mongodb-exp"
mongodb_exporter_archive_path: mongodb_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/mongodb_exporter-{{ ansible_system | lower }}-{{ _mongodb_exporter_go_ansible_arch }}/{{ mongodb_exporter_version }}"

View file

@ -5,7 +5,7 @@ provisioner:
inventory: inventory:
group_vars: group_vars:
all: all:
mongodb_exporter_binary_local_dir: "/tmp/mongodb_exporter-linux-amd64_local" mongodb_exporter_local_cache_path: "/tmp/mongodb_exporter-linux-amd64_local"
mongodb_exporter_web_listen_address: "127.0.1.1:9216" mongodb_exporter_web_listen_address: "127.0.1.1:9216"
mongodb_exporter_tls_server_config: mongodb_exporter_tls_server_config:
cert_file: /etc/mongodb_exporter/tls.cert cert_file: /etc/mongodb_exporter/tls.cert
@ -14,7 +14,6 @@ provisioner:
http2: true http2: true
mongodb_exporter_basic_auth_users: mongodb_exporter_basic_auth_users:
randomuser: examplepassword randomuser: examplepassword
go_arch: amd64
mongodb_exporter_version: 0.40.0 mongodb_exporter_version: 0.40.0
mongodb_exporter_binary_url: "https://github.com/percona/mongodb_exporter/releases/download/v{{ mongodb_exporter_version }}/\ mongodb_exporter_binary_url: "https://github.com/percona/mongodb_exporter/releases/download/v{{ mongodb_exporter_version }}/\
mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz" mongodb_exporter-{{ mongodb_exporter_version }}.linux-amd64.tar.gz"

View file

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

View file

@ -1,36 +0,0 @@
---
- name: Copy the mongodb_exporter systemd service file
ansible.builtin.template:
src: mongodb_exporter.service.j2
dest: /etc/systemd/system/mongodb_exporter.service
owner: root
group: root
mode: 0644
notify: restart mongodb_exporter
- name: Create mongodb_exporter config directory
ansible.builtin.file:
path: "/etc/mongodb_exporter"
state: directory
owner: root
group: root
mode: u+rwX,g+rwX,o=rX
- name: Copy the mongodb_exporter web config file
ansible.builtin.template:
src: web_config.yaml.j2
dest: /etc/mongodb_exporter/web_config.yaml
owner: root
group: root
mode: 0644
notify: restart mongodb_exporter
- name: Allow mongodb_exporter port in SELinux on RedHat OS family
community.general.seport:
ports: "{{ mongodb_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 mongodb_exporter group
ansible.builtin.group:
name: "{{ mongodb_exporter_system_group }}"
state: present
system: true
when: mongodb_exporter_system_group not in ["root", 'mongodb', 'nogroup']
- name: Create the mongodb_exporter user
ansible.builtin.user:
name: "{{ mongodb_exporter_system_user }}"
groups: "{{ mongodb_exporter_system_group }}"
append: true
shell: /usr/sbin/nologin
system: true
create_home: false
home: /
when: mongodb_exporter_system_user not in ["root", 'mongodb', 'nobody']
- name: Get binary
when:
- mongodb_exporter_binary_local_dir | length == 0
- not mongodb_exporter_skip_install
block:
- name: Download mongodb_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ mongodb_exporter_binary_url }}"
dest: "{{ mongodb_exporter_archive_path }}/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
checksum: "sha256:{{ __mongodb_exporter_checksum }}"
mode: '0644'
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
- name: Unpack mongodb_exporter binary
become: false
ansible.builtin.unarchive:
src: "{{ mongodb_exporter_archive_path }}/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "{{ mongodb_exporter_archive_path }}"
creates: "{{ mongodb_exporter_archive_path }}/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}/mongodb_exporter"
delegate_to: localhost
check_mode: false
- name: Propagate mongodb_exporter binaries
ansible.builtin.copy:
src: "{{ mongodb_exporter_archive_path }}/mongodb_exporter-{{ mongodb_exporter_version }}.linux-{{ go_arch }}/mongodb_exporter"
dest: "{{ mongodb_exporter_binary_install_dir }}/mongodb_exporter"
mode: 0755
owner: root
group: root
notify: restart mongodb_exporter
when: not ansible_check_mode
- name: Propagate locally distributed mongodb_exporter binary
ansible.builtin.copy:
src: "{{ mongodb_exporter_binary_local_dir }}/mongodb_exporter"
dest: "{{ mongodb_exporter_binary_install_dir }}/mongodb_exporter"
mode: 0755
owner: root
group: root
when:
- mongodb_exporter_binary_local_dir | length > 0
- not mongodb_exporter_skip_install
notify: restart mongodb_exporter

View file

@ -2,51 +2,49 @@
- name: Preflight - name: Preflight
ansible.builtin.include_tasks: ansible.builtin.include_tasks:
file: preflight.yml file: preflight.yml
apply:
tags:
- mongodb_exporter_install
- mongodb_exporter_configure
- mongodb_exporter_run
tags: tags:
- mongodb_exporter_install - mongodb_exporter_install
- mongodb_exporter_configure - mongodb_exporter_configure
- mongodb_exporter_run - mongodb_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: "{{ mongodb_exporter_local_cache_path }}"
- mongodb_exporter_install _common_binaries: "{{ _mongodb_exporter_binaries }}"
when: _common_binary_install_dir: "{{ mongodb_exporter_binary_install_dir }}"
( not __mongodb_exporter_is_installed.stat.exists ) or _common_binary_url: "{{ mongodb_exporter_binary_url }}"
( (__mongodb_exporter_current_version_output.stderr_lines | length > 0) _common_checksums_url: "{{ mongodb_exporter_checksums_url }}"
and (__mongodb_exporter_current_version_output.stderr_lines[1].split(" ")[1] != 'v' + mongodb_exporter_version) ) or _common_system_group: "{{ mongodb_exporter_system_group }}"
( (__mongodb_exporter_current_version_output.stdout_lines | length > 0) _common_system_user: "{{ mongodb_exporter_system_user }}"
and (__mongodb_exporter_current_version_output.stdout_lines[1].split(" ")[1] != 'v' + mongodb_exporter_version) ) or _common_config_dir: "{{ mongodb_exporter_config_dir }}"
( mongodb_exporter_binary_local_dir | length > 0 ) _common_binary_unarchive_opts: ['--strip-components=1']
tags: tags:
- mongodb_exporter_install - mongodb_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: "{{ mongodb_exporter_web_listen_address | urlsplit('port') }}"
- mongodb_exporter_configure
when: ansible_selinux.status == "enabled" when: ansible_selinux.status == "enabled"
tags: tags:
- mongodb_exporter_configure - mongodb_exporter_configure
- name: Configure - name: Configure
ansible.builtin.include_tasks: ansible.builtin.include_role:
file: configure.yml name: prometheus.prometheus._common
apply: tasks_from: configure.yml
become: true vars:
tags: _common_system_user: "{{ mongodb_exporter_system_user }}"
- mongodb_exporter_configure _common_system_group: "{{ mongodb_exporter_system_group }}"
_common_config_dir: "{{ mongodb_exporter_config_dir }}"
_common_tls_server_config: "{{ mongodb_exporter_tls_server_config }}"
_common_http_server_config: "{{ mongodb_exporter_http_server_config }}"
_common_basic_auth_users: "{{ mongodb_exporter_basic_auth_users }}"
tags: tags:
- mongodb_exporter_configure - mongodb_exporter_configure

View file

@ -1,24 +1,8 @@
--- ---
- 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:
_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: 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 +45,6 @@
- "__mongodb_exporter_cert_file.stat.exists" - "__mongodb_exporter_cert_file.stat.exists"
- "__mongodb_exporter_key_file.stat.exists" - "__mongodb_exporter_key_file.stat.exists"
- name: Check if mongodb_exporter is installed
ansible.builtin.stat:
path: "{{ mongodb_exporter_binary_install_dir }}/mongodb_exporter"
register: __mongodb_exporter_is_installed
check_mode: false
tags:
- mongodb_exporter_install
- name: Gather currently installed mongodb_exporter version (if any)
ansible.builtin.command: "{{ mongodb_exporter_binary_install_dir }}/mongodb_exporter --version"
changed_when: false
register: __mongodb_exporter_current_version_output
check_mode: false
when: __mongodb_exporter_is_installed.stat.exists
tags:
- mongodb_exporter_install
- name: Discover latest version - name: Discover latest version
ansible.builtin.set_fact: ansible.builtin.set_fact:
mongodb_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _mongodb_exporter_repo }}/releases/latest', headers=_github_api_headers, mongodb_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _mongodb_exporter_repo }}/releases/latest', headers=_github_api_headers,
@ -87,24 +54,9 @@
retries: 10 retries: 10
when: when:
- mongodb_exporter_version == "latest" - mongodb_exporter_version == "latest"
- mongodb_exporter_binary_local_dir | length == 0 tags:
- not mongodb_exporter_skip_install - mongodb_exporter
- install
- name: Get mongodb_exporter binary checksum - mongodb_exporter_install
when: - download
- mongodb_exporter_binary_local_dir | length == 0 - mongodb_exporter_download
- not mongodb_exporter_skip_install
block:
- name: Get checksum list from github
ansible.builtin.set_fact:
__mongodb_exporter_checksums: "{{ lookup('url', mongodb_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}"
run_once: true
until: __mongodb_exporter_checksums is search('linux-' + go_arch + '.tar.gz')
retries: 3
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__mongodb_exporter_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __mongodb_exporter_checksums }}"
when:
- "item.endswith('mongodb_exporter-' + mongodb_exporter_version + '.linux-' + go_arch + '.tar.gz')"

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

@ -29,7 +29,7 @@ ExecStart={{ mongodb_exporter_binary_install_dir }}/mongodb_exporter \
--collector.collstats-limit={{ mongodb_exporter_collstats_limit }} \ --collector.collstats-limit={{ mongodb_exporter_collstats_limit }} \
{% endif -%} {% endif -%}
{% if mongodb_exporter_tls_server_config | length > 0 or mongodb_exporter_http_server_config | length > 0 or mongodb_exporter_basic_auth_users | length > 0 -%} {% if mongodb_exporter_tls_server_config | length > 0 or mongodb_exporter_http_server_config | length > 0 or mongodb_exporter_basic_auth_users | length > 0 -%}
--web.config={{ mongodb_exporter_config_dir }}/web_config.yaml \ --web.config={{ mongodb_exporter_config_dir }}/web_config.yml \
{% endif -%} {% endif -%}
{% if mongodb_exporter_compatible_mode -%} {% if mongodb_exporter_compatible_mode -%}
--compatible-mode \ --compatible-mode \

View file

@ -1,18 +0,0 @@
---
{{ ansible_managed | comment }}
{% if mongodb_exporter_tls_server_config | length > 0 %}
tls_server_config:
{{ mongodb_exporter_tls_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if mongodb_exporter_http_server_config | length > 0 %}
http_server_config:
{{ mongodb_exporter_http_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if mongodb_exporter_basic_auth_users | length > 0 %}
basic_auth_users:
{% for k, v in mongodb_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: _mongodb_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) }}"
_mongodb_exporter_repo: "percona/mongodb_exporter" _mongodb_exporter_repo: "percona/mongodb_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 {} }}"
_mongodb_exporter_binaries: ['mongodb_exporter']