ansible playbook 配置批量变更失败告警的核心做法是结合 notify 触发器与自定义的 fail_json 任务,在 handlers 中统一处理错误。当任意主机执行失败时,通过 failed_when 或 ignore_errors: yes 收集状态,并在最后阶段检查 ansible_failed_host 或 play_hosts 的差异来触发告警。这样做可以在不中断整个 playbook 的情况下,精准定位失败节点并发送通知。

如何检测单个任务执行失败并触发通知?
要检测单个任务的失败,最直接的方法是在该任务后添加一个 meta: flush_handlers 或者利用 changed_when 和 failed_when 的组合。但更稳健的做法是使用 block 和 rescue 结构,或者在任务结束后立即检查返回值。这里我们采用一种通用的模式:在关键变更后,使用 assert 模块验证结果,如果验证失败,则设置一个事实(fact)标记为失败,并触发 handler。

---
- hosts: all
tasks:
- name: Perform critical change
command: /usr/bin/critical_operation
register: result
changed_when: result.rc != 0
failed_when: false # 防止立即终止
- name: Check if operation succeeded
assert:
that:
- result.rc == 0
fail_msg: "Critical operation failed on {{ inventory_hostname }}"
rescue:
- name: Mark failure and notify
set_fact:
task_failed: true
notify: Send Failure Alert
handlers:
- name: Send Failure Alert
debug:
msg: "Alert triggered for {{ inventory_hostname }} due to task failure."
listen: "Send Failure Alert"
上述代码中,critical_operation 是一个模拟命令。通过 failed_when: false 阻止 Ansible 在命令返回非零退出码时立即停止,从而允许后续的 assert 模块捕获错误。如果 assert 失败,进入 rescue 块,设置 task_failed 事实并触发名为 Send Failure Alert 的 handler。这种模式确保了即使底层命令失败,Playbook 也能继续执行到最后的汇总阶段,或者至少能记录具体的失败主机。
如何在多主机环境中汇总失败并发送全局告警?
在批量变更场景中,单个主机的失败可能不足以触发全局告警,但如果多个主机失败,或者所有主机都失败,则需要发送全局通知。这通常需要在 Playbook 的最后阶段,通过检查 ansible_play_hosts_all 和 ansible_failed_hosts 来实现。我们可以创建一个专门的任务,在所有主机执行完毕后运行,以检查是否有主机失败,并据此触发告警。

---
- hosts: all
tasks:
- name: Simulate potential failure
command: /bin/false
register: cmd_result
failed_when: false
- name: Record failure status
set_fact:
host_failed: "{{ ansible_failed_result is defined or cmd_result.rc != 0 }}"
post_tasks:
- name: Check for any failures across the batch
delegate_to: localhost
run_once: true
block:
- name: Gather failure info
set_fact:
failed_hosts: "{{ ansible_play_hosts_all | difference(ansible_ok_hosts) }}"
- name: Trigger global alert if any host failed
assert:
that:
- failed_hosts | length == 0
fail_msg: "Batch change failed on hosts: {{ failed_hosts | join(', ') }}"
rescue:
- name: Send Global Alert
debug:
msg: "GLOBAL ALERT: Batch change failed on {{ failed_hosts | join(', ') }}"
listen: "Global Alert"
在这个示例中,post_tasks 部分确保在所有主机的主任务完成后执行。delegate_to: localhost 和 run_once: true 保证这段逻辑只在控制节点上运行一次。ansible_play_hosts_all | difference(ansible_ok_hosts) 计算出了所有未成功的主机列表。如果这个列表不为空,assert 模块会失败,从而触发 rescue 中的全局告警任务。这种方法避免了在每个主机上单独发送告警导致的重复通知,而是提供了一个集中的失败视图。
基于实际输入条件,如何处理特定命令返回码的告警阈值?
在实际操作中,并非所有非零返回码都代表严重错误。例如,某些命令可能返回 1 表示“无变化”,而返回 2 表示“致命错误”。为了精确控制告警,我们需要根据具体的返回码设置阈值。假设我们的 critical_operation 命令在成功时返回 0,在无变化时返回 1,在错误时返回 2。我们可以修改之前的逻辑,只在对返回码 2 进行告警。
---
- hosts: all
tasks:
- name: Run operation with specific exit codes
command: /usr/bin/operation_with_codes
register: op_result
failed_when: false
- name: Evaluate exit code for alerting
set_fact:
should_alert: "{{ op_result.rc == 2 }}"
- name: Notify on fatal error
debug:
msg: "Fatal error on {{ inventory_hostname }}, RC: {{ op_result.rc }}"
when: should_alert
notify: Send Fatal Error Alert
handlers:
- name: Send Fatal Error Alert
debug:
msg: "Sending fatal error alert for {{ inventory_hostname }}"
listen: "Send Fatal Error Alert"
在这个改进的版本中,should_alert 事实仅当 op_result.rc 等于 2 时才为真。这意味着如果命令返回 1(无变化),不会触发任何告警。这种细粒度的控制对于生产环境至关重要,因为它减少了噪音,确保运维团队只关注真正的问题。通过调整 set_fact 中的条件,可以适应不同命令的语义,从而实现精准的批量变更失败告警。

















