From 4648988bbb4ddec6edab0df6407cd93838ca9a16 Mon Sep 17 00:00:00 2001 From: anviar Date: Tue, 13 Feb 2024 12:55:07 +0300 Subject: [PATCH 1/7] feat: Add postgres_exporter role Signed-off-by: anviar --- roles/postgres_exporter/README.md | 85 ++++++++++++ roles/postgres_exporter/TROUBLESHOOTING.md | 20 +++ roles/postgres_exporter/defaults/main.yml | 33 +++++ roles/postgres_exporter/handlers/main.yml | 10 ++ .../postgres_exporter/meta/argument_specs.yml | 91 +++++++++++++ roles/postgres_exporter/meta/main.yml | 31 +++++ .../molecule/alternative/molecule.yml | 23 ++++ .../molecule/alternative/prepare.yml | 80 ++++++++++++ .../alternative/tests/test_alternative.py | 30 +++++ .../molecule/default/molecule.yml | 6 + .../molecule/default/tests/test_default.py | 73 +++++++++++ .../molecule/latest/molecule.yml | 6 + .../molecule/latest/tests/test_latest.py | 37 ++++++ roles/postgres_exporter/tasks/configure.yml | 70 ++++++++++ roles/postgres_exporter/tasks/install.yml | 71 ++++++++++ roles/postgres_exporter/tasks/main.yml | 63 +++++++++ roles/postgres_exporter/tasks/preflight.yml | 121 ++++++++++++++++++ roles/postgres_exporter/tasks/selinux.yml | 23 ++++ .../templates/postgres_exporter.service.j2 | 75 +++++++++++ .../templates/postgres_exporter.yml.j2 | 15 +++ .../templates/web_config.yaml.j2 | 18 +++ roles/postgres_exporter/test-requirements.txt | 1 + roles/postgres_exporter/vars/main.yml | 12 ++ .../runme.sh | 4 + .../runme.sh | 4 + .../runme.sh | 4 + 26 files changed, 1006 insertions(+) create mode 100644 roles/postgres_exporter/README.md create mode 100644 roles/postgres_exporter/TROUBLESHOOTING.md create mode 100644 roles/postgres_exporter/defaults/main.yml create mode 100644 roles/postgres_exporter/handlers/main.yml create mode 100644 roles/postgres_exporter/meta/argument_specs.yml create mode 100644 roles/postgres_exporter/meta/main.yml create mode 100644 roles/postgres_exporter/molecule/alternative/molecule.yml create mode 100644 roles/postgres_exporter/molecule/alternative/prepare.yml create mode 100644 roles/postgres_exporter/molecule/alternative/tests/test_alternative.py create mode 100644 roles/postgres_exporter/molecule/default/molecule.yml create mode 100644 roles/postgres_exporter/molecule/default/tests/test_default.py create mode 100644 roles/postgres_exporter/molecule/latest/molecule.yml create mode 100644 roles/postgres_exporter/molecule/latest/tests/test_latest.py create mode 100644 roles/postgres_exporter/tasks/configure.yml create mode 100644 roles/postgres_exporter/tasks/install.yml create mode 100644 roles/postgres_exporter/tasks/main.yml create mode 100644 roles/postgres_exporter/tasks/preflight.yml create mode 100644 roles/postgres_exporter/tasks/selinux.yml create mode 100644 roles/postgres_exporter/templates/postgres_exporter.service.j2 create mode 100644 roles/postgres_exporter/templates/postgres_exporter.yml.j2 create mode 100644 roles/postgres_exporter/templates/web_config.yaml.j2 create mode 100644 roles/postgres_exporter/test-requirements.txt create mode 100644 roles/postgres_exporter/vars/main.yml create mode 100755 tests/integration/targets/molecule-postgres_exporter-alternative/runme.sh create mode 100755 tests/integration/targets/molecule-postgres_exporter-default/runme.sh create mode 100755 tests/integration/targets/molecule-postgres_exporter-latest/runme.sh diff --git a/roles/postgres_exporter/README.md b/roles/postgres_exporter/README.md new file mode 100644 index 000000000..70aabc012 --- /dev/null +++ b/roles/postgres_exporter/README.md @@ -0,0 +1,85 @@ +

graph logo

+ +# Ansible Role: PostgreSQL exporter + + +Deploy prometheus [postgresql exporter](https://github.com/prometheus-community/postgres_exporter) using ansible. + +## Requirements + +- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it) +- gnu-tar on Mac deployer host (`brew install gnu-tar`) +- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`) + +## Role Variables +All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml). +Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/postgres_exporter_role.html) for description and default values of the variables. + +## Example + +### Playbook + +Use it in a playbook as follows: +```yaml +- hosts: all + collections: + - prometheus.prometheus + roles: + - prometheus.prometheus.postgres_exporter +``` + +### TLS config + +Before running postgres_exporter role, the user needs to provision their own certificate and key. +```yaml +- hosts: all + pre_tasks: + - name: Create postgres_exporter cert dir + ansible.builtin.file: + path: "/etc/postgres_exporter" + state: directory + owner: root + group: root + + - name: Create cert and key + community.crypto.x509_certificate: + path: /etc/postgres_exporter/tls.cert + csr_path: /etc/postgres_exporter/tls.csr + privatekey_path: /etc/postgres_exporter/tls.key + provider: selfsigned + collections: + - prometheus.prometheus + roles: + - prometheus.prometheus.postgres_exporter + vars: + postgres_exporter_tls_server_config: + cert_file: /etc/postgres_exporter/tls.cert + key_file: /etc/postgres_exporter/tls.key + postgres_exporter_basic_auth_users: + randomuser: examplepassword +``` + + +### Demo site + +We provide an example site that demonstrates a full monitoring solution based on prometheus and grafana. The repository with code and links to running instances is [available on github](https://github.com/prometheus/demo-site) and the site is hosted on [DigitalOcean](https://digitalocean.com). + +## Local Testing + +The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable for your system. Running your tests is as simple as executing `molecule test`. + +## Continuous Integration + +Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have quite a large test matrix which can take more time than local testing, so please be patient. + +## Contributing + +See [contributor guideline](CONTRIBUTING.md). + +## Troubleshooting + +See [troubleshooting](TROUBLESHOOTING.md). + +## License + +This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details. diff --git a/roles/postgres_exporter/TROUBLESHOOTING.md b/roles/postgres_exporter/TROUBLESHOOTING.md new file mode 100644 index 000000000..0bece12af --- /dev/null +++ b/roles/postgres_exporter/TROUBLESHOOTING.md @@ -0,0 +1,20 @@ +# Troubleshooting + +## Bad requests (HTTP 400) + +This role downloads checksums from the Github project to verify the integrity of artifacts installed on your servers. When downloading the checksums, a "bad request" error might occur. + +This happens in environments which (knowningly or unknowling) use the [netrc mechanism](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) to auto-login into servers. + +Unless netrc is needed by your playbook and ansible roles, please unset the var like so: + +``` +$ NETRC= ansible-playbook ... +``` + +Or: + +``` +$ export NETRC= +$ ansible-playbook ... +``` diff --git a/roles/postgres_exporter/defaults/main.yml b/roles/postgres_exporter/defaults/main.yml new file mode 100644 index 000000000..f1d41811d --- /dev/null +++ b/roles/postgres_exporter/defaults/main.yml @@ -0,0 +1,33 @@ +--- +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_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" + +postgres_exporter_config_file: "postgres_exporter.yml" + +postgres_exporter_name: "postgresql:///postgres?host=/var/run/postgresql" +# postgres_exporter_uri: "localhost?sslmode=disable" +# postgres_exporter_username: +# postgres_exporter_passpassword: + +postgres_exporter_tls_server_config: {} + +postgres_exporter_http_server_config: {} + +postgres_exporter_basic_auth_users: {} + +postgres_exporter_enabled_collectors: [] + +postgres_exporter_disabled_collectors: [] + +# Internal variables. +postgres_exporter_binary_install_dir: "/usr/local/bin" +postgres_exporter_config_dir: "/etc/postgres_exporter" +postgres_exporter_system_group: "postgres-exp" +postgres_exporter_system_user: "{{ postgres_exporter_system_group }}" diff --git a/roles/postgres_exporter/handlers/main.yml b/roles/postgres_exporter/handlers/main.yml new file mode 100644 index 000000000..8c7880ba8 --- /dev/null +++ b/roles/postgres_exporter/handlers/main.yml @@ -0,0 +1,10 @@ +--- +- name: Restart postgres_exporter + listen: "restart postgres_exporter" + become: true + ansible.builtin.systemd: + daemon_reload: true + name: postgres_exporter + state: restarted + when: + - not ansible_check_mode diff --git a/roles/postgres_exporter/meta/argument_specs.yml b/roles/postgres_exporter/meta/argument_specs.yml new file mode 100644 index 000000000..3bad0e48c --- /dev/null +++ b/roles/postgres_exporter/meta/argument_specs.yml @@ -0,0 +1,91 @@ +--- +# yamllint disable rule:line-length +argument_specs: + main: + short_description: "Prometheus PostgreSQL Exporter" + description: + - "Deploy prometheus L(postgres_exporter,https://github.com/prometheus-community/postgres_exporter) using ansible" + author: + - "Prometheus Community" + options: + 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" + 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" + postgres_exporter_web_listen_address: + description: "Address on which postgres_exporter will listen" + default: "0.0.0.0:9187" + postgres_exporter_web_telemetry_path: + description: "Path under which to expose metrics" + default: "/metrics" + postgres_exporter_config_dir: + description: "The path where exporter configuration is stored" + default: "/etc/postgres_exporter" + postgres_exporter_config_file: + description: "The filename of the postgres exporter config file" + default: "/etc/postgres_exporter/postgres_exporter.yml" + postgres_exporter_name: + description: "The target PostgreSQL L(URI, https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS)" + default: "postgresql:///postgres?host=/var/run/postgresql" + postgres_exporter_uri: + description: "The target PostgreSQL, alternative format, single target only is supported. You should set postgres_exporter_name to empty string to use it. Read more L(here,https://github.com/prometheus-community/postgres_exporter?tab=readme-ov-file#environment-variables)" + postgres_exporter_username: + description: "The username for PostgreSQL login, required for postgres_exporter_uri" + default: "exporter" + postgres_exporter_password: + description: "The password for PostgreSQL password, required for postgres_exporter_uri" + default: "secret" + postgres_exporter_enabled_collectors: + description: + - "List of dicts defining additionally enabled collectors and their configuration." + - "It adds collectors to L(those enabled by default,https://github.com/prometheus-community/postgres_exporter?tab=readme-ov-file#flags)." + type: "list" + default: [] + postgres_exporter_disabled_collectors: + description: + - "List of disabled collectors." + - "By default postgres_exporter disables collectors listed L(here,https://github.com/prometheus-community/postgres_exporter?tab=readme-ov-file#flags)." + type: "list" + elements: "str" + postgres_exporter_tls_server_config: + description: + - "Configuration for TLS authentication." + - "Keys and values are the same as in L(prometheus docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)." + type: "dict" + postgres_exporter_http_server_config: + description: + - "Config for HTTP/2 support." + - "Keys and values are the same as in L(prometheus docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)." + type: "dict" + postgres_exporter_basic_auth_users: + description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt." + type: "dict" + postgres_exporter_binary_install_dir: + description: + - "I(Advanced)" + - "Directory to install postgres_exporter binary" + default: "/usr/local/bin" + postgres_exporter_system_group: + description: + - "I(Advanced)" + - "System group for PostgreSQL Exporter" + default: "postgres-exp" + postgres_exporter_system_user: + description: + - "I(Advanced)" + - "PostgreSQL Exporter user" + default: "postgres-exp" diff --git a/roles/postgres_exporter/meta/main.yml b/roles/postgres_exporter/meta/main.yml new file mode 100644 index 000000000..5fa587908 --- /dev/null +++ b/roles/postgres_exporter/meta/main.yml @@ -0,0 +1,31 @@ +--- +galaxy_info: + author: "Prometheus Community" + description: "Prometheus PostgreSQL Exporter" + license: "Apache" + min_ansible_version: "2.9" + platforms: + - name: "Ubuntu" + versions: + - "focal" + - "jammy" + - name: "Debian" + versions: + - "bullseye" + - "buster" + - name: "EL" + versions: + - "7" + - "8" + - "9" + - name: "Fedora" + versions: + - "37" + - '38' + galaxy_tags: + - "monitoring" + - "prometheus" + - "exporter" + - "metrics" + - "system" + - "postgresql" diff --git a/roles/postgres_exporter/molecule/alternative/molecule.yml b/roles/postgres_exporter/molecule/alternative/molecule.yml new file mode 100644 index 000000000..4a8d5da49 --- /dev/null +++ b/roles/postgres_exporter/molecule/alternative/molecule.yml @@ -0,0 +1,23 @@ +--- +provisioner: + inventory: + group_vars: + all: + postgres_exporter_binary_local_dir: "/tmp/postgres_exporter-linux-amd64" + postgres_exporter_web_listen_address: + - '127.0.0.1:8080' + - '127.0.1.1:8080' + postgres_exporter_enabled_collectors: + - postmaster + postgres_exporter_disabled_collectors: + - replication + + postgres_exporter_tls_server_config: + cert_file: /etc/postgres_exporter/tls.cert + key_file: /etc/postgres_exporter/tls.key + postgres_exporter_http_server_config: + http2: true + postgres_exporter_basic_auth_users: + randomuser: examplepassword + go_arch: amd64 + postgres_exporter_version: 0.15.0 diff --git a/roles/postgres_exporter/molecule/alternative/prepare.yml b/roles/postgres_exporter/molecule/alternative/prepare.yml new file mode 100644 index 000000000..c67573c0d --- /dev/null +++ b/roles/postgres_exporter/molecule/alternative/prepare.yml @@ -0,0 +1,80 @@ +--- +- name: Run local preparation + hosts: localhost + gather_facts: false + tasks: + - name: Download postgres_exporter binary to local folder + become: false + ansible.builtin.get_url: + url: "https://github.com/prometheus/postgres_exporter/releases/download/v{{\ + \ postgres_exporter_version }}/postgres_exporter-{{ postgres_exporter_version\ + \ }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch\ + \ }}.tar.gz" + mode: 0644 + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + check_mode: false + + - name: Unpack postgres_exporter binary + become: false + ansible.builtin.unarchive: + src: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch\ + \ }}.tar.gz" + dest: "/tmp" + creates: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch\ + \ }}/postgres_exporter" + check_mode: false + + - name: Link to postgres_exporter binaries directory + become: false + ansible.builtin.file: + src: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-amd64" + dest: "/tmp/postgres_exporter-linux-amd64" + state: link + check_mode: false + + - name: Install pyOpenSSL for certificate generation + ansible.builtin.pip: + name: "pyOpenSSL" + + - name: Create private key + community.crypto.openssl_privatekey: + path: "/tmp/tls.key" + + - name: Create CSR + community.crypto.openssl_csr: + path: "/tmp/tls.csr" + privatekey_path: "/tmp/tls.key" + + - name: Create certificate + community.crypto.x509_certificate: + path: "/tmp/tls.cert" + csr_path: "/tmp/tls.csr" + privatekey_path: "/tmp/tls.key" + provider: selfsigned + +- name: Run target preparation + hosts: all + any_errors_fatal: true + tasks: + - name: Create postgres_exporter cert dir + ansible.builtin.file: + path: "{{ postgres_exporter_tls_server_config.cert_file | dirname }}" + state: directory + owner: root + group: root + mode: u+rwX,g+rwX,o=rX + + - name: Copy cert and key + ansible.builtin.copy: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + mode: "{{ item.mode | default('0644') }}" + loop: + - src: "/tmp/tls.cert" + dest: "{{ postgres_exporter_tls_server_config.cert_file }}" + - src: "/tmp/tls.key" + dest: "{{ postgres_exporter_tls_server_config.key_file }}" diff --git a/roles/postgres_exporter/molecule/alternative/tests/test_alternative.py b/roles/postgres_exporter/molecule/alternative/tests/test_alternative.py new file mode 100644 index 000000000..49920c20d --- /dev/null +++ b/roles/postgres_exporter/molecule/alternative/tests/test_alternative.py @@ -0,0 +1,30 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner +import pytest + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_service(host): + s = host.service("postgres_exporter") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u postgres_exporter --since "1 hour ago"') + print("\n==== journalctl -u postgres_exporter Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +@pytest.mark.parametrize("sockets", [ + "tcp://127.0.0.1:9187", + "tcp://127.0.1.1:9187", +]) +def test_socket(host, sockets): + assert host.socket(sockets).is_listening diff --git a/roles/postgres_exporter/molecule/default/molecule.yml b/roles/postgres_exporter/molecule/default/molecule.yml new file mode 100644 index 000000000..662306af3 --- /dev/null +++ b/roles/postgres_exporter/molecule/default/molecule.yml @@ -0,0 +1,6 @@ +--- +provisioner: + inventory: + group_vars: + all: + postgres_exporter_web_listen_address: "127.0.0.1:9187" diff --git a/roles/postgres_exporter/molecule/default/tests/test_default.py b/roles/postgres_exporter/molecule/default/tests/test_default.py new file mode 100644 index 000000000..d497d5b13 --- /dev/null +++ b/roles/postgres_exporter/molecule/default/tests/test_default.py @@ -0,0 +1,73 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_directories(host): + dirs = [ + "/etc/postgres_exporter" + ] + for dir in dirs: + d = host.file(dir) + assert d.is_directory + assert d.exists + + +def test_files(host): + files = [ + "/etc/systemd/system/postgres_exporter.service", + "/usr/local/bin/postgres_exporter" + ] + for file in files: + f = host.file(file) + assert f.exists + assert f.is_file + + +def test_permissions_didnt_change(host): + dirs = [ + "/etc", + "/root", + "/usr", + "/var" + ] + for file in dirs: + f = host.file(file) + assert f.exists + assert f.is_directory + assert f.user == "root" + assert f.group == "root" + + +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): + s = host.service("postgres_exporter") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u postgres_exporter --since "1 hour ago"') + print("\n==== journalctl -u postgres_exporter Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +def test_socket(host): + sockets = [ + "tcp://127.0.0.1:9187" + ] + for socket in sockets: + s = host.socket(socket) + assert s.is_listening diff --git a/roles/postgres_exporter/molecule/latest/molecule.yml b/roles/postgres_exporter/molecule/latest/molecule.yml new file mode 100644 index 000000000..f9c1323d8 --- /dev/null +++ b/roles/postgres_exporter/molecule/latest/molecule.yml @@ -0,0 +1,6 @@ +--- +provisioner: + inventory: + group_vars: + all: + postgres_exporter_version: latest diff --git a/roles/postgres_exporter/molecule/latest/tests/test_latest.py b/roles/postgres_exporter/molecule/latest/tests/test_latest.py new file mode 100644 index 000000000..2d8c9d499 --- /dev/null +++ b/roles/postgres_exporter/molecule/latest/tests/test_latest.py @@ -0,0 +1,37 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner +import pytest + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +@pytest.mark.parametrize("files", [ + "/etc/systemd/system/postgres_exporter.service", + "/usr/local/bin/postgres_exporter" +]) +def test_files(host, files): + f = host.file(files) + assert f.exists + assert f.is_file + + +def test_service(host): + s = host.service("postgres_exporter") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u postgres_exporter --since "1 hour ago"') + print("\n==== journalctl -u postgres_exporter Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +def test_socket(host): + s = host.socket("tcp://0.0.0.0:9187") + assert s.is_listening diff --git a/roles/postgres_exporter/tasks/configure.yml b/roles/postgres_exporter/tasks/configure.yml new file mode 100644 index 000000000..5dfa8b718 --- /dev/null +++ b/roles/postgres_exporter/tasks/configure.yml @@ -0,0 +1,70 @@ +--- +- 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: Create postgres_exporter.yml + ansible.builtin.template: + src: postgres_exporter.yml.j2 + dest: "{{ postgres_exporter_config_dir }}/{{ postgres_exporter_config_file }}" + owner: root + group: '{{ postgres_exporter_system_group }}' + mode: '0640' + # no_log: "{{ false if (lookup('env', 'CI')) or (lookup('env', 'MOLECULE_PROVISIONER_NAME')) else true }}" + notify: restart postgres_exporter +- block: + - name: Creating file postgres_exporter_uri + copy: + dest: "{{ postgres_exporter_config_dir }}/postgres_exporter_uri" + content: "{{ postgres_exporter_uri }}" + notify: restart postgres_exporter + - name: Creating file postgres_exporter_user + copy: + dest: "{{ postgres_exporter_config_dir }}/postgres_exporter_user" + content: "{{ postgres_exporter_username }}" + notify: restart postgres_exporter + - name: Creating file postgres_exporter_pass + copy: + dest: "{{ postgres_exporter_config_dir }}/postgres_exporter_pass" + content: "{{ postgres_exporter_password }}" + notify: restart postgres_exporter + when: + - postgres_exporter_uri is defined + +- 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" diff --git a/roles/postgres_exporter/tasks/install.yml b/roles/postgres_exporter/tasks/install.yml new file mode 100644 index 000000000..fcde13764 --- /dev/null +++ b/roles/postgres_exporter/tasks/install.yml @@ -0,0 +1,71 @@ +--- +- 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: "/tmp/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: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp" + creates: "/tmp/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: "/tmp/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 diff --git a/roles/postgres_exporter/tasks/main.yml b/roles/postgres_exporter/tasks/main.yml new file mode 100644 index 000000000..e1060efaa --- /dev/null +++ b/roles/postgres_exporter/tasks/main.yml @@ -0,0 +1,63 @@ +--- +- 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 ) + tags: + - postgres_exporter_install + +- name: SELinux + ansible.builtin.include_tasks: + file: selinux.yml + apply: + become: true + tags: + - postgres_exporter_configure + when: ansible_selinux.status == "enabled" + tags: + - postgres_exporter_configure + +- name: Configure + ansible.builtin.include_tasks: + file: configure.yml + apply: + become: true + tags: + - postgres_exporter_configure + tags: + - postgres_exporter_configure + +- name: Ensure PostgreSQL Exporter is enabled on boot + become: true + ansible.builtin.systemd: + daemon_reload: true + name: postgres_exporter + enabled: true + state: started + when: + - not ansible_check_mode + tags: + - postgres_exporter_run diff --git a/roles/postgres_exporter/tasks/preflight.yml b/roles/postgres_exporter/tasks/preflight.yml new file mode 100644 index 000000000..38b09d338 --- /dev/null +++ b/roles/postgres_exporter/tasks/preflight.yml @@ -0,0 +1,121 @@ +--- +- 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: Assert that used version supports listen address type + ansible.builtin.assert: + that: + - >- + postgres_exporter_web_listen_address is string + or + ( + postgres_exporter_version is version('0.15.0', '>=') and + postgres_exporter_web_listen_address | type_debug == "list" + ) + +- name: Naive assertion of proper listen address + ansible.builtin.assert: + that: + - >- + [postgres_exporter_web_listen_address] | + flatten | + reject('match', '.+:\\d+$') | + list | + length == 0 + +- name: Assert collectors are not both disabled and enabled at the same time + ansible.builtin.assert: + that: + - "item not in postgres_exporter_enabled_collectors" + with_items: "{{ postgres_exporter_disabled_collectors }}" + +- name: Assert that TLS config is correct + when: postgres_exporter_tls_server_config | length > 0 + block: + - name: Assert that TLS key and cert path are set + ansible.builtin.assert: + that: + - "postgres_exporter_tls_server_config.cert_file is defined" + - "postgres_exporter_tls_server_config.key_file is defined" + + - name: Check existence of TLS cert file + ansible.builtin.stat: + path: "{{ postgres_exporter_tls_server_config.cert_file }}" + register: __postgres_exporter_cert_file + + - name: Check existence of TLS key file + ansible.builtin.stat: + path: "{{ postgres_exporter_tls_server_config.key_file }}" + register: __postgres_exporter_key_file + + - name: Assert that TLS key and cert are present + ansible.builtin.assert: + that: + - "__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, + split_lines=False) | from_json).get('tag_name') | replace('v', '') }}" + run_once: true + until: postgres_exporter_version is version('0.0.0', '>=') + 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" diff --git a/roles/postgres_exporter/tasks/selinux.yml b/roles/postgres_exporter/tasks/selinux.yml new file mode 100644 index 000000000..754cbd0a2 --- /dev/null +++ b/roles/postgres_exporter/tasks/selinux.yml @@ -0,0 +1,23 @@ +--- +- 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" diff --git a/roles/postgres_exporter/templates/postgres_exporter.service.j2 b/roles/postgres_exporter/templates/postgres_exporter.service.j2 new file mode 100644 index 000000000..ef5268ea0 --- /dev/null +++ b/roles/postgres_exporter/templates/postgres_exporter.service.j2 @@ -0,0 +1,75 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Prometheus PostgreSQL Exporter +After=network-online.target + +[Service] +Type=simple +User={{ postgres_exporter_system_user }} +Group={{ postgres_exporter_system_group }} +{% if postgres_exporter_name is defined and postgres_exporter_name !="" %} +Environment=DATA_SOURCE_NAME={{ postgres_exporter_name}} +{% elif postgres_exporter_uri is defined %} +Environment=DATA_SOURCE_URI_FILE={{ postgres_exporter_config_dir }}/postgres_exporter_uri +{% if postgres_exporter_username is defined %} +Environment=DATA_SOURCE_USER_FILE={{ postgres_exporter_config_dir }}/postgres_exporter_user +{% endif %} +{% if postgres_exporter_password is defined %} +Environment=DATA_SOURCE_PASS_FILE={{ postgres_exporter_config_dir }}/postgres_exporter_pass +{% endif %}{% endif %} +ExecStart={{ postgres_exporter_binary_install_dir }}/postgres_exporter \ +{% for collector in postgres_exporter_enabled_collectors -%} +{% if not collector is mapping %} + --collector.{{ collector }} \ +{% else -%} +{% set name, options = (collector.items()|list)[0] -%} + --collector.{{ name }} \ +{% for k,v in options|dictsort %} + --collector.{{ name }}.{{ k }}={{ v | quote }} \ +{% endfor -%} +{% endif -%} +{% endfor -%} +{% for collector in postgres_exporter_disabled_collectors %} + --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 \ +{% endif %} +{% if postgres_exporter_version is version('0.15.0', '>=') and + postgres_exporter_web_listen_address is iterable and + postgres_exporter_web_listen_address is not mapping and + postgres_exporter_web_listen_address is not string %} +{% for address in postgres_exporter_web_listen_address %} + --web.listen-address={{ address }} \ +{% endfor %} +{% else %} + --web.listen-address={{ postgres_exporter_web_listen_address }} \ +{% endif %} + --config.file={{ postgres_exporter_config_dir }}/{{ postgres_exporter_config_file }} \ + --web.telemetry-path={{ postgres_exporter_web_telemetry_path }} + + +SyslogIdentifier=postgres_exporter +Restart=always +RestartSec=1 +StartLimitInterval=0 + +{% set protect_home = 'yes' %} +{% for m in ansible_mounts if m.mount.startswith('/home') %} +{% set protect_home = 'read-only' %} +{% endfor %} +ProtectHome={{ protect_home }} +NoNewPrivileges=yes + +{% if (ansible_facts.packages.systemd | first).version is version('232', '>=') %} +ProtectSystem=strict +ProtectControlGroups=true +ProtectKernelModules=true +ProtectKernelTunables=yes +{% else %} +ProtectSystem=full +{% endif %} + +[Install] +WantedBy=multi-user.target diff --git a/roles/postgres_exporter/templates/postgres_exporter.yml.j2 b/roles/postgres_exporter/templates/postgres_exporter.yml.j2 new file mode 100644 index 000000000..1e8afa501 --- /dev/null +++ b/roles/postgres_exporter/templates/postgres_exporter.yml.j2 @@ -0,0 +1,15 @@ +{{ ansible_managed | comment }} + +{% if postgres_exporter_uri is defined %} +auth_modules: + foo1: # Set this to any name you want + type: userpass + userpass: + username: {{ postgres_exporter_username }} + password: {{ postgres_exporter_password }} + options: + # options become key=value parameters of the DSN + sslmode: disable +{% else %} +auth_modules: {} +{% endif %} diff --git a/roles/postgres_exporter/templates/web_config.yaml.j2 b/roles/postgres_exporter/templates/web_config.yaml.j2 new file mode 100644 index 000000000..669be69f9 --- /dev/null +++ b/roles/postgres_exporter/templates/web_config.yaml.j2 @@ -0,0 +1,18 @@ +--- +{{ 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 %} diff --git a/roles/postgres_exporter/test-requirements.txt b/roles/postgres_exporter/test-requirements.txt new file mode 100644 index 000000000..7f0b6e759 --- /dev/null +++ b/roles/postgres_exporter/test-requirements.txt @@ -0,0 +1 @@ +bcrypt diff --git a/roles/postgres_exporter/vars/main.yml b/roles/postgres_exporter/vars/main.yml new file mode 100644 index 000000000..44a2fdf41 --- /dev/null +++ b/roles/postgres_exporter/vars/main.yml @@ -0,0 +1,12 @@ +--- +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_repo: "prometheus-community/postgres_exporter" +_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}" diff --git a/tests/integration/targets/molecule-postgres_exporter-alternative/runme.sh b/tests/integration/targets/molecule-postgres_exporter-alternative/runme.sh new file mode 100755 index 000000000..d094c3e1b --- /dev/null +++ b/tests/integration/targets/molecule-postgres_exporter-alternative/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" diff --git a/tests/integration/targets/molecule-postgres_exporter-default/runme.sh b/tests/integration/targets/molecule-postgres_exporter-default/runme.sh new file mode 100755 index 000000000..d094c3e1b --- /dev/null +++ b/tests/integration/targets/molecule-postgres_exporter-default/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" diff --git a/tests/integration/targets/molecule-postgres_exporter-latest/runme.sh b/tests/integration/targets/molecule-postgres_exporter-latest/runme.sh new file mode 100755 index 000000000..d094c3e1b --- /dev/null +++ b/tests/integration/targets/molecule-postgres_exporter-latest/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" From 0b1e4d1f62b0bcfb5c38bbfb3d5578edd0409588 Mon Sep 17 00:00:00 2001 From: anviar Date: Tue, 13 Feb 2024 13:45:59 +0300 Subject: [PATCH 2/7] test: postgres_exporter fix linting errors Signed-off-by: anviar --- roles/postgres_exporter/defaults/main.yml | 6 +++--- roles/postgres_exporter/tasks/configure.yml | 21 +++++++++++++------ .../templates/postgres_exporter.service.j2 | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/roles/postgres_exporter/defaults/main.yml b/roles/postgres_exporter/defaults/main.yml index f1d41811d..6b0e4e470 100644 --- a/roles/postgres_exporter/defaults/main.yml +++ b/roles/postgres_exporter/defaults/main.yml @@ -12,9 +12,9 @@ postgres_exporter_web_telemetry_path: "/metrics" postgres_exporter_config_file: "postgres_exporter.yml" postgres_exporter_name: "postgresql:///postgres?host=/var/run/postgresql" -# postgres_exporter_uri: "localhost?sslmode=disable" -# postgres_exporter_username: -# postgres_exporter_passpassword: +postgres_exporter_uri: "" +postgres_exporter_username: "" +postgres_exporter_password: "" postgres_exporter_tls_server_config: {} diff --git a/roles/postgres_exporter/tasks/configure.yml b/roles/postgres_exporter/tasks/configure.yml index 5dfa8b718..0b23efc49 100644 --- a/roles/postgres_exporter/tasks/configure.yml +++ b/roles/postgres_exporter/tasks/configure.yml @@ -25,24 +25,33 @@ mode: '0640' # no_log: "{{ false if (lookup('env', 'CI')) or (lookup('env', 'MOLECULE_PROVISIONER_NAME')) else true }}" notify: restart postgres_exporter -- block: +- name: Configure via URI + when: postgres_exporter_uri | length > 0 + block: - name: Creating file postgres_exporter_uri - copy: + ansible.builtin.copy: dest: "{{ postgres_exporter_config_dir }}/postgres_exporter_uri" content: "{{ postgres_exporter_uri }}" + owner: root + group: '{{ postgres_exporter_system_group }}' + mode: '0640' notify: restart postgres_exporter - name: Creating file postgres_exporter_user - copy: + ansible.builtin.copy: dest: "{{ postgres_exporter_config_dir }}/postgres_exporter_user" content: "{{ postgres_exporter_username }}" + owner: root + group: '{{ postgres_exporter_system_group }}' + mode: '0640' notify: restart postgres_exporter - name: Creating file postgres_exporter_pass - copy: + ansible.builtin.copy: dest: "{{ postgres_exporter_config_dir }}/postgres_exporter_pass" content: "{{ postgres_exporter_password }}" + owner: root + group: '{{ postgres_exporter_system_group }}' + mode: '0640' notify: restart postgres_exporter - when: - - postgres_exporter_uri is defined - name: Configure postgres_exporter web config when: diff --git a/roles/postgres_exporter/templates/postgres_exporter.service.j2 b/roles/postgres_exporter/templates/postgres_exporter.service.j2 index ef5268ea0..4ffb8cd8c 100644 --- a/roles/postgres_exporter/templates/postgres_exporter.service.j2 +++ b/roles/postgres_exporter/templates/postgres_exporter.service.j2 @@ -8,7 +8,7 @@ After=network-online.target Type=simple User={{ postgres_exporter_system_user }} Group={{ postgres_exporter_system_group }} -{% if postgres_exporter_name is defined and postgres_exporter_name !="" %} +{% if postgres_exporter_name | length > 0 %} Environment=DATA_SOURCE_NAME={{ postgres_exporter_name}} {% elif postgres_exporter_uri is defined %} Environment=DATA_SOURCE_URI_FILE={{ postgres_exporter_config_dir }}/postgres_exporter_uri From a56e3b480238e0f293a60866562a30e2aa0b3f31 Mon Sep 17 00:00:00 2001 From: anviar Date: Tue, 13 Feb 2024 16:05:32 +0300 Subject: [PATCH 3/7] test: postgres_exporter fix url for alternative scenario Signed-off-by: anviar --- roles/postgres_exporter/molecule/alternative/prepare.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/postgres_exporter/molecule/alternative/prepare.yml b/roles/postgres_exporter/molecule/alternative/prepare.yml index c67573c0d..db3e281d6 100644 --- a/roles/postgres_exporter/molecule/alternative/prepare.yml +++ b/roles/postgres_exporter/molecule/alternative/prepare.yml @@ -6,7 +6,7 @@ - name: Download postgres_exporter binary to local folder become: false ansible.builtin.get_url: - url: "https://github.com/prometheus/postgres_exporter/releases/download/v{{\ + url: "https://github.com/prometheus-community/postgres_exporter/releases/download/v{{\ \ postgres_exporter_version }}/postgres_exporter-{{ postgres_exporter_version\ \ }}.linux-{{ go_arch }}.tar.gz" dest: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch\ From cf1f04dd633755af7ca81630a1d46030f5f3a2cc Mon Sep 17 00:00:00 2001 From: anviar Date: Tue, 13 Feb 2024 16:19:21 +0300 Subject: [PATCH 4/7] test: postgres_exporter fix port in alternative scenario Signed-off-by: anviar --- .../molecule/alternative/tests/test_alternative.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roles/postgres_exporter/molecule/alternative/tests/test_alternative.py b/roles/postgres_exporter/molecule/alternative/tests/test_alternative.py index 49920c20d..113fd33c7 100644 --- a/roles/postgres_exporter/molecule/alternative/tests/test_alternative.py +++ b/roles/postgres_exporter/molecule/alternative/tests/test_alternative.py @@ -23,8 +23,8 @@ def test_service(host): @pytest.mark.parametrize("sockets", [ - "tcp://127.0.0.1:9187", - "tcp://127.0.1.1:9187", + "tcp://127.0.0.1:8080", + "tcp://127.0.1.1:8080", ]) def test_socket(host, sockets): assert host.socket(sockets).is_listening From 29bb493b1d36130dd0a18d541d50f7526e0baa46 Mon Sep 17 00:00:00 2001 From: anviar Date: Fri, 23 Feb 2024 13:33:13 +0300 Subject: [PATCH 5/7] feat: Add bind_exporter role Signed-off-by: anviar --- roles/bind_exporter/README.md | 85 +++++++++++++ roles/bind_exporter/TROUBLESHOOTING.md | 20 +++ roles/bind_exporter/defaults/main.yml | 26 ++++ roles/bind_exporter/handlers/main.yml | 10 ++ roles/bind_exporter/meta/argument_specs.yml | 90 ++++++++++++++ roles/bind_exporter/meta/main.yml | 33 +++++ .../molecule/alternative/molecule.yml | 21 ++++ .../molecule/alternative/prepare.yml | 80 ++++++++++++ .../alternative/tests/test_alternative.py | 30 +++++ .../molecule/default/molecule.yml | 6 + .../molecule/default/tests/test_default.py | 73 +++++++++++ .../molecule/latest/molecule.yml | 6 + .../molecule/latest/tests/test_latest.py | 37 ++++++ roles/bind_exporter/tasks/configure.yml | 42 +++++++ roles/bind_exporter/tasks/install.yml | 71 +++++++++++ roles/bind_exporter/tasks/main.yml | 63 ++++++++++ roles/bind_exporter/tasks/preflight.yml | 115 ++++++++++++++++++ roles/bind_exporter/tasks/selinux.yml | 23 ++++ .../templates/bind_exporter.service.j2 | 48 ++++++++ .../templates/web_config.yaml.j2 | 18 +++ roles/bind_exporter/test-requirements.txt | 1 + roles/bind_exporter/vars/main.yml | 12 ++ 22 files changed, 910 insertions(+) create mode 100644 roles/bind_exporter/README.md create mode 100644 roles/bind_exporter/TROUBLESHOOTING.md create mode 100644 roles/bind_exporter/defaults/main.yml create mode 100644 roles/bind_exporter/handlers/main.yml create mode 100644 roles/bind_exporter/meta/argument_specs.yml create mode 100644 roles/bind_exporter/meta/main.yml create mode 100644 roles/bind_exporter/molecule/alternative/molecule.yml create mode 100644 roles/bind_exporter/molecule/alternative/prepare.yml create mode 100644 roles/bind_exporter/molecule/alternative/tests/test_alternative.py create mode 100644 roles/bind_exporter/molecule/default/molecule.yml create mode 100644 roles/bind_exporter/molecule/default/tests/test_default.py create mode 100644 roles/bind_exporter/molecule/latest/molecule.yml create mode 100644 roles/bind_exporter/molecule/latest/tests/test_latest.py create mode 100644 roles/bind_exporter/tasks/configure.yml create mode 100644 roles/bind_exporter/tasks/install.yml create mode 100644 roles/bind_exporter/tasks/main.yml create mode 100644 roles/bind_exporter/tasks/preflight.yml create mode 100644 roles/bind_exporter/tasks/selinux.yml create mode 100644 roles/bind_exporter/templates/bind_exporter.service.j2 create mode 100644 roles/bind_exporter/templates/web_config.yaml.j2 create mode 100644 roles/bind_exporter/test-requirements.txt create mode 100644 roles/bind_exporter/vars/main.yml diff --git a/roles/bind_exporter/README.md b/roles/bind_exporter/README.md new file mode 100644 index 000000000..49c65b50d --- /dev/null +++ b/roles/bind_exporter/README.md @@ -0,0 +1,85 @@ +

graph logo

+ +# Ansible Role: BIND exporter + + +Deploy prometheus [bind exporter](https://github.com/prometheus-community/bind_exporter) using ansible. + +## Requirements + +- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it) +- gnu-tar on Mac deployer host (`brew install gnu-tar`) +- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`) + +## Role Variables +All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml). +Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/bind_exporter_role.html) for description and default values of the variables. + +## Example + +### Playbook + +Use it in a playbook as follows: +```yaml +- hosts: all + collections: + - prometheus.prometheus + roles: + - prometheus.prometheus.bind_exporter +``` + +### TLS config + +Before running bind_exporter role, the user needs to provision their own certificate and key. +```yaml +- hosts: all + pre_tasks: + - name: Create bind_exporter cert dir + ansible.builtin.file: + path: "/etc/bind_exporter" + state: directory + owner: root + group: root + + - name: Create cert and key + community.crypto.x509_certificate: + path: /etc/bind_exporter/tls.cert + csr_path: /etc/bind_exporter/tls.csr + privatekey_path: /etc/bind_exporter/tls.key + provider: selfsigned + collections: + - prometheus.prometheus + roles: + - prometheus.prometheus.bind_exporter + vars: + bind_exporter_tls_server_config: + cert_file: /etc/bind_exporter/tls.cert + key_file: /etc/bind_exporter/tls.key + bind_exporter_basic_auth_users: + randomuser: examplepassword +``` + + +### Demo site + +We provide an example site that demonstrates a full monitoring solution based on prometheus and grafana. The repository with code and links to running instances is [available on github](https://github.com/prometheus/demo-site) and the site is hosted on [DigitalOcean](https://digitalocean.com). + +## Local Testing + +The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable for your system. Running your tests is as simple as executing `molecule test`. + +## Continuous Integration + +Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have quite a large test matrix which can take more time than local testing, so please be patient. + +## Contributing + +See [contributor guideline](CONTRIBUTING.md). + +## Troubleshooting + +See [troubleshooting](TROUBLESHOOTING.md). + +## License + +This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details. diff --git a/roles/bind_exporter/TROUBLESHOOTING.md b/roles/bind_exporter/TROUBLESHOOTING.md new file mode 100644 index 000000000..0bece12af --- /dev/null +++ b/roles/bind_exporter/TROUBLESHOOTING.md @@ -0,0 +1,20 @@ +# Troubleshooting + +## Bad requests (HTTP 400) + +This role downloads checksums from the Github project to verify the integrity of artifacts installed on your servers. When downloading the checksums, a "bad request" error might occur. + +This happens in environments which (knowningly or unknowling) use the [netrc mechanism](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) to auto-login into servers. + +Unless netrc is needed by your playbook and ansible roles, please unset the var like so: + +``` +$ NETRC= ansible-playbook ... +``` + +Or: + +``` +$ export NETRC= +$ ansible-playbook ... +``` diff --git a/roles/bind_exporter/defaults/main.yml b/roles/bind_exporter/defaults/main.yml new file mode 100644 index 000000000..b5146697a --- /dev/null +++ b/roles/bind_exporter/defaults/main.yml @@ -0,0 +1,26 @@ +--- +bind_exporter_version: 0.7.0 +bind_exporter_binary_local_dir: "" +bind_exporter_binary_url: "https://github.com/{{ _bind_exporter_repo }}/releases/download/v{{ bind_exporter_version }}/\ + bind_exporter-{{ bind_exporter_version }}.linux-{{ go_arch }}.tar.gz" +bind_exporter_checksums_url: "https://github.com/{{ _bind_exporter_repo }}/releases/download/v{{ bind_exporter_version }}/sha256sums.txt" +bind_exporter_skip_install: false + +bind_exporter_web_listen_address: "0.0.0.0:9119" +bind_exporter_web_telemetry_path: "/metrics" + +bind_exporter_stats_url: "http://localhost:8053/" +bind_exporter_timeout: "10s" +bind_exporter_pid_file: "/run/named/named.pid" +bind_exporter_stats_version: "auto" +bind_exporter_stats_groups: [] + +bind_exporter_tls_server_config: {} +bind_exporter_http_server_config: {} +bind_exporter_basic_auth_users: {} + +# Internal variables. +bind_exporter_binary_install_dir: "/usr/local/bin" +bind_exporter_config_dir: "/etc/bind_exporter" +bind_exporter_system_group: "bind-exp" +bind_exporter_system_user: "{{ bind_exporter_system_group }}" diff --git a/roles/bind_exporter/handlers/main.yml b/roles/bind_exporter/handlers/main.yml new file mode 100644 index 000000000..7a24b442e --- /dev/null +++ b/roles/bind_exporter/handlers/main.yml @@ -0,0 +1,10 @@ +--- +- name: Restart bind_exporter + listen: "restart bind_exporter" + become: true + ansible.builtin.systemd: + daemon_reload: true + name: bind_exporter + state: restarted + when: + - not ansible_check_mode diff --git a/roles/bind_exporter/meta/argument_specs.yml b/roles/bind_exporter/meta/argument_specs.yml new file mode 100644 index 000000000..4cfdebc0b --- /dev/null +++ b/roles/bind_exporter/meta/argument_specs.yml @@ -0,0 +1,90 @@ +--- +# yamllint disable rule:line-length +argument_specs: + main: + short_description: "Prometheus BIND Exporter" + description: + - "Deploy prometheus L(bind_exporter,https://github.com/prometheus-community/bind_exporter) using ansible" + author: + - "Prometheus Community" + options: + bind_exporter_version: + description: "BIND exporter package version. Also accepts latest as parameter." + default: "0.7.0" + bind_exporter_skip_install: + description: "BIND installation tasks gets skipped when set to true." + type: bool + default: false + bind_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(bind_exporter) binary is stored on the host where ansible is run." + - "This overrides the I(bind_exporter_version) parameter" + bind_exporter_binary_url: + description: "URL of the bind_exporter binaries .tar.gz file" + default: "https://github.com/{{ _bind_exporter_repo }}/download/v{{ bind_exporter_version }}/bind_exporter-{{ bind_exporter_version }}.linux-{{ go_arch }}.tar.gz" + bind_exporter_checksums_url: + description: "URL of the bind_exporter checksums file" + default: "https://github.com/{{ _bind_exporter_repo }}/releases/download/v{{ bind_exporter_version }}/sha256sums.txt" + bind_exporter_web_listen_address: + description: "Address on which bind_exporter will listen" + default: "0.0.0.0:9119" + bind_exporter_web_telemetry_path: + description: "Path under which to expose metrics" + default: "/metrics" + bind_exporter_config_dir: + description: "The path where exporter configuration is stored" + default: "/etc/bind_exporter" + bind_exporter_stats_url: + description: "HTTP XML API address of BIND server" + default: "http://localhost:8053/" + bind_exporter_timeout: + description: "Timeout for trying to get stats from BIND server" + default: "10s" + bind_exporter_pid_file: + description: "Path to BIND's pid file to export process information" + default: "/run/named/named.pid" + bind_exporter_stats_version: + description: "BIND statistics version." + default: "auto" + choices: + - "auto" + - "json" + - "xml" + - "xml.v3" + bind_exporter_stats_groups: + description: "List of statistics to collect" + default: [] + type: "list" + choices: + - "server" + - "view" + - "tasks" + bind_exporter_tls_server_config: + description: + - "Configuration for TLS authentication." + - "Keys and values are the same as in L(prometheus docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)." + type: "dict" + bind_exporter_http_server_config: + description: + - "Config for HTTP/2 support." + - "Keys and values are the same as in L(prometheus docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)." + type: "dict" + bind_exporter_basic_auth_users: + description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt." + type: "dict" + bind_exporter_binary_install_dir: + description: + - "I(Advanced)" + - "Directory to install bind_exporter binary" + default: "/usr/local/bin" + bind_exporter_system_group: + description: + - "I(Advanced)" + - "System group for BIND Exporter" + default: "bind-exp" + bind_exporter_system_user: + description: + - "I(Advanced)" + - "BIND Exporter user" + default: "bind-exp" diff --git a/roles/bind_exporter/meta/main.yml b/roles/bind_exporter/meta/main.yml new file mode 100644 index 000000000..6fb907d79 --- /dev/null +++ b/roles/bind_exporter/meta/main.yml @@ -0,0 +1,33 @@ +--- +galaxy_info: + author: "Prometheus Community" + description: "Prometheus BIND Exporter" + license: "Apache" + min_ansible_version: "2.9" + platforms: + - name: "Ubuntu" + versions: + - "focal" + - "jammy" + - name: "Debian" + versions: + - "bullseye" + - "buster" + - name: "EL" + versions: + - "7" + - "8" + - "9" + - name: "Fedora" + versions: + - "37" + - '38' + galaxy_tags: + - "monitoring" + - "prometheus" + - "exporter" + - "metrics" + - "system" + - "bind" + - "named" + - "dns" diff --git a/roles/bind_exporter/molecule/alternative/molecule.yml b/roles/bind_exporter/molecule/alternative/molecule.yml new file mode 100644 index 000000000..ad910d835 --- /dev/null +++ b/roles/bind_exporter/molecule/alternative/molecule.yml @@ -0,0 +1,21 @@ +--- +provisioner: + inventory: + group_vars: + all: + bind_exporter_binary_local_dir: "/tmp/bind_exporter-linux-amd64" + bind_exporter_web_listen_address: + - '127.0.0.1:8080' + - '127.0.1.1:8080' + bind_exporter_stats_groups: + - server + - view + bind_exporter_tls_server_config: + cert_file: /etc/bind_exporter/tls.cert + key_file: /etc/bind_exporter/tls.key + bind_exporter_http_server_config: + http2: true + bind_exporter_basic_auth_users: + randomuser: examplepassword + go_arch: amd64 + bind_exporter_version: 0.7.0 diff --git a/roles/bind_exporter/molecule/alternative/prepare.yml b/roles/bind_exporter/molecule/alternative/prepare.yml new file mode 100644 index 000000000..a6a857935 --- /dev/null +++ b/roles/bind_exporter/molecule/alternative/prepare.yml @@ -0,0 +1,80 @@ +--- +- name: Run local preparation + hosts: localhost + gather_facts: false + tasks: + - name: Download bind_exporter binary to local folder + become: false + ansible.builtin.get_url: + url: "https://github.com/prometheus-community/bind_exporter/releases/download/v{{\ + \ bind_exporter_version }}/bind_exporter-{{ bind_exporter_version\ + \ }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp/bind_exporter-{{ bind_exporter_version }}.linux-{{ go_arch\ + \ }}.tar.gz" + mode: 0644 + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + check_mode: false + + - name: Unpack bind_exporter binary + become: false + ansible.builtin.unarchive: + src: "/tmp/bind_exporter-{{ bind_exporter_version }}.linux-{{ go_arch\ + \ }}.tar.gz" + dest: "/tmp" + creates: "/tmp/bind_exporter-{{ bind_exporter_version }}.linux-{{ go_arch\ + \ }}/bind_exporter" + check_mode: false + + - name: Link to bind_exporter binaries directory + become: false + ansible.builtin.file: + src: "/tmp/bind_exporter-{{ bind_exporter_version }}.linux-amd64" + dest: "/tmp/bind_exporter-linux-amd64" + state: link + check_mode: false + + - name: Install pyOpenSSL for certificate generation + ansible.builtin.pip: + name: "pyOpenSSL" + + - name: Create private key + community.crypto.openssl_privatekey: + path: "/tmp/tls.key" + + - name: Create CSR + community.crypto.openssl_csr: + path: "/tmp/tls.csr" + privatekey_path: "/tmp/tls.key" + + - name: Create certificate + community.crypto.x509_certificate: + path: "/tmp/tls.cert" + csr_path: "/tmp/tls.csr" + privatekey_path: "/tmp/tls.key" + provider: selfsigned + +- name: Run target preparation + hosts: all + any_errors_fatal: true + tasks: + - name: Create bind_exporter cert dir + ansible.builtin.file: + path: "{{ bind_exporter_tls_server_config.cert_file | dirname }}" + state: directory + owner: root + group: root + mode: u+rwX,g+rwX,o=rX + + - name: Copy cert and key + ansible.builtin.copy: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + mode: "{{ item.mode | default('0644') }}" + loop: + - src: "/tmp/tls.cert" + dest: "{{ bind_exporter_tls_server_config.cert_file }}" + - src: "/tmp/tls.key" + dest: "{{ bind_exporter_tls_server_config.key_file }}" diff --git a/roles/bind_exporter/molecule/alternative/tests/test_alternative.py b/roles/bind_exporter/molecule/alternative/tests/test_alternative.py new file mode 100644 index 000000000..18ea53deb --- /dev/null +++ b/roles/bind_exporter/molecule/alternative/tests/test_alternative.py @@ -0,0 +1,30 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner +import pytest + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_service(host): + s = host.service("bind_exporter") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u bind_exporter --since "1 hour ago"') + print("\n==== journalctl -u bind_exporter Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +@pytest.mark.parametrize("sockets", [ + "tcp://127.0.0.1:8080", + "tcp://127.0.1.1:8080", +]) +def test_socket(host, sockets): + assert host.socket(sockets).is_listening diff --git a/roles/bind_exporter/molecule/default/molecule.yml b/roles/bind_exporter/molecule/default/molecule.yml new file mode 100644 index 000000000..0cadae894 --- /dev/null +++ b/roles/bind_exporter/molecule/default/molecule.yml @@ -0,0 +1,6 @@ +--- +provisioner: + inventory: + group_vars: + all: + bind_exporter_web_listen_address: "127.0.0.1:9119" diff --git a/roles/bind_exporter/molecule/default/tests/test_default.py b/roles/bind_exporter/molecule/default/tests/test_default.py new file mode 100644 index 000000000..64e8251ed --- /dev/null +++ b/roles/bind_exporter/molecule/default/tests/test_default.py @@ -0,0 +1,73 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_directories(host): + dirs = [ + "/etc/bind_exporter" + ] + for dir in dirs: + d = host.file(dir) + assert d.is_directory + assert d.exists + + +def test_files(host): + files = [ + "/etc/systemd/system/bind_exporter.service", + "/usr/local/bin/bind_exporter" + ] + for file in files: + f = host.file(file) + assert f.exists + assert f.is_file + + +def test_permissions_didnt_change(host): + dirs = [ + "/etc", + "/root", + "/usr", + "/var" + ] + for file in dirs: + f = host.file(file) + assert f.exists + assert f.is_directory + assert f.user == "root" + assert f.group == "root" + + +def test_user(host): + assert host.group("bind-exp").exists + assert "bind-exp" in host.user("bind-exp").groups + assert host.user("bind-exp").shell == "/usr/sbin/nologin" + assert host.user("bind-exp").home == "/" + + +def test_service(host): + s = host.service("bind_exporter") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u bind_exporter --since "1 hour ago"') + print("\n==== journalctl -u bind_exporter Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +def test_socket(host): + sockets = [ + "tcp://127.0.0.1:9119" + ] + for socket in sockets: + s = host.socket(socket) + assert s.is_listening diff --git a/roles/bind_exporter/molecule/latest/molecule.yml b/roles/bind_exporter/molecule/latest/molecule.yml new file mode 100644 index 000000000..7152db870 --- /dev/null +++ b/roles/bind_exporter/molecule/latest/molecule.yml @@ -0,0 +1,6 @@ +--- +provisioner: + inventory: + group_vars: + all: + bind_exporter_version: latest diff --git a/roles/bind_exporter/molecule/latest/tests/test_latest.py b/roles/bind_exporter/molecule/latest/tests/test_latest.py new file mode 100644 index 000000000..dc9e04034 --- /dev/null +++ b/roles/bind_exporter/molecule/latest/tests/test_latest.py @@ -0,0 +1,37 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import testinfra.utils.ansible_runner +import pytest + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +@pytest.mark.parametrize("files", [ + "/etc/systemd/system/bind_exporter.service", + "/usr/local/bin/bind_exporter" +]) +def test_files(host, files): + f = host.file(files) + assert f.exists + assert f.is_file + + +def test_service(host): + s = host.service("bind_exporter") + try: + assert s.is_running + except AssertionError: + # Capture service logs + journal_output = host.run('journalctl -u bind_exporter --since "1 hour ago"') + print("\n==== journalctl -u bind_exporter Output ====\n") + print(journal_output) + print("\n============================================\n") + raise # Re-raise the original assertion error + + +def test_socket(host): + s = host.socket("tcp://0.0.0.0:9119") + assert s.is_listening diff --git a/roles/bind_exporter/tasks/configure.yml b/roles/bind_exporter/tasks/configure.yml new file mode 100644 index 000000000..4b7841e85 --- /dev/null +++ b/roles/bind_exporter/tasks/configure.yml @@ -0,0 +1,42 @@ +--- +- name: Copy the bind_exporter systemd service file + ansible.builtin.template: + src: bind_exporter.service.j2 + dest: /etc/systemd/system/bind_exporter.service + owner: root + group: root + mode: '0644' + notify: restart bind_exporter + +- name: Create bind_exporter config directory + ansible.builtin.file: + path: "{{ bind_exporter_config_dir }}" + state: directory + owner: root + group: root + mode: u+rwX,g+rwX,o=rX + +- name: Configure bind_exporter web config + when: + ( bind_exporter_tls_server_config | length > 0 ) or + ( bind_exporter_http_server_config | length > 0 ) or + ( bind_exporter_basic_auth_users | length > 0 ) + block: + - name: Copy the bind_exporter web config file + ansible.builtin.template: + src: web_config.yaml.j2 + dest: "{{ bind_exporter_config_dir }}/web_config.yaml" + owner: root + group: '{{ bind_exporter_system_group }}' + mode: '0640' + notify: restart bind_exporter + +- name: Allow bind_exporter port in SELinux on RedHat OS family + community.general.seport: + ports: "{{ bind_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" diff --git a/roles/bind_exporter/tasks/install.yml b/roles/bind_exporter/tasks/install.yml new file mode 100644 index 000000000..6f835dab2 --- /dev/null +++ b/roles/bind_exporter/tasks/install.yml @@ -0,0 +1,71 @@ +--- +- name: Create the bind_exporter group + ansible.builtin.group: + name: "{{ bind_exporter_system_group }}" + state: present + system: true + when: bind_exporter_system_group not in ["root"] + +- name: Create the bind_exporter user + ansible.builtin.user: + name: "{{ bind_exporter_system_user }}" + groups: "{{ bind_exporter_system_group }}" + append: true + shell: /usr/sbin/nologin + system: true + create_home: false + home: / + when: bind_exporter_system_user not in ["root"] + +- name: Get binary + when: + - bind_exporter_binary_local_dir | length == 0 + - not bind_exporter_skip_install + block: + + - name: Download bind_exporter binary to local folder + become: false + ansible.builtin.get_url: + url: "{{ bind_exporter_binary_url }}" + dest: "/tmp/bind_exporter-{{ bind_exporter_version }}.linux-{{ go_arch }}.tar.gz" + checksum: "sha256:{{ __bind_exporter_checksum }}" + mode: '0644' + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + delegate_to: localhost + check_mode: false + + - name: Unpack bind_exporter binary + become: false + ansible.builtin.unarchive: + src: "/tmp/bind_exporter-{{ bind_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp" + creates: "/tmp/bind_exporter-{{ bind_exporter_version }}.linux-{{ go_arch }}/bind_exporter" + extra_opts: + - --no-same-owner + delegate_to: localhost + check_mode: false + + - name: Propagate bind_exporter binaries + ansible.builtin.copy: + src: "/tmp/bind_exporter-{{ bind_exporter_version }}.linux-{{ go_arch }}/bind_exporter" + dest: "{{ bind_exporter_binary_install_dir }}/bind_exporter" + mode: '0755' + owner: root + group: root + notify: restart bind_exporter + when: not ansible_check_mode + +- name: Propagate locally distributed bind_exporter binary + ansible.builtin.copy: + src: "{{ bind_exporter_binary_local_dir }}/bind_exporter" + dest: "{{ bind_exporter_binary_install_dir }}/bind_exporter" + mode: '0755' + owner: root + group: root + when: + - bind_exporter_binary_local_dir | length > 0 + - not bind_exporter_skip_install + notify: restart bind_exporter diff --git a/roles/bind_exporter/tasks/main.yml b/roles/bind_exporter/tasks/main.yml new file mode 100644 index 000000000..6dc4ac9c2 --- /dev/null +++ b/roles/bind_exporter/tasks/main.yml @@ -0,0 +1,63 @@ +--- +- name: Preflight + ansible.builtin.include_tasks: + file: preflight.yml + apply: + tags: + - bind_exporter_install + - bind_exporter_configure + - bind_exporter_run + tags: + - bind_exporter_install + - bind_exporter_configure + - bind_exporter_run + +- name: Install + ansible.builtin.include_tasks: + file: install.yml + apply: + become: true + tags: + - bind_exporter_install + when: + ( not __bind_exporter_is_installed.stat.exists ) or + ( (__bind_exporter_current_version_output.stderr_lines | length > 0) + and (__bind_exporter_current_version_output.stderr_lines[0].split(" ")[2] != bind_exporter_version) ) or + ( (__bind_exporter_current_version_output.stdout_lines | length > 0) + and (__bind_exporter_current_version_output.stdout_lines[0].split(" ")[2] != bind_exporter_version) ) or + ( bind_exporter_binary_local_dir | length > 0 ) + tags: + - bind_exporter_install + +- name: SELinux + ansible.builtin.include_tasks: + file: selinux.yml + apply: + become: true + tags: + - bind_exporter_configure + when: ansible_selinux.status == "enabled" + tags: + - bind_exporter_configure + +- name: Configure + ansible.builtin.include_tasks: + file: configure.yml + apply: + become: true + tags: + - bind_exporter_configure + tags: + - bind_exporter_configure + +- name: Ensure BIND Exporter is enabled on boot + become: true + ansible.builtin.systemd: + daemon_reload: true + name: bind_exporter + enabled: true + state: started + when: + - not ansible_check_mode + tags: + - bind_exporter_run diff --git a/roles/bind_exporter/tasks/preflight.yml b/roles/bind_exporter/tasks/preflight.yml new file mode 100644 index 000000000..89a00e81c --- /dev/null +++ b/roles/bind_exporter/tasks/preflight.yml @@ -0,0 +1,115 @@ +--- +- 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: Assert that used version supports listen address type + ansible.builtin.assert: + that: + - >- + bind_exporter_web_listen_address is string + or + ( + bind_exporter_version is version('0.15.0', '>=') and + bind_exporter_web_listen_address | type_debug == "list" + ) + +- name: Naive assertion of proper listen address + ansible.builtin.assert: + that: + - >- + [bind_exporter_web_listen_address] | + flatten | + reject('match', '.+:\\d+$') | + list | + length == 0 + +- name: Assert that TLS config is correct + when: bind_exporter_tls_server_config | length > 0 + block: + - name: Assert that TLS key and cert path are set + ansible.builtin.assert: + that: + - "bind_exporter_tls_server_config.cert_file is defined" + - "bind_exporter_tls_server_config.key_file is defined" + + - name: Check existence of TLS cert file + ansible.builtin.stat: + path: "{{ bind_exporter_tls_server_config.cert_file }}" + register: __bind_exporter_cert_file + + - name: Check existence of TLS key file + ansible.builtin.stat: + path: "{{ bind_exporter_tls_server_config.key_file }}" + register: __bind_exporter_key_file + + - name: Assert that TLS key and cert are present + ansible.builtin.assert: + that: + - "__bind_exporter_cert_file.stat.exists" + - "__bind_exporter_key_file.stat.exists" + +- name: Check if bind_exporter is installed + ansible.builtin.stat: + path: "{{ bind_exporter_binary_install_dir }}/bind_exporter" + register: __bind_exporter_is_installed + check_mode: false + tags: + - bind_exporter_install + +- name: Gather currently installed bind_exporter version (if any) + ansible.builtin.command: "{{ bind_exporter_binary_install_dir }}/bind_exporter --version" + changed_when: false + register: __bind_exporter_current_version_output + check_mode: false + when: __bind_exporter_is_installed.stat.exists + tags: + - bind_exporter_install + +- name: Discover latest version + ansible.builtin.set_fact: + bind_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _bind_exporter_repo }}/releases/latest', headers=_github_api_headers, + split_lines=False) | from_json).get('tag_name') | replace('v', '') }}" + run_once: true + until: bind_exporter_version is version('0.0.0', '>=') + retries: 10 + when: + - bind_exporter_version == "latest" + - bind_exporter_binary_local_dir | length == 0 + - not bind_exporter_skip_install + +- name: Get bind_exporter binary checksum + when: + - bind_exporter_binary_local_dir | length == 0 + - not bind_exporter_skip_install + block: + - name: Get checksum list from github + ansible.builtin.set_fact: + __bind_exporter_checksums: "{{ lookup('url', bind_exporter_checksums_url, headers=_github_api_headers, wantlist=True) | list }}" + run_once: true + until: __bind_exporter_checksums is search('linux-' + go_arch + '.tar.gz') + retries: 10 + + - name: "Get checksum for {{ go_arch }}" + ansible.builtin.set_fact: + __bind_exporter_checksum: "{{ item.split(' ')[0] }}" + with_items: "{{ __bind_exporter_checksums }}" + when: + - "('linux-' + go_arch + '.tar.gz') in item" diff --git a/roles/bind_exporter/tasks/selinux.yml b/roles/bind_exporter/tasks/selinux.yml new file mode 100644 index 000000000..754cbd0a2 --- /dev/null +++ b/roles/bind_exporter/tasks/selinux.yml @@ -0,0 +1,23 @@ +--- +- 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" diff --git a/roles/bind_exporter/templates/bind_exporter.service.j2 b/roles/bind_exporter/templates/bind_exporter.service.j2 new file mode 100644 index 000000000..badee86a2 --- /dev/null +++ b/roles/bind_exporter/templates/bind_exporter.service.j2 @@ -0,0 +1,48 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Prometheus BIND Exporter +After=network-online.target + +[Service] +Type=simple +User={{ bind_exporter_system_user }} +Group={{ bind_exporter_system_group }} +ExecStart={{ bind_exporter_binary_install_dir }}/bind_exporter \ +{% if bind_exporter_stats_groups | length > 0 %} + --bind.stats-groups="{{ bind_exporter_stats_groups | join(',') }}" \ +{% endif %} +{% if bind_exporter_tls_server_config | length > 0 or bind_exporter_http_server_config | length > 0 or bind_exporter_basic_auth_users | length > 0 %} + --web.config.file={{ bind_exporter_config_dir }}/web_config.yaml \ +{% endif %} + --bind.stats-url="{{ bind_exporter_stats_url }}" \ + --bind.timeout="{{ bind_exporter_timeout }}" \ + --bind.pid-file="{{ bind_exporter_pid_file }}" \ + --bind.stats-version={{ bind_exporter_stats_version }} \ + --web.listen-address={{ bind_exporter_web_listen_address }} \ + --web.telemetry-path={{ bind_exporter_web_telemetry_path }} + + +SyslogIdentifier=bind_exporter +Restart=always +RestartSec=1 +StartLimitInterval=0 + +{% set protect_home = 'yes' %} +{% for m in ansible_mounts if m.mount.startswith('/home') %} +{% set protect_home = 'read-only' %} +{% endfor %} +ProtectHome={{ protect_home }} +NoNewPrivileges=yes + +{% if (ansible_facts.packages.systemd | first).version is version('232', '>=') %} +ProtectSystem=strict +ProtectControlGroups=true +ProtectKernelModules=true +ProtectKernelTunables=yes +{% else %} +ProtectSystem=full +{% endif %} + +[Install] +WantedBy=multi-user.target diff --git a/roles/bind_exporter/templates/web_config.yaml.j2 b/roles/bind_exporter/templates/web_config.yaml.j2 new file mode 100644 index 000000000..cf458e424 --- /dev/null +++ b/roles/bind_exporter/templates/web_config.yaml.j2 @@ -0,0 +1,18 @@ +--- +{{ ansible_managed | comment }} +{% if bind_exporter_tls_server_config | length > 0 %} +tls_server_config: +{{ bind_exporter_tls_server_config | to_nice_yaml | indent(2, true) }} +{% endif %} + +{% if bind_exporter_http_server_config | length > 0 %} +http_server_config: +{{ bind_exporter_http_server_config | to_nice_yaml | indent(2, true) }} +{% endif %} + +{% if bind_exporter_basic_auth_users | length > 0 %} +basic_auth_users: +{% for k, v in bind_exporter_basic_auth_users.items() %} + {{ k }}: {{ v | string | password_hash('bcrypt', ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' | shuffle(seed=inventory_hostname) | join)[:22], rounds=9) }} +{% endfor %} +{% endif %} diff --git a/roles/bind_exporter/test-requirements.txt b/roles/bind_exporter/test-requirements.txt new file mode 100644 index 000000000..7f0b6e759 --- /dev/null +++ b/roles/bind_exporter/test-requirements.txt @@ -0,0 +1 @@ +bcrypt diff --git a/roles/bind_exporter/vars/main.yml b/roles/bind_exporter/vars/main.yml new file mode 100644 index 000000000..40eba5dba --- /dev/null +++ b/roles/bind_exporter/vars/main.yml @@ -0,0 +1,12 @@ +--- +go_arch_map: + i386: '386' + x86_64: 'amd64' + aarch64: 'arm64' + armv7l: 'armv7' + armv6l: 'armv6' + +go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}" + +_bind_exporter_repo: "prometheus-community/bind_exporter" +_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}" From 631971b233d80cbfd21d36edfb33521f14304228 Mon Sep 17 00:00:00 2001 From: anviar Date: Fri, 23 Feb 2024 13:40:20 +0300 Subject: [PATCH 6/7] test: Add bind_exporter scripts Signed-off-by: anviar --- .../targets/molecule-bind_exporter-alternative/runme.sh | 4 ++++ .../targets/molecule-bind_exporter-default/runme.sh | 4 ++++ .../targets/molecule-bind_exporter-latest/runme.sh | 4 ++++ 3 files changed, 12 insertions(+) create mode 100755 tests/integration/targets/molecule-bind_exporter-alternative/runme.sh create mode 100755 tests/integration/targets/molecule-bind_exporter-default/runme.sh create mode 100755 tests/integration/targets/molecule-bind_exporter-latest/runme.sh diff --git a/tests/integration/targets/molecule-bind_exporter-alternative/runme.sh b/tests/integration/targets/molecule-bind_exporter-alternative/runme.sh new file mode 100755 index 000000000..d094c3e1b --- /dev/null +++ b/tests/integration/targets/molecule-bind_exporter-alternative/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" diff --git a/tests/integration/targets/molecule-bind_exporter-default/runme.sh b/tests/integration/targets/molecule-bind_exporter-default/runme.sh new file mode 100755 index 000000000..d094c3e1b --- /dev/null +++ b/tests/integration/targets/molecule-bind_exporter-default/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" diff --git a/tests/integration/targets/molecule-bind_exporter-latest/runme.sh b/tests/integration/targets/molecule-bind_exporter-latest/runme.sh new file mode 100755 index 000000000..d094c3e1b --- /dev/null +++ b/tests/integration/targets/molecule-bind_exporter-latest/runme.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +collection_root=$(pwd | grep -oP ".+\/ansible_collections\/\w+?\/\w+") +source "$collection_root/tests/integration/molecule.sh" From 3f9aa6eaba18942b39eb2c0a03dcb91e59c86cee Mon Sep 17 00:00:00 2001 From: anviar Date: Fri, 23 Feb 2024 22:04:27 +0300 Subject: [PATCH 7/7] fix: bind_exporter multiple listen addresses definition Signed-off-by: anviar --- roles/bind_exporter/tasks/preflight.yml | 11 ----------- .../bind_exporter/templates/bind_exporter.service.j2 | 8 ++++++++ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/roles/bind_exporter/tasks/preflight.yml b/roles/bind_exporter/tasks/preflight.yml index 89a00e81c..a130762c0 100644 --- a/roles/bind_exporter/tasks/preflight.yml +++ b/roles/bind_exporter/tasks/preflight.yml @@ -20,17 +20,6 @@ ansible.builtin.package_facts: when: "not 'packages' in ansible_facts" -- name: Assert that used version supports listen address type - ansible.builtin.assert: - that: - - >- - bind_exporter_web_listen_address is string - or - ( - bind_exporter_version is version('0.15.0', '>=') and - bind_exporter_web_listen_address | type_debug == "list" - ) - - name: Naive assertion of proper listen address ansible.builtin.assert: that: diff --git a/roles/bind_exporter/templates/bind_exporter.service.j2 b/roles/bind_exporter/templates/bind_exporter.service.j2 index badee86a2..ab5624ef6 100644 --- a/roles/bind_exporter/templates/bind_exporter.service.j2 +++ b/roles/bind_exporter/templates/bind_exporter.service.j2 @@ -19,7 +19,15 @@ ExecStart={{ bind_exporter_binary_install_dir }}/bind_exporter \ --bind.timeout="{{ bind_exporter_timeout }}" \ --bind.pid-file="{{ bind_exporter_pid_file }}" \ --bind.stats-version={{ bind_exporter_stats_version }} \ +{% if bind_exporter_web_listen_address is iterable and + bind_exporter_web_listen_address is not mapping and + bind_exporter_web_listen_address is not string %} +{% for address in bind_exporter_web_listen_address %} + --web.listen-address={{ address }} \ +{% endfor %} +{% else %} --web.listen-address={{ bind_exporter_web_listen_address }} \ +{% endif %} --web.telemetry-path={{ bind_exporter_web_telemetry_path }}