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