Merge pull request 'feat: add blackbox monitoring and app3 deployment' (#6) from ADDBlackboxapp3 into main

Reviewed-on: #6
This commit is contained in:
2026-02-04 10:21:09 +00:00
19 changed files with 1045 additions and 0 deletions

View File

@ -0,0 +1,104 @@
---
- name: Add Blackbox Exporter job to existing Prometheus config
hosts: 192.168.0.105
become: yes
tasks:
- name: Backup current config
copy:
src: /etc/prometheus/prometheus.yml
dest: /etc/prometheus/prometheus.yml.backup-blackbox-{{ ansible_date_time.epoch }}
remote_src: yes
tags: prometheus
- name: Check if blackbox job already exists
shell: |
grep -q 'job_name:.*blackbox' /etc/prometheus/prometheus.yml && echo "exists" || echo "not exists"
register: blackbox_exists
changed_when: false
tags: prometheus
- name: Add blackbox job to scrape_configs (if not exists)
blockinfile:
path: /etc/prometheus/prometheus.yml
insertbefore: '^remote_write:'
block: |
- job_name: blackbox
honor_timestamps: true
track_timestamps_staleness: false
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /probe
params:
module: [http_2xx]
scheme: http
follow_redirects: true
enable_http2: true
static_configs:
- targets:
# Внутренние сервисы стенда
- "http://192.168.0.110/"
- "http://192.168.0.111:9187/metrics"
- "http://192.168.0.112:8080/get"
- "http://192.168.0.100:3000/"
- "http://192.168.0.101:9100/metrics"
- "http://192.168.0.103:8200/ui/"
- "http://192.168.0.104:8428/metrics"
- "http://192.168.0.105:9090/metrics"
- "http://192.168.0.106:3000"
# Внешние домены
- "http://forgejo.pvenode.ru/"
- "http://grafana.pvenode.ru/"
- "http://prometheus.pvenode.ru/"
- "http://app1.pvenode.ru/"
- "http://wiki.pvenode.ru/"
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.0.112:8083
metric_relabel_configs:
- source_labels: [__address__]
separator: ;
regex: (.*)
target_label: instance
replacement: $1
action: replace
- source_labels: [__address__]
separator: ;
regex: ([^:]+):\d+
target_label: host
replacement: ${1}
action: replace
marker: "# {mark} ANSIBLE MANAGED BLOCK - blackbox"
when: blackbox_exists.stdout == "not exists"
tags: prometheus
- name: Check Prometheus configuration
command: promtool check config /etc/prometheus/prometheus.yml
register: promtool_check
failed_when: promtool_check.rc != 0
changed_when: false
tags: prometheus
- name: Show config check result
debug:
msg: "{{ promtool_check.stdout_lines }}"
when: promtool_check.rc == 0
tags: prometheus
- name: Reload Prometheus if config is valid
systemd:
name: prometheus
state: reloaded
when: promtool_check.rc == 0
tags: prometheus
- name: Show status
debug:
msg: |
Blackbox job {{ "added successfully" if promtool_check.rc == 0 else "failed to add" }}
Backup created: /etc/prometheus/prometheus.yml.backup-blackbox-{{ ansible_date_time.epoch }}
tags: prometheus

View File

@ -0,0 +1,151 @@
---
- name: Add correct Blackbox Exporter job
hosts: 192.168.0.105
become: yes
tasks:
- name: Backup current config
copy:
src: /etc/prometheus/prometheus.yml
dest: /etc/prometheus/prometheus.yml.backup-pre-blackbox-{{ ansible_date_time.epoch }}
remote_src: yes
tags: prometheus
- name: Check current line numbers
shell: |
echo "Last scrape_config job ends at line:"
grep -n "job_name: postgres" /etc/prometheus/prometheus.yml
echo ""
echo "Remote_write starts at line:"
grep -n "^remote_write:" /etc/prometheus/prometheus.yml
register: line_info
changed_when: false
tags: prometheus
- name: Create correct blackbox job config
copy:
dest: /tmp/blackbox-job.yml
content: |
- job_name: blackbox
honor_timestamps: true
track_timestamps_staleness: false
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /probe
params:
module: [http_2xx]
scheme: http
follow_redirects: true
enable_http2: true
static_configs:
- targets:
# Internal services
- "http://192.168.0.110/"
- "http://192.168.0.111:9187/metrics"
- "http://192.168.0.112:8080/get"
- "http://192.168.0.100:3000/"
- "http://192.168.0.101:9100/metrics"
- "http://192.168.0.103:8200/ui/"
- "http://192.168.0.104:8428/metrics"
- "http://192.168.0.105:9090/metrics"
- "http://192.168.0.106:3000"
# External domains
- "http://forgejo.pvenode.ru/"
- "http://grafana.pvenode.ru/"
- "http://prometheus.pvenode.ru/"
- "http://app1.pvenode.ru/"
- "http://wiki.pvenode.ru/"
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.0.112:8083
metric_relabel_configs:
- source_labels: [__address__]
separator: ;
regex: (.*)
target_label: instance
replacement: $1
action: replace
- source_labels: [__address__]
separator: ;
regex: ([^:]+):\d+
target_label: host
replacement: ${1}
action: replace
tags: prometheus
- name: Insert blackbox job before remote_write
shell: |
# Находим строку с remote_write
remote_line=$(grep -n "^remote_write:" /etc/prometheus/prometheus.yml | cut -d: -f1)
if [ -z "$remote_line" ]; then
echo "ERROR: remote_write not found"
exit 1
fi
# Создаем новый файл
cp /etc/prometheus/prometheus.yml /etc/prometheus/prometheus.yml.tmp
# Вставляем blackbox перед remote_write
head -n $((remote_line - 1)) /etc/prometheus/prometheus.yml > /etc/prometheus/prometheus.yml.new
cat /tmp/blackbox-job.yml >> /etc/prometheus/prometheus.yml.new
tail -n +$remote_line /etc/prometheus/prometheus.yml >> /etc/prometheus/prometheus.yml.new
# Заменяем старый файл
mv /etc/prometheus/prometheus.yml.new /etc/prometheus/prometheus.yml
rm -f /etc/prometheus/prometheus.yml.tmp
echo "Inserted at line $((remote_line - 1))"
args:
executable: /bin/bash
tags: prometheus
- name: Check Prometheus configuration
command: promtool check config /etc/prometheus/prometheus.yml
register: promtool_check
failed_when: promtool_check.rc != 0
changed_when: false
tags: prometheus
- name: Show config check result
debug:
msg: "{{ promtool_check.stdout_lines }}"
when: promtool_check.rc == 0
tags: prometheus
- name: Reload Prometheus
systemd:
name: prometheus
state: reloaded
when: promtool_check.rc == 0
tags: prometheus
- name: Verify blackbox job added
shell: |
sleep 2
echo "=== Checking if blackbox job exists ==="
if grep -q "job_name: blackbox" /etc/prometheus/prometheus.yml; then
echo "✓ Blackbox job found in config"
echo ""
echo "=== Checking Prometheus targets ==="
curl -s "http://localhost:9090/api/v1/targets" | python3 -c "
import json, sys
data = json.load(sys.stdin)
for target in data['data']['activeTargets']:
job = target['discoveredLabels'].get('job', 'N/A')
if 'blackbox' in job.lower():
print(f'✓ Blackbox target: {target[\"health\"]}')
print(f' URL: {target[\"scrapeUrl\"]}')
exit(0)
print('✗ Blackbox not in targets yet (may need 15s scrape interval)')
"
else
echo "✗ Blackbox job not found in config"
fi
args:
executable: /bin/bash
tags: prometheus

View File

@ -0,0 +1,105 @@
---
- name: Add Blackbox job to Prometheus config
hosts: 192.168.0.105
become: yes
tasks:
- name: Backup config
copy:
src: /etc/prometheus/prometheus.yml
dest: /etc/prometheus/prometheus.yml.backup-blackbox
remote_src: yes
- name: Create blackbox config file
copy:
dest: /tmp/blackbox-config.yml
content: |
# Blackbox Exporter monitoring
- job_name: blackbox
honor_timestamps: true
track_timestamps_staleness: false
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /probe
params:
module: [http_2xx]
scheme: http
follow_redirects: true
enable_http2: true
static_configs:
- targets:
- "http://192.168.0.110/"
- "http://192.168.0.111:9187/metrics"
- "http://192.168.0.112:8080/get"
- "http://192.168.0.100:3000/"
- "http://192.168.0.101:9100/metrics"
- "http://192.168.0.103:8200/ui/"
- "http://192.168.0.104:8428/metrics"
- "http://192.168.0.105:9090/metrics"
- "http://192.168.0.106:3000"
- "http://forgejo.pvenode.ru/"
- "http://grafana.pvenode.ru/"
- "http://prometheus.pvenode.ru/"
- "http://app1.pvenode.ru/"
- "http://wiki.pvenode.ru/"
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.0.112:8083
metric_relabel_configs:
- source_labels: [__address__]
separator: ;
regex: (.*)
target_label: instance
replacement: $1
action: replace
- source_labels: [__address__]
separator: ;
regex: ([^:]+):\d+
target_label: host
replacement: ${1}
action: replace
- name: Insert blackbox config before remote_write
shell: |
# Find remote_write line
remote_line=$(grep -n "^remote_write:" /etc/prometheus/prometheus.yml | head -1 | cut -d: -f1)
if [ -z "$remote_line" ]; then
echo "ERROR: remote_write not found"
exit 1
fi
# Insert blackbox config
head -n $((remote_line - 1)) /etc/prometheus/prometheus.yml > /tmp/prometheus-new.yml
cat /tmp/blackbox-config.yml >> /tmp/prometheus-new.yml
tail -n +$remote_line /etc/prometheus/prometheus.yml >> /tmp/prometheus-new.yml
# Replace original
mv /tmp/prometheus-new.yml /etc/prometheus/prometheus.yml
echo "Inserted at line $((remote_line - 1))"
- name: Validate config
command: promtool check config /etc/prometheus/prometheus.yml
register: config_check
changed_when: false
- name: Show validation result
debug:
msg: "{{ config_check.stdout_lines }}"
- name: Reload Prometheus
systemd:
name: prometheus
state: reloaded
when: config_check.rc == 0
- name: Check result
debug:
msg: |
Blackbox job {{ "successfully added" if config_check.rc == 0 else "failed to add" }}
Backup: /etc/prometheus/prometheus.yml.backup-blackbox

View File

@ -0,0 +1,103 @@
---
- name: Add Blackbox Exporter to Prometheus
hosts: 192.168.0.105
become: yes
tasks:
- name: Backup current config
copy:
src: /etc/prometheus/prometheus.yml
dest: /etc/prometheus/prometheus.yml.backup-{{ ansible_date_time.epoch }}
remote_src: yes
- name: Get line number where remote_write starts
shell: grep -n "^remote_write:" /etc/prometheus/prometheus.yml | cut -d: -f1
register: remote_line
changed_when: false
- name: Create blackbox job config file
copy:
dest: /tmp/blackbox-job.yml
content: |
- job_name: blackbox
honor_timestamps: true
track_timestamps_staleness: false
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: /probe
params:
module: [http_2xx]
scheme: http
follow_redirects: true
enable_http2: true
static_configs:
- targets:
- "http://192.168.0.110/"
- "http://192.168.0.111:9187/metrics"
- "http://192.168.0.112:8080/get"
- "http://192.168.0.100:3000/"
- "http://192.168.0.101:9100/metrics"
- "http://192.168.0.103:8200/ui/"
- "http://192.168.0.104:8428/metrics"
- "http://192.168.0.105:9090/metrics"
- "http://192.168.0.106:3000"
- "http://forgejo.pvenode.ru/"
- "http://grafana.pvenode.ru/"
- "http://prometheus.pvenode.ru/"
- "http://app1.pvenode.ru/"
- "http://wiki.pvenode.ru/"
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.0.112:8083
metric_relabel_configs:
- source_labels: [__address__]
separator: ;
regex: (.*)
target_label: instance
replacement: $1
action: replace
- source_labels: [__address__]
separator: ;
regex: ([^:]+):\d+
target_label: host
replacement: ${1}
action: replace
- name: Insert blackbox job before remote_write
shell: |
# Вставляем blackbox job перед remote_write
head -n $(({{ remote_line.stdout }} - 1)) /etc/prometheus/prometheus.yml > /tmp/prometheus-new.yml
cat /tmp/blackbox-job.yml >> /tmp/prometheus-new.yml
tail -n +{{ remote_line.stdout }} /etc/prometheus/prometheus.yml >> /tmp/prometheus-new.yml
mv /tmp/prometheus-new.yml /etc/prometheus/prometheus.yml
args:
executable: /bin/bash
- name: Check Prometheus configuration
command: promtool check config /etc/prometheus/prometheus.yml
register: promtool_check
changed_when: false
- name: Show config status
debug:
msg: "{{ promtool_check.stdout_lines }}"
- name: Reload Prometheus if config valid
systemd:
name: prometheus
state: reloaded
when: promtool_check.rc == 0
- name: Verify blackbox job
shell: |
echo "Checking if blackbox job was added..."
if grep -q "job_name: blackbox" /etc/prometheus/prometheus.yml; then
echo "SUCCESS: Blackbox job found in config"
else
echo "ERROR: Blackbox job not found"
fi
changed_when: false

View File

@ -0,0 +1,76 @@
---
- name: Configure Prometheus for Blackbox monitoring
hosts: 192.168.0.105
become: yes
vars:
blackbox_targets:
# Основные сервисы стенда (из ИП)
- "http://192.168.0.110/"
- "http://192.168.0.111:9187/metrics" # postgres_exporter
- "http://192.168.0.112:8080/get" # httpbin
- "http://192.168.0.112:8081/metrics" # cadvisor
- "http://192.168.0.100:3000/" # forgejo
- "http://192.168.0.101:9100/metrics" # ansible node_exporter
- "http://192.168.0.103:8200/ui/" # vault
- "http://192.168.0.104:8428/metrics" # victoriametrics
- "http://192.168.0.105:9090/metrics" # prometheus
- "http://192.168.0.106:3000" # grafana
# Основные домены (первые для теста)
- "http://forgejo.pvenode.ru/"
- "http://grafana.pvenode.ru/"
- "http://prometheus.pvenode.ru/"
- "http://app1.pvenode.ru/"
- "http://wiki.pvenode.ru/"
tasks:
- name: Backup original Prometheus config
copy:
src: /etc/prometheus/prometheus.yml
dest: /etc/prometheus/prometheus.yml.backup-{{ ansible_date_time.epoch }}
remote_src: yes
tags: prometheus
- name: Add blackbox exporter to Prometheus
blockinfile:
path: /etc/prometheus/prometheus.yml
insertafter: ' # cAdvisor container metrics'
block: |
# Blackbox Exporter probes
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
{% for target in blackbox_targets %}
- "{{ target }}"
{% endfor %}
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.0.112:8083 # blackbox-exporter
marker: "# {mark} ANSIBLE MANAGED BLOCK - blackbox"
tags: prometheus
- name: Check Prometheus configuration
command: promtool check config /etc/prometheus/prometheus.yml
register: promtool_check
failed_when: promtool_check.rc != 0
tags: prometheus
- name: Reload Prometheus
systemd:
name: prometheus
state: reloaded
when: promtool_check.rc == 0
tags: prometheus
- name: Show configured targets
debug:
msg: "Added {{ blackbox_targets|length }} targets to blackbox monitoring"
tags: prometheus

View File

@ -0,0 +1,43 @@
---
- name: Configure Prometheus for Blackbox monitoring
hosts: 192.168.0.105
become: yes
vars:
blackbox_targets: "{{ hostvars['192.168.0.112']['blackbox_targets'] }}"
tasks:
- name: Add blackbox exporter to Prometheus
blockinfile:
path: /etc/prometheus/prometheus.yml
insertafter: ' # cAdvisor container metrics'
block: |
# Blackbox Exporter probes
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
{% for target in blackbox_targets %}
- {{ target.url }}
{% endfor %}
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.0.112:8083 # blackbox-exporter
marker: "# {mark} ANSIBLE MANAGED BLOCK - blackbox"
- name: Check Prometheus configuration
command: promtool check config /etc/prometheus/prometheus.yml
register: promtool_check
failed_when: promtool_check.rc != 0
- name: Reload Prometheus
systemd:
name: prometheus
state: reloaded
when: promtool_check.rc == 0

View File

@ -0,0 +1,16 @@
---
- name: Deploy Blackbox Exporter on App3
hosts: 192.168.0.112
become: yes
gather_facts: yes
pre_tasks:
- name: Ensure Docker is installed
include_role:
name: docker
apply:
tags: docker
roles:
- role: blackbox_exporter
tags: blackbox

View File

@ -0,0 +1,16 @@
---
- name: Deploy cAdvisor on App3
hosts: 192.168.0.112
become: yes
gather_facts: yes
pre_tasks:
- name: Ensure Docker is installed
include_role:
name: docker
apply:
tags: docker
roles:
- role: cadvisor
tags: cadvisor

View File

@ -0,0 +1,9 @@
---
- name: Deploy Docker on App3
hosts: 192.168.0.112
become: yes
gather_facts: yes
roles:
- role: docker
tags: docker

View File

@ -0,0 +1,16 @@
---
- name: Deploy httpbin on App3
hosts: 192.168.0.112
become: yes
gather_facts: yes
pre_tasks:
- name: Ensure Docker is installed
include_role:
name: docker
apply:
tags: docker
roles:
- role: httpbin
tags: httpbin