feat: initial installation role for prometheus
This commit is contained in:
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