考虑以下(为简洁起见而简化的)剧本,其中包含 block 和救援 block
- name: deploy block
block:
- name: debug meta
debug: var=meta
- name: create/update configmap with new data
k8s:
state: present
namespace: "{{ namespace }}"
definition: "{{ lookup('template', 'configmap.tpl.yml') }}"
kind: ConfigMap
notify:
- successfull_deployment_slack_handler
rescue:
- name: Something went wrong handler
debug:
msg: "Something has failed in the playbook"
notify: failure_deployment_slack_handler
- meta: flush_handlers
在愉快的路径中测试它有效并按预期调用处理程序。
但是,当我在测试中失败时,我确实看到了预期的调试消息,但实际的处理程序并未被调用(我已将其换成调试消息以进行验证)。
是的,我尝试添加meta:flush_handlers
。
我怎样才能做到这一点?
Ansible 版本:2.9.9
最佳答案
你的精简版剧本中的“错误”是debug
永远不会向ansible报告已更改
,因此处理程序不会触发。
在这个剧本中,我用 shell
替换了 rescue
debug
,处理程序被调用
---
- hosts: all
handlers:
- name: handler
debug:
msg: handler
tasks:
- name: deploy block
block:
- name: debug meta
debug:
msg: in the block
- fail:
msg: failing
rescue:
- name: Something went wrong handler
shell: date
notify: handler
结果
TASK [debug meta] **************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "in the block"
}
TASK [fail] ********************************************************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "failing"}
TASK [Something went wrong handler] ********************************************************************************************************************************************************************************************************************************************
changed: [localhost]
RUNNING HANDLER [handler] ******************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "handler"
}
关于Ansible 处理程序 - 未从救援 block 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63139921/