feat: Add Prometheus installation role and playbook
- Created Prometheus role with automated installation - Version: 2.48.1 - Configured remote_write to VictoriaMetrics (192.168.0.104:8428) - Added scrape configs for monitoring infrastructure - Created systemd service configuration - Successfully tested on 192.168.0.105
This commit is contained in:
121
roles/prometheus/tasks/main.yml
Normal file
121
roles/prometheus/tasks/main.yml
Normal file
@ -0,0 +1,121 @@
|
||||
---
|
||||
- name: Create prometheus system user
|
||||
user:
|
||||
name: "{{ prometheus_user }}"
|
||||
shell: /bin/false
|
||||
home: /nonexistent
|
||||
create_home: no
|
||||
system: yes
|
||||
state: present
|
||||
|
||||
- name: Create prometheus directories
|
||||
file:
|
||||
path: "{{ item }}"
|
||||
state: directory
|
||||
owner: "{{ prometheus_user }}"
|
||||
group: "{{ prometheus_group }}"
|
||||
mode: '0755'
|
||||
loop:
|
||||
- "{{ prometheus_config_dir }}"
|
||||
- "{{ prometheus_config_dir }}/rules"
|
||||
- "{{ prometheus_config_dir }}/file_sd"
|
||||
- "{{ prometheus_data_dir }}"
|
||||
|
||||
- name: Check if Prometheus is already installed
|
||||
stat:
|
||||
path: "{{ prometheus_binary_dir }}/prometheus"
|
||||
register: prometheus_binary
|
||||
|
||||
- name: Check Prometheus version
|
||||
shell: "{{ prometheus_binary_dir }}/prometheus --version 2>&1 | head -1 | awk '{print $3}'"
|
||||
register: prometheus_installed_version
|
||||
when: prometheus_binary.stat.exists
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
|
||||
- name: Download and install Prometheus
|
||||
block:
|
||||
- name: Download Prometheus {{ prometheus_version }}
|
||||
unarchive:
|
||||
src: "https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_version }}/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz"
|
||||
dest: /tmp
|
||||
remote_src: yes
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0755'
|
||||
|
||||
- name: Copy Prometheus binaries
|
||||
copy:
|
||||
src: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64/{{ item }}"
|
||||
dest: "{{ prometheus_binary_dir }}/{{ item }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0755'
|
||||
remote_src: yes
|
||||
loop:
|
||||
- prometheus
|
||||
- promtool
|
||||
notify: restart prometheus
|
||||
|
||||
- name: Copy console libraries
|
||||
copy:
|
||||
src: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64/{{ item }}/"
|
||||
dest: "{{ prometheus_config_dir }}/{{ item }}/"
|
||||
owner: "{{ prometheus_user }}"
|
||||
group: "{{ prometheus_group }}"
|
||||
remote_src: yes
|
||||
loop:
|
||||
- consoles
|
||||
- console_libraries
|
||||
|
||||
- name: Clean up downloaded files
|
||||
file:
|
||||
path: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64"
|
||||
state: absent
|
||||
when: not prometheus_binary.stat.exists or (prometheus_installed_version.stdout != prometheus_version)
|
||||
|
||||
- name: Configure Prometheus
|
||||
template:
|
||||
src: prometheus.yml.j2
|
||||
dest: "{{ prometheus_config_dir }}/prometheus.yml"
|
||||
owner: "{{ prometheus_user }}"
|
||||
group: "{{ prometheus_group }}"
|
||||
mode: '0644'
|
||||
backup: yes
|
||||
validate: "{{ prometheus_binary_dir }}/promtool check config %s"
|
||||
notify: reload prometheus
|
||||
|
||||
- name: Create Prometheus systemd service
|
||||
template:
|
||||
src: prometheus.service.j2
|
||||
dest: /etc/systemd/system/prometheus.service
|
||||
mode: '0644'
|
||||
notify: restart prometheus
|
||||
|
||||
- name: Ensure Prometheus is started and enabled
|
||||
systemd:
|
||||
name: prometheus
|
||||
state: started
|
||||
enabled: yes
|
||||
daemon_reload: yes
|
||||
|
||||
- name: Wait for Prometheus to be ready
|
||||
uri:
|
||||
url: "http://localhost:{{ prometheus_port }}/metrics"
|
||||
status_code: 200
|
||||
register: prometheus_health
|
||||
until: prometheus_health.status == 200
|
||||
retries: 10
|
||||
delay: 5
|
||||
|
||||
- name: Check Prometheus targets status
|
||||
uri:
|
||||
url: "http://localhost:{{ prometheus_port }}/api/v1/targets"
|
||||
register: targets_response
|
||||
|
||||
- name: Display Prometheus status
|
||||
debug:
|
||||
msg:
|
||||
- "Prometheus version: {{ prometheus_version }}"
|
||||
- "Prometheus URL: http://{{ ansible_host }}:{{ prometheus_port }}"
|
||||
- "Active targets: {{ targets_response.json.data.activeTargets | length }}"
|
||||
Reference in New Issue
Block a user