Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
postgres_exporter_setup: 'install'

# See available releases: https://github.com/prometheus-community/postgres_exporter/releases
postgres_exporter_version: '0.18.1'
postgres_exporter_archive_name: 'postgres_exporter-{{ postgres_exporter_version }}.{{ __postgres_exporter_os }}-{{ __postgres_exporter_architecture }}'
Expand Down
6 changes: 6 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
state: 'restarted'
when: not ansible_check_mode

- name: 'Linux | Systemd daemon-reload'
become: true
ansible.builtin.systemd:
daemon_reload: true
when: not ansible_check_mode

- name: 'Windows | Restart the Postgres Exporter scheduled task'
ansible.windows.win_shell: schtasks /end /tn 'Postgres Exporter'; schtasks /run /tn 'Postgres Exporter'
8 changes: 8 additions & 0 deletions meta/argument_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ argument_specs:
- 'An Ansible role to install, configure and update the L(Postgres Exporter,https://github.com/prometheus-community/postgres_exporter).'
author: 'Melekhin Anton'
options:
postgres_exporter_setup:
type: 'str'
description: ''
choices:
- 'install'
- 'upgrade'
- 'uninstall'
default: 'install'
postgres_exporter_version:
type: 'str'
description: 'The version of Postgres Exporter to install.'
Expand Down
6 changes: 6 additions & 0 deletions molecule/uninstall/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: 'Converge'
hosts: all
roles:
- role: antmelekhin.postgres_exporter
postgres_exporter_setup: 'uninstall'
20 changes: 20 additions & 0 deletions molecule/uninstall/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
role_name_check: 2
dependency:
name: 'galaxy'
enabled: true
driver:
name: 'docker'
platforms:
- name: 'postgres_exporter-${IMAGE:-docker-systemd}-${TAG:-ubuntu-22.04}${TOX_ENVNAME}'
image: '${NAMESPACE:-antmelekhin}/${IMAGE:-docker-systemd}:${TAG:-ubuntu-22.04}'
volumes:
- '/sys/fs/cgroup:/sys/fs/cgroup:rw'
- '/var/lib/containerd'
cgroupns_mode: 'host'
privileged: true
pre_build_image: true
provisioner:
name: 'ansible'
verifier:
name: 'ansible'
14 changes: 14 additions & 0 deletions molecule/uninstall/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: 'Prepare an Ansible host for GitHub Actions'
hosts: localhost
tasks:
- name: 'Install tar for tasks delegated to the localhost'
community.general.apk:
name: 'tar'
update_cache: true
when: ansible_os_family == 'Alpine'

- name: 'Install the Postgres Exporter'
hosts: all
roles:
- role: antmelekhin.postgres_exporter
28 changes: 28 additions & 0 deletions molecule/uninstall/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
- name: 'Verify'
hosts: all
gather_facts: false

tasks:
- name: 'Check if Postgres Exporter is installed'
ansible.builtin.stat:
path: '/usr/local/bin/postgres_exporter'
register: __postgres_exporter_is_installed
failed_when: __postgres_exporter_is_installed['stat']['exists'] | bool
check_mode: true

- name: 'Check if Postgres Exporter service is running'
ansible.builtin.systemd:
name: 'postgres_exporter'
state: 'started'
enabled: true
register: __postgres_exporter_service
failed_when: __postgres_exporter_service is changed
check_mode: true

- name: 'Check web_config.yaml does not exist'
ansible.builtin.stat:
path: '/etc/postgres_exporter/web_config.yaml'
register: __postgres_exporter_config
failed_when: __postgres_exporter_config['stat']['exists'] | bool
check_mode: true
4 changes: 2 additions & 2 deletions tasks/Linux/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
when:
- not __postgres_exporter_is_installed.stat.exists or
( __postgres_exporter_version_check.stderr_lines | length and
postgres_exporter_version not in __postgres_exporter_version_check.stderr_lines[0].split()[2] ) or
postgres_exporter_version not in __postgres_exporter_version_check.stderr_lines[0].split()[2] and 'upgrade' in postgres_exporter_setup ) or
( __postgres_exporter_version_check.stdout_lines | length and
postgres_exporter_version not in __postgres_exporter_version_check.stdout_lines[0].split()[2] )
postgres_exporter_version not in __postgres_exporter_version_check.stdout_lines[0].split()[2] and 'upgrade' in postgres_exporter_setup )
block:
- name: 'Linux | Download the Postgres Exporter archive to a local folder'
become: false
Expand Down
42 changes: 42 additions & 0 deletions tasks/Linux/uninstall.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
- name: 'Linux | Uninstall the Postgres Exporter'
become: true
block:
- name: 'Linux | Check if Postgres Exporter systemd service is existed'
ansible.builtin.stat:
path: '/etc/systemd/system/postgres_exporter.service'
register: __postgres_exporter_unit_file

- name: 'Linux | Ensure the Postgres Exporter is stopped and disabled at boot'
ansible.builtin.systemd:
name: 'postgres_exporter'
state: 'stopped'
enabled: false
when: __postgres_exporter_unit_file.stat.exists

- name: 'Linux | Remove the Postgres Exporter systemd service'
ansible.builtin.file:
path: '/etc/systemd/system/postgres_exporter.service'
state: 'absent'
notify: 'Linux | Systemd daemon-reload'

- name: 'Linux | Remove the Postgres Exporter binary'
ansible.builtin.file:
path: '{{ postgres_exporter_install_path }}/postgres_exporter'
state: 'absent'

- name: 'Linux | Remove a Postgres Exporter configuration directory'
ansible.builtin.file:
path: '{{ postgres_exporter_config_path }}'
state: 'absent'

- name: 'Linux | Remove a Postgres Exporter user'
ansible.builtin.user:
name: '{{ postgres_exporter_user }}'
state: 'absent'
remove: true

- name: 'Linux | Remove a Postgres Exporter group'
ansible.builtin.group:
name: '{{ postgres_exporter_group }}'
state: 'absent'
4 changes: 2 additions & 2 deletions tasks/Win32NT/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
when:
- not __postgres_exporter_is_installed.stat.exists or
( __postgres_exporter_version_check.stderr_lines | length and
postgres_exporter_version not in __postgres_exporter_version_check.stderr_lines[0].split()[2] ) or
postgres_exporter_version not in __postgres_exporter_version_check.stderr_lines[0].split()[2] and 'upgrade' in postgres_exporter_setup ) or
( __postgres_exporter_version_check.stdout_lines | length and
postgres_exporter_version not in __postgres_exporter_version_check.stdout_lines[0].split()[2] )
postgres_exporter_version not in __postgres_exporter_version_check.stdout_lines[0].split()[2] and 'upgrade' in postgres_exporter_setup )
block:
- name: 'Windows | Download the Postgres Exporter archive to a local folder'
become: false
Expand Down
7 changes: 7 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
---
- name: 'Prepare for installing the Postgres Exporter'
ansible.builtin.include_tasks: '{{ ansible_system }}/pre-install.yml'
when: postgres_exporter_setup in ['install','upgrade']

- name: 'Install the Postgres Exporter'
ansible.builtin.include_tasks: '{{ ansible_system }}/install.yml'
when: postgres_exporter_setup in ['install','upgrade']

- name: 'Configure the Postgres Exporter'
ansible.builtin.include_tasks: '{{ ansible_system }}/configure.yml'
when: postgres_exporter_setup in ['install','upgrade']

- name: 'Uninstall the Postgres Exporter'
ansible.builtin.include_tasks: '{{ ansible_system }}/uninstall.yml'
when: postgres_exporter_setup in ['uninstall']