refactor(postgres_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:09:10 +00:00
parent 3c5d710747
commit 90086175e1
No known key found for this signature in database
GPG key ID: 75FAE37CBA8C13C2
12 changed files with 73 additions and 273 deletions

View file

@ -1,10 +1,8 @@
---
postgres_exporter_version: 0.15.0
postgres_exporter_binary_local_dir: ""
postgres_exporter_binary_url: "https://github.com/{{ _postgres_exporter_repo }}/releases/download/v{{ postgres_exporter_version }}/\
postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch }}.tar.gz"
postgres_exporter-{{ postgres_exporter_version }}.{{ ansible_system | lower }}-{{ _postgres_exporter_go_ansible_arch }}.tar.gz"
postgres_exporter_checksums_url: "https://github.com/{{ _postgres_exporter_repo }}/releases/download/v{{ postgres_exporter_version }}/sha256sums.txt"
postgres_exporter_skip_install: false
postgres_exporter_web_listen_address: "0.0.0.0:9187"
postgres_exporter_web_telemetry_path: "/metrics"
@ -33,4 +31,4 @@ postgres_exporter_system_group: "postgres-exp"
postgres_exporter_system_user: "{{ postgres_exporter_system_group }}"
# Local path to stash the archive and its extraction
postgres_exporter_archive_path: /tmp
postgres_exporter_local_cache_path: "/tmp/postgres_exporter-{{ ansible_system | lower }}-{{ _postgres_exporter_go_ansible_arch }}/{{ postgres_exporter_version }}"

View file

@ -11,18 +11,9 @@ argument_specs:
postgres_exporter_version:
description: "PostgreSQL exporter package version. Also accepts latest as parameter."
default: "0.15.0"
postgres_exporter_skip_install:
description: "PostgreSQL installation tasks gets skipped when set to true."
type: bool
default: false
postgres_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(postgres_exporter) binary is stored on the host where ansible is run."
- "This overrides the I(postgres_exporter_version) parameter"
postgres_exporter_binary_url:
description: "URL of the postgres_exporter binaries .tar.gz file"
default: "https://github.com/{{ _postgres_exporter_repo }}/download/v{{ postgres_exporter_version }}/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch }}.tar.gz"
default: "https://github.com/{{ _postgres_exporter_repo }}/releases/download/v{{ postgres_exporter_version }}/postgres_exporter-{{ postgres_exporter_version }}.{{ ansible_system | lower }}-{{ _postgres_exporter_go_ansible_arch }}.tar.gz"
postgres_exporter_checksums_url:
description: "URL of the postgres_exporter checksums file"
default: "https://github.com/{{ _postgres_exporter_repo }}/releases/download/v{{ postgres_exporter_version }}/sha256sums.txt"
@ -33,7 +24,7 @@ argument_specs:
description: "Path under which to expose metrics"
default: "/metrics"
postgres_exporter_config_dir:
description: "The path where exporter configuration is stored"
description: "Path to directory with postgres_exporter configuration"
default: "/etc/postgres_exporter"
postgres_exporter_config_file:
description: "The filename of the postgres exporter config file"
@ -89,6 +80,6 @@ argument_specs:
- "I(Advanced)"
- "PostgreSQL Exporter user"
default: "postgres-exp"
postgres_exporter_archive_path:
description: 'Local path to stash the archive and its extraction'
default: "/tmp"
postgres_exporter_local_cache_path:
description: "Local path to stash the archive and its extraction"
default: "/tmp/postgres_exporter-{{ ansible_system | lower }}-{{ _postgres_exporter_go_ansible_arch }}/{{ postgres_exporter_version }}"

View file

@ -5,7 +5,7 @@ provisioner:
inventory:
group_vars:
all:
postgres_exporter_binary_local_dir: "/tmp/postgres_exporter-linux-amd64"
postgres_exporter_local_cache_path: "/tmp/postgres_exporter-linux-amd64"
postgres_exporter_web_listen_address:
- '127.0.0.1:8080'
- '127.0.1.1:8080'
@ -20,8 +20,6 @@ provisioner:
http2: true
postgres_exporter_basic_auth_users:
randomuser: examplepassword
go_arch: amd64
postgres_exporter_version: 0.15.0
postgres_exporter_binary_url: "https://github.com/prometheus-community/postgres_exporter/releases/download/v{{\
\ postgres_exporter_version }}/postgres_exporter-{{ postgres_exporter_version\
\ }}.linux-{{ go_arch }}.tar.gz"
\ postgres_exporter_version }}/postgres_exporter-{{ postgres_exporter_version }}.linux-amd64.tar.gz"

View file

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

View file

@ -1,20 +1,19 @@
---
- name: Copy the postgres_exporter systemd service file
ansible.builtin.template:
src: postgres_exporter.service.j2
dest: /etc/systemd/system/postgres_exporter.service
owner: root
group: root
mode: '0644'
notify: restart postgres_exporter
- name: Create postgres_exporter config directory
ansible.builtin.file:
path: "{{ postgres_exporter_config_dir }}"
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: "{{ postgres_exporter_system_user }}"
_common_system_group: "{{ postgres_exporter_system_group }}"
_common_config_dir: "{{ postgres_exporter_config_dir }}"
_common_tls_server_config: "{{ postgres_exporter_tls_server_config }}"
_common_http_server_config: "{{ postgres_exporter_http_server_config }}"
_common_basic_auth_users: "{{ postgres_exporter_basic_auth_users }}"
tags:
- postgres_exporter
- configure
- postgres_exporter_configure
- name: Create postgres_exporter.yml
ansible.builtin.template:
@ -23,10 +22,21 @@
owner: root
group: '{{ postgres_exporter_system_group }}'
mode: '0640'
# 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: restart postgres_exporter
become: true
tags:
- postgres_exporter
- configure
- postgres_exporter_configure
- name: Configure via URI
when: postgres_exporter_uri | length > 0
become: true
tags:
- postgres_exporter
- configure
- postgres_exporter_configure
block:
- name: Creating file postgres_exporter_uri
ansible.builtin.copy:
@ -36,6 +46,7 @@
group: '{{ postgres_exporter_system_group }}'
mode: '0640'
notify: restart postgres_exporter
- name: Creating file postgres_exporter_user
ansible.builtin.copy:
dest: "{{ postgres_exporter_config_dir }}/postgres_exporter_user"
@ -44,6 +55,7 @@
group: '{{ postgres_exporter_system_group }}'
mode: '0640'
notify: restart postgres_exporter
- name: Creating file postgres_exporter_pass
ansible.builtin.copy:
dest: "{{ postgres_exporter_config_dir }}/postgres_exporter_pass"
@ -52,28 +64,3 @@
group: '{{ postgres_exporter_system_group }}'
mode: '0640'
notify: restart postgres_exporter
- name: Configure postgres_exporter web config
when:
( postgres_exporter_tls_server_config | length > 0 ) or
( postgres_exporter_http_server_config | length > 0 ) or
( postgres_exporter_basic_auth_users | length > 0 )
block:
- name: Copy the postgres_exporter web config file
ansible.builtin.template:
src: web_config.yaml.j2
dest: "{{ postgres_exporter_config_dir }}/web_config.yaml"
owner: root
group: '{{ postgres_exporter_system_group }}'
mode: '0640'
notify: restart postgres_exporter
- name: Allow postgres_exporter port in SELinux on RedHat OS family
community.general.seport:
ports: "{{ postgres_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,71 +0,0 @@
---
- name: Create the postgres_exporter group
ansible.builtin.group:
name: "{{ postgres_exporter_system_group }}"
state: present
system: true
when: postgres_exporter_system_group not in ["root", "postgres"]
- name: Create the postgres_exporter user
ansible.builtin.user:
name: "{{ postgres_exporter_system_user }}"
groups: "{{ postgres_exporter_system_group }}"
append: true
shell: /usr/sbin/nologin
system: true
create_home: false
home: /
when: postgres_exporter_system_user not in ["root", "postgres"]
- name: Get binary
when:
- postgres_exporter_binary_local_dir | length == 0
- not postgres_exporter_skip_install
block:
- name: Download postgres_exporter binary to local folder
become: false
ansible.builtin.get_url:
url: "{{ postgres_exporter_binary_url }}"
dest: "{{ postgres_exporter_archive_path }}/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch }}.tar.gz"
checksum: "sha256:{{ __postgres_exporter_checksum }}"
mode: '0644'
register: _download_binary
until: _download_binary is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
- name: Unpack postgres_exporter binary
become: false
ansible.builtin.unarchive:
src: "{{ postgres_exporter_archive_path }}/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch }}.tar.gz"
dest: "{{ postgres_exporter_archive_path }}"
creates: "{{ postgres_exporter_archive_path }}/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch }}/postgres_exporter"
extra_opts:
- --no-same-owner
delegate_to: localhost
check_mode: false
- name: Propagate postgres_exporter binaries
ansible.builtin.copy:
src: "{{ postgres_exporter_archive_path }}/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch }}/postgres_exporter"
dest: "{{ postgres_exporter_binary_install_dir }}/postgres_exporter"
mode: '0755'
owner: root
group: root
notify: restart postgres_exporter
when: not ansible_check_mode
- name: Propagate locally distributed postgres_exporter binary
ansible.builtin.copy:
src: "{{ postgres_exporter_binary_local_dir }}/postgres_exporter"
dest: "{{ postgres_exporter_binary_install_dir }}/postgres_exporter"
mode: '0755'
owner: root
group: root
when:
- postgres_exporter_binary_local_dir | length > 0
- not postgres_exporter_skip_install
notify: restart postgres_exporter

View file

@ -2,40 +2,34 @@
- name: Preflight
ansible.builtin.include_tasks:
file: preflight.yml
apply:
tags:
- postgres_exporter_install
- postgres_exporter_configure
- postgres_exporter_run
tags:
- postgres_exporter_install
- postgres_exporter_configure
- postgres_exporter_run
- name: Install
ansible.builtin.include_tasks:
file: install.yml
apply:
become: true
tags:
- postgres_exporter_install
when:
( not __postgres_exporter_is_installed.stat.exists ) or
( (__postgres_exporter_current_version_output.stderr_lines | length > 0)
and (__postgres_exporter_current_version_output.stderr_lines[0].split(" ")[2] != postgres_exporter_version) ) or
( (__postgres_exporter_current_version_output.stdout_lines | length > 0)
and (__postgres_exporter_current_version_output.stdout_lines[0].split(" ")[2] != postgres_exporter_version) ) or
( postgres_exporter_binary_local_dir | length > 0 )
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: install.yml
vars:
_common_local_cache_path: "{{ postgres_exporter_local_cache_path }}"
_common_binaries: "{{ _postgres_exporter_binaries }}"
_common_binary_install_dir: "{{ postgres_exporter_binary_install_dir }}"
_common_binary_url: "{{ postgres_exporter_binary_url }}"
_common_checksums_url: "{{ postgres_exporter_checksums_url }}"
_common_system_group: "{{ postgres_exporter_system_group }}"
_common_system_user: "{{ postgres_exporter_system_user }}"
_common_config_dir: "{{ postgres_exporter_config_dir }}"
_common_binary_unarchive_opts: ['--strip-components=1']
tags:
- postgres_exporter_install
- name: SELinux
ansible.builtin.include_tasks:
file: selinux.yml
apply:
become: true
tags:
- postgres_exporter_configure
ansible.builtin.include_role:
name: prometheus.prometheus._common
tasks_from: selinux.yml
vars:
_common_selinux_port: "{{ postgres_exporter_web_listen_address | urlsplit('port') }}"
when: ansible_selinux.status == "enabled"
tags:
- postgres_exporter_configure
@ -43,10 +37,6 @@
- name: Configure
ansible.builtin.include_tasks:
file: configure.yml
apply:
become: true
tags:
- postgres_exporter_configure
tags:
- postgres_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:
@ -72,23 +56,6 @@
- "__postgres_exporter_cert_file.stat.exists"
- "__postgres_exporter_key_file.stat.exists"
- name: Check if postgres_exporter is installed
ansible.builtin.stat:
path: "{{ postgres_exporter_binary_install_dir }}/postgres_exporter"
register: __postgres_exporter_is_installed
check_mode: false
tags:
- postgres_exporter_install
- name: Gather currently installed postgres_exporter version (if any)
ansible.builtin.command: "{{ postgres_exporter_binary_install_dir }}/postgres_exporter --version"
changed_when: false
register: __postgres_exporter_current_version_output
check_mode: false
when: __postgres_exporter_is_installed.stat.exists
tags:
- postgres_exporter_install
- name: Discover latest version
ansible.builtin.set_fact:
postgres_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _postgres_exporter_repo }}/releases/latest', headers=_github_api_headers,
@ -98,24 +65,9 @@
retries: 10
when:
- postgres_exporter_version == "latest"
- postgres_exporter_binary_local_dir | length == 0
- not postgres_exporter_skip_install
- name: Get postgres_exporter binary checksum
when:
- postgres_exporter_binary_local_dir | length == 0
- not postgres_exporter_skip_install
block:
- name: Get checksum list from github
ansible.builtin.set_fact:
__postgres_exporter_checksums: "{{ lookup('url', postgres_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}"
run_once: true
until: __postgres_exporter_checksums is search('linux-' + go_arch + '.tar.gz')
retries: 10
- name: "Get checksum for {{ go_arch }}"
ansible.builtin.set_fact:
__postgres_exporter_checksum: "{{ item.split(' ')[0] }}"
with_items: "{{ __postgres_exporter_checksums }}"
when:
- "('linux-' + go_arch + '.tar.gz') in item"
tags:
- postgres_exporter
- install
- postgres_exporter_install
- download
- postgres_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

@ -34,7 +34,7 @@ ExecStart={{ postgres_exporter_binary_install_dir }}/postgres_exporter \
--no-collector.{{ collector }} \
{% endfor %}
{% if postgres_exporter_tls_server_config | length > 0 or postgres_exporter_http_server_config | length > 0 or postgres_exporter_basic_auth_users | length > 0 %}
--web.config.file={{ postgres_exporter_config_dir }}/web_config.yaml \
--web.config.file={{ postgres_exporter_config_dir }}/web_config.yml \
{% endif %}
{% if postgres_exporter_version is version('0.15.0', '>=') and
postgres_exporter_web_listen_address is iterable and

View file

@ -1,18 +0,0 @@
---
{{ ansible_managed | comment }}
{% if postgres_exporter_tls_server_config | length > 0 %}
tls_server_config:
{{ postgres_exporter_tls_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if postgres_exporter_http_server_config | length > 0 %}
http_server_config:
{{ postgres_exporter_http_server_config | to_nice_yaml | indent(2, true) }}
{% endif %}
{% if postgres_exporter_basic_auth_users | length > 0 %}
basic_auth_users:
{% for k, v in postgres_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,12 +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) }}"
_postgres_exporter_go_ansible_arch: "{{ {'i386': '386',
'x86_64': 'amd64',
'aarch64': 'arm64',
'armv7l': 'armv7',
'armv6l': 'armv6'}.get(ansible_architecture, ansible_architecture) }}"
_postgres_exporter_repo: "prometheus-community/postgres_exporter"
_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}"
_postgres_exporter_binaries: ['postgres_exporter']