Skip to content
Merged
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
78 changes: 65 additions & 13 deletions roles/prometheus/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,69 @@
<!--
Copyright 2024 Cloudera, Inc.
# prometheus

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
Install Prometheus.

https://www.apache.org/licenses/LICENSE-2.0
This role automates the installation of the Prometheus monitoring system from its official distribution archive. It sets up the necessary directories for configuration and the time-series database (TSDB), creates a dedicated system user and group for the service, and installs a basic Prometheus configuration to get started.

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.
-->
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.
```
6 changes: 3 additions & 3 deletions roles/prometheus/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
19 changes: 16 additions & 3 deletions roles/prometheus/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
59 changes: 59 additions & 0 deletions roles/prometheus/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -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
33 changes: 13 additions & 20 deletions roles/prometheus/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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 }}"
Expand All @@ -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
Loading