Ansible 处理程序 - 不是从救援块调用
Ansible handler - not called from rescue 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
处理程序仅在更改时调用,任务 meta: flush_handlers
将执行处理程序 只有当 首先有要刷新的处理程序时,这不是你的情况。
As we’ve mentioned, modules should be idempotent and can relay when they have made a change on the remote system. Playbooks recognize this and have a basic event system that can be used to respond to change.
为了让它工作,您将不得不以某种方式创建一个改变的状态。这可能是通过使用 changed_when
:
- 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"
changed_when: true
notify: failure_deployment_slack_handler
- meta: flush_handlers
相关:
你的精简版剧本中的 'error' 是 debug
永远不会向 ansible 报告 changed
,因此,处理程序不会触发。
在我用 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"
}
考虑以下(为简洁起见进行了简化)带有块和救援块的剧本
- 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
处理程序仅在更改时调用,任务 meta: flush_handlers
将执行处理程序 只有当 首先有要刷新的处理程序时,这不是你的情况。
As we’ve mentioned, modules should be idempotent and can relay when they have made a change on the remote system. Playbooks recognize this and have a basic event system that can be used to respond to change.
为了让它工作,您将不得不以某种方式创建一个改变的状态。这可能是通过使用 changed_when
:
- 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"
changed_when: true
notify: failure_deployment_slack_handler
- meta: flush_handlers
相关:
你的精简版剧本中的 'error' 是 debug
永远不会向 ansible 报告 changed
,因此,处理程序不会触发。
在我用 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"
}