diff --git a/roles/prometheus/README.md b/roles/prometheus/README.md index 5d73cbcc..0557eb07 100644 --- a/roles/prometheus/README.md +++ b/roles/prometheus/README.md @@ -1,17 +1,69 @@ - +The role will: +- Create a dedicated system user and group (`prometheus`). +- Create necessary directories for Prometheus configuration (`/etc/prometheus`) and TSDB storage (`/var/lib/prometheus`). +- Download the Prometheus distribution tarball. +- Extract the Prometheus binary and related files to the installation directory. +- Install a basic `prometheus.yml` configuration file. +- Set up a `systemd` service for Prometheus. +- Enable and start the Prometheus service, ensuring it runs on system boot. -# prometheus +# Requirements + +- Target host must have `systemd` for service management. +- Internet access on the target host to download the Prometheus tarball. + +# Dependencies + +None. + +# Parameters + +| Variable | Type | Required | Default | Description | +| --- | --- | --- | --- | --- | +| `prometheus_tarball_url` | `str` | `False` | `https://github.com/prometheus/prometheus/releases/download/v2.48.1/prometheus-2.48.1.linux-amd64.tar.gz` | URL to the Prometheus distribution archive file. | +| `prometheus_directory` | `path` | `False` | `/etc/prometheus` | Prometheus configuration directory. | +| `prometheus_tsdb_directory` | `path` | `False` | `/var/lib/prometheus` | Prometheus TSDB directory. | +| `prometheus_tarball_file` | `str` | `False` | `prometheus.tar.gz` | Intermediate archive file name for the downloaded tarball. | +| `prometheus_user` | `str` | `False` | `prometheus` | Prometheus service user. | +| `prometheus_group` | `str` | `False` | `prometheus` | Prometheus service group. | +| `prometheus_service_directory` | `path` | `False` | `/etc/systemd/system/prometheus.service` | Prometheus Systemd service directory (full path to the service file). | + +# Example Playbook + +```yaml +- hosts: monitoring_servers + tasks: + - name: Install Prometheus server with custom settings + ansible.builtin.import_role: + name: prometheus + vars: + prometheus_tarball_url: "[https://github.com/prometheus/prometheus/releases/download/v2.49.0/prometheus-2.49.0.linux-amd64.tar.gz](https://github.com/prometheus/prometheus/releases/download/v2.49.0/prometheus-2.49.0.linux-amd64.tar.gz)" + prometheus_directory: "/opt/prometheus/config" + prometheus_tsdb_directory: "/data/prometheus_tsdb" + prometheus_user: "prom_admin" + prometheus_group: "prom_admin" +``` + +# License + +``` +Copyright 2025 Cloudera, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` diff --git a/roles/prometheus/defaults/main.yml b/roles/prometheus/defaults/main.yml index 9adf8302..e9b44288 100644 --- a/roles/prometheus/defaults/main.yml +++ b/roles/prometheus/defaults/main.yml @@ -1,6 +1,4 @@ ---- - -# Copyright 2024 Cloudera, Inc. All Rights Reserved. +# Copyright 2025 Cloudera, Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +--- + prometheus_tarball_url: https://github.com/prometheus/prometheus/releases/download/v2.48.1/prometheus-2.48.1.linux-amd64.tar.gz prometheus_directory: /etc/prometheus prometheus_tsdb_directory: /var/lib/prometheus diff --git a/roles/prometheus/handlers/main.yml b/roles/prometheus/handlers/main.yml index 3425e35a..12fd25f3 100644 --- a/roles/prometheus/handlers/main.yml +++ b/roles/prometheus/handlers/main.yml @@ -1,5 +1,4 @@ ---- -# Copyright 2024 Cloudera, Inc. All Rights Reserved. +# Copyright 2025 Cloudera, Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,7 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -- name: Restart prometheus +--- + +- name: Enable Prometheus + block: + - name: Reload systemd daemon + ansible.builtin.systemd: + daemon_reload: true + + - name: Enable and start Prometheus service + ansible.builtin.systemd: + name: prometheus + state: started + enabled: true + +- name: Restart Prometheus ansible.builtin.service: name: prometheus state: restarted diff --git a/roles/prometheus/meta/argument_specs.yml b/roles/prometheus/meta/argument_specs.yml new file mode 100644 index 00000000..f6293691 --- /dev/null +++ b/roles/prometheus/meta/argument_specs.yml @@ -0,0 +1,59 @@ +# Copyright 2025 Cloudera, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +argument_specs: + main: + short_description: Install Prometheus. + description: + - Install Prometheus from the distribution archive file. + - Set up the local time-series database. + - Set up the service user and group. + - Install a basic configuration. + author: Cloudera Labs + options: + prometheus_tarball_url: + description: URL to the Prometheus distribution archive file. + type: str + required: false + default: https://github.com/prometheus/prometheus/releases/download/v2.48.1/prometheus-2.48.1.linux-amd64.tar.gz + prometheus_directory: + description: Prometheus configuration directory. + type: path + required: false + default: /etc/prometheus + prometheus_tsdb_directory: + description: Prometheus TSDB directory. + type: path + required: false + default: /var/lib/prometheus + prometheus_tarball_file: + description: Intermediate archive file name. + type: str + required: false + default: prometheus.tar.gz + prometheus_user: + description: Prometheus service user. + type: str + required: false + default: prometheus + prometheus_group: + description: Prometheus service group. + type: str + required: false + default: prometheus + prometheus_service_directory: + description: Prometheus Systemd service directory. + type: path + required: false + default: /etc/systemd/system/prometheus.service diff --git a/roles/prometheus/tasks/main.yml b/roles/prometheus/tasks/main.yml index 06475717..e056cc89 100644 --- a/roles/prometheus/tasks/main.yml +++ b/roles/prometheus/tasks/main.yml @@ -1,5 +1,4 @@ ---- -# Copyright 2024 Cloudera, Inc. All Rights Reserved. +# Copyright 2025 Cloudera, Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,9 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +--- + - name: Create Prometheus directory ansible.builtin.file: path: "{{ prometheus_directory }}" + mode: "0755" state: directory - name: Create a temporary directory @@ -27,15 +29,16 @@ ansible.builtin.get_url: url: "{{ prometheus_tarball_url }}" dest: "{{ __prometheus_tmp.path }}/{{ prometheus_tarball_file }}" + mode: "0755" -- name: Extract tarball +- name: Extract Prometheus tarball ansible.builtin.unarchive: src: "{{ __prometheus_tmp.path }}/{{ prometheus_tarball_file }}" dest: "{{ prometheus_directory }}" extra_opts: --strip-components=1 remote_src: true -- name: Remove the temporary directory +- name: Remove the temporary directory when: __prometheus_tmp is defined ansible.builtin.file: path: "{{ __prometheus_tmp.path }}" @@ -54,34 +57,24 @@ state: directory recurse: true -- name: Set ownership of all files inside /etc/prometheus +- name: Set ownership of Prometheus files ansible.builtin.file: path: "{{ prometheus_directory }}" owner: "{{ prometheus_user }}" group: "{{ prometheus_group }}" + mode: "0755" recurse: true - name: Create Prometheus service template ansible.builtin.template: src: prometheus.service.j2 dest: "{{ prometheus_service_directory }}" - register: __prometheus_service - -- name: Start and enable prometheus service - when: __prometheus_service.changed - block: - - name: Reload systemd daemon - ansible.builtin.systemd: - daemon_reload: true - - - name: Enable and start prometheus service - ansible.builtin.systemd: - name: prometheus - state: started - enabled: true + mode: "0755" + notify: Enable Prometheus - name: Update Prometheus configuration ansible.builtin.template: src: prometheus.yml.j2 dest: /etc/prometheus/prometheus.yml - notify: restart prometheus + mode: "0755" + notify: Restart Prometheus