diff --git a/defaults/main.yml b/defaults/main.yml index f97a767..0471add 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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 }}' diff --git a/handlers/main.yml b/handlers/main.yml index 218c007..4978c49 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -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' diff --git a/meta/argument_specs.yml b/meta/argument_specs.yml index 6b2e389..f4bbfd8 100644 --- a/meta/argument_specs.yml +++ b/meta/argument_specs.yml @@ -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.' diff --git a/molecule/uninstall/converge.yml b/molecule/uninstall/converge.yml new file mode 100644 index 0000000..0fc9bb6 --- /dev/null +++ b/molecule/uninstall/converge.yml @@ -0,0 +1,6 @@ +--- +- name: 'Converge' + hosts: all + roles: + - role: antmelekhin.postgres_exporter + postgres_exporter_setup: 'uninstall' diff --git a/molecule/uninstall/molecule.yml b/molecule/uninstall/molecule.yml new file mode 100644 index 0000000..673084e --- /dev/null +++ b/molecule/uninstall/molecule.yml @@ -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' diff --git a/molecule/uninstall/prepare.yml b/molecule/uninstall/prepare.yml new file mode 100644 index 0000000..5f3a2fb --- /dev/null +++ b/molecule/uninstall/prepare.yml @@ -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 diff --git a/molecule/uninstall/verify.yml b/molecule/uninstall/verify.yml new file mode 100644 index 0000000..6a7f12d --- /dev/null +++ b/molecule/uninstall/verify.yml @@ -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 diff --git a/tasks/Linux/install.yml b/tasks/Linux/install.yml index 282fdf3..c766c15 100644 --- a/tasks/Linux/install.yml +++ b/tasks/Linux/install.yml @@ -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 diff --git a/tasks/Linux/uninstall.yml b/tasks/Linux/uninstall.yml new file mode 100644 index 0000000..449ae81 --- /dev/null +++ b/tasks/Linux/uninstall.yml @@ -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' diff --git a/tasks/Win32NT/install.yml b/tasks/Win32NT/install.yml index 8481e8e..b7715a7 100644 --- a/tasks/Win32NT/install.yml +++ b/tasks/Win32NT/install.yml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index 50ba27f..b835a1a 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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']