Files
ansible-config/playbooks/add-blackbox-simple.yml

104 lines
3.6 KiB
YAML
Raw Normal View History

---
- 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