feat: initial installation role for prometheus
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.retry
|
||||
39
README.md
Normal file
39
README.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# ansible-role-prometheus
|
||||
|
||||
Installs Prometheus from GitHub releases on Linux. Architecture is auto-detected.
|
||||
The role is idempotent: it only downloads and extracts when the version changes.
|
||||
|
||||
The service user and group must be created beforehand.
|
||||
Prometheus configuration (`prometheus.yml`) is out of scope for this role.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Ansible >= 2.14
|
||||
|
||||
## Role Variables
|
||||
|
||||
| Variable | Default | Description |
|
||||
|---|---|---|
|
||||
| `prometheus_version` | `""` | Version to install (empty = latest) |
|
||||
| `prometheus_user` | `prometheus` | User to run Prometheus |
|
||||
| `prometheus_group` | `prometheus` | Group to run Prometheus |
|
||||
| `prometheus_install_dir` | `/opt/prometheus` | Binary and assets directory |
|
||||
| `prometheus_data_dir` | `/var/lib/prometheus` | TSDB storage directory |
|
||||
| `prometheus_config_dir` | `/etc/prometheus` | Configuration directory |
|
||||
| `prometheus_service_enabled` | `true` | Enable service at boot |
|
||||
| `prometheus_service_state` | `started` | Service state after installation |
|
||||
|
||||
## Example Playbook
|
||||
|
||||
```yaml
|
||||
- hosts: monitoring_servers
|
||||
roles:
|
||||
- role: ansible-role-prometheus
|
||||
vars:
|
||||
prometheus_user: prometheus
|
||||
prometheus_group: prometheus
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
9
defaults/main.yml
Normal file
9
defaults/main.yml
Normal file
@@ -0,0 +1,9 @@
|
||||
---
|
||||
prometheus_version: ""
|
||||
prometheus_user: prometheus
|
||||
prometheus_group: prometheus
|
||||
prometheus_install_dir: /opt/prometheus
|
||||
prometheus_data_dir: /var/lib/prometheus
|
||||
prometheus_config_dir: /etc/prometheus
|
||||
prometheus_service_enabled: true
|
||||
prometheus_service_state: started
|
||||
1
handlers/main.yml
Normal file
1
handlers/main.yml
Normal file
@@ -0,0 +1 @@
|
||||
---
|
||||
15
meta/main.yml
Normal file
15
meta/main.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
---
|
||||
galaxy_info:
|
||||
author: mathieu
|
||||
description: Install Prometheus binary on Linux
|
||||
license: MIT
|
||||
min_ansible_version: "2.14"
|
||||
platforms:
|
||||
- name: Debian
|
||||
versions: [bullseye, bookworm]
|
||||
- name: Ubuntu
|
||||
versions: [focal, jammy, noble]
|
||||
- name: EL
|
||||
versions: ["8", "9"]
|
||||
galaxy_tags: [prometheus, monitoring, metrics]
|
||||
dependencies: []
|
||||
125
tasks/main.yml
Normal file
125
tasks/main.yml
Normal file
@@ -0,0 +1,125 @@
|
||||
---
|
||||
- name: Set architecture fact
|
||||
ansible.builtin.set_fact:
|
||||
_prometheus_arch: "{{ {'x86_64': 'amd64', 'aarch64': 'arm64', 'armv7l': 'armv7'}.get(ansible_architecture, 'amd64') }}"
|
||||
|
||||
- name: Get Prometheus release info from GitHub
|
||||
ansible.builtin.uri:
|
||||
url: >-
|
||||
{{ 'https://api.github.com/repos/prometheus/prometheus/releases/latest'
|
||||
if prometheus_version == ''
|
||||
else 'https://api.github.com/repos/prometheus/prometheus/releases/tags/v' + prometheus_version }}
|
||||
headers:
|
||||
Accept: "application/vnd.github.v3+json"
|
||||
return_content: true
|
||||
register: _prometheus_release
|
||||
|
||||
- name: Set version and download URL facts
|
||||
ansible.builtin.set_fact:
|
||||
_prometheus_version_tag: "{{ _prometheus_release.json.tag_name }}"
|
||||
_prometheus_download_url: >-
|
||||
{{ _prometheus_release.json.assets
|
||||
| selectattr('name', 'search', 'linux-' + _prometheus_arch + '.tar.gz')
|
||||
| map(attribute='browser_download_url')
|
||||
| first }}
|
||||
|
||||
- name: Read installed version
|
||||
ansible.builtin.slurp:
|
||||
src: "{{ prometheus_install_dir }}/.version"
|
||||
register: _prometheus_installed_raw
|
||||
ignore_errors: true
|
||||
become: true
|
||||
|
||||
- name: Set installed version fact
|
||||
ansible.builtin.set_fact:
|
||||
_prometheus_installed_version: >-
|
||||
{{ (_prometheus_installed_raw.content | b64decode | trim)
|
||||
if _prometheus_installed_raw is not failed else '' }}
|
||||
|
||||
- name: Install or upgrade Prometheus
|
||||
when: _prometheus_installed_version != _prometheus_version_tag
|
||||
block:
|
||||
- name: Download Prometheus archive
|
||||
ansible.builtin.get_url:
|
||||
url: "{{ _prometheus_download_url }}"
|
||||
dest: /tmp/prometheus.tar.gz
|
||||
mode: '0644'
|
||||
|
||||
- name: Create Prometheus installation directory
|
||||
ansible.builtin.file:
|
||||
path: "{{ prometheus_install_dir }}"
|
||||
state: directory
|
||||
owner: "{{ prometheus_user }}"
|
||||
group: "{{ prometheus_group }}"
|
||||
mode: '0755'
|
||||
become: true
|
||||
|
||||
- name: Extract Prometheus archive
|
||||
ansible.builtin.unarchive:
|
||||
src: /tmp/prometheus.tar.gz
|
||||
dest: "{{ prometheus_install_dir }}"
|
||||
remote_src: true
|
||||
extra_opts: [--strip-components=1]
|
||||
owner: "{{ prometheus_user }}"
|
||||
group: "{{ prometheus_group }}"
|
||||
become: true
|
||||
|
||||
- name: Write installed version file
|
||||
ansible.builtin.copy:
|
||||
content: "{{ _prometheus_version_tag }}"
|
||||
dest: "{{ prometheus_install_dir }}/.version"
|
||||
owner: "{{ prometheus_user }}"
|
||||
group: "{{ prometheus_group }}"
|
||||
mode: '0644'
|
||||
become: true
|
||||
|
||||
- name: Remove downloaded archive
|
||||
ansible.builtin.file:
|
||||
path: /tmp/prometheus.tar.gz
|
||||
state: absent
|
||||
|
||||
- name: Create Prometheus directories
|
||||
ansible.builtin.file:
|
||||
path: "{{ item }}"
|
||||
state: directory
|
||||
owner: "{{ prometheus_user }}"
|
||||
group: "{{ prometheus_group }}"
|
||||
mode: '0755'
|
||||
become: true
|
||||
loop:
|
||||
- "{{ prometheus_data_dir }}"
|
||||
- "{{ prometheus_config_dir }}"
|
||||
|
||||
- name: Create Prometheus systemd service
|
||||
ansible.builtin.copy:
|
||||
content: |
|
||||
[Unit]
|
||||
Description=Prometheus
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User={{ prometheus_user }}
|
||||
Group={{ prometheus_group }}
|
||||
ExecStart={{ prometheus_install_dir }}/prometheus \
|
||||
--config.file={{ prometheus_config_dir }}/prometheus.yml \
|
||||
--storage.tsdb.path={{ prometheus_data_dir }} \
|
||||
--web.console.templates={{ prometheus_install_dir }}/consoles \
|
||||
--web.console.libraries={{ prometheus_install_dir }}/console_libraries
|
||||
Restart=on-failure
|
||||
RestartSec=5s
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
dest: /etc/systemd/system/prometheus.service
|
||||
mode: '0644'
|
||||
become: true
|
||||
|
||||
- name: Enable and start Prometheus service
|
||||
ansible.builtin.systemd:
|
||||
name: prometheus
|
||||
enabled: "{{ prometheus_service_enabled }}"
|
||||
state: "{{ prometheus_service_state }}"
|
||||
daemon_reload: true
|
||||
become: true
|
||||
Reference in New Issue
Block a user