修正 monitoring.yml 的 check 模式与 Patroni pending 判断

只读探测在 check 模式下照常执行;依赖真实执行结果的等待与验证在 check 模式跳过。
patronictl JSON 会省略空值字段,pending 判断先检查键是否存在。

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-01 16:20:15 +00:00
co-authored by Claude Opus 5.5
parent 0cd4a67e12
commit 567b42ed13
3 changed files with 17 additions and 6 deletions
@@ -8,6 +8,7 @@
ansible.builtin.command:
argv: [systemctl, is-active, "homelab-postgresql-{{ pg_instance }}.service"]
changed_when: false
check_mode: false
# 生产:Patroni 只在自身 reload 时把本地配置写成数据目录里的 postgresql.conf/pg_ident.conf,
# 所以文件视图能说明 Patroni 是否已接收新配置;预加载库是否已生效另看 current_setting。
# dev:ident_file 是 postmaster 级参数,视图读取的是“当前生效”的那个文件,
@@ -34,6 +35,7 @@
become: true
become_user: "{{ pg_profile.user }}"
changed_when: false
check_mode: false
register: pg_monitor_state
- name: 让 Patroni 接收新的本地配置(reload,不重启)
ansible.builtin.command:
@@ -45,7 +47,7 @@
argv: "{{ pg_monitor_state.cmd }}"
become: true
become_user: "{{ pg_profile.user }}"
when: pg_instance == 'prod' and pg_monitor_state.stdout.split('|')[0] != 't'
when: pg_instance == 'prod' and pg_monitor_state.stdout.split('|')[0] != 't' and not ansible_check_mode
changed_when: false
register: pg_monitor_reloaded
retries: 10
@@ -62,7 +64,7 @@
argv: "{{ pg_monitor_state.cmd }}"
become: true
become_user: "{{ pg_profile.user }}"
when: pg_instance == 'dev'
when: pg_instance == 'dev' and not ansible_check_mode
changed_when: false
register: pg_monitor_dev
retries: 10
@@ -22,6 +22,7 @@
become: true
become_user: "{{ pg_profile.user }}"
changed_when: false
check_mode: false
register: pg_monitor_role
- name: 创建或修正监控角色(无密码,仅能经本地 ident 映射登录)
ansible.builtin.command:
@@ -49,6 +50,8 @@
daemon_reload: "{{ pg_exporter_unit is changed }}"
enabled: true
state: "{{ 'restarted' if pg_exporter_unit is changed else 'started' }}"
# check 模式下 unit 文件尚未写入,systemd 找不到服务。
when: not (ansible_check_mode and pg_exporter_unit is changed)
# 同时证明 HBA/ident 已加载、角色可登录、exporter 能查询。
- name: 检查 exporter 已连上数据库
ansible.builtin.uri:
@@ -56,6 +59,7 @@
return_content: true
register: pg_exporter_metrics
changed_when: false
when: not ansible_check_mode
retries: 10
delay: 3
until: pg_exporter_metrics.status == 200 and pg_exporter_metrics.content is search('(?m)^pg_up 1$')