Ansible 处理程序 - 未从救援 block 中调用

标签 ansible ansible-handlers

考虑以下(为简洁起见而简化的)剧本,其中包含 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/

相关文章:

可靠的 : Split a string with multiple spaces as delimiter

ssh - 通过 ssh-tunnel 从本地计算机连接到远程服务器

django - 使用 Ansible 部署 django 应用程序时的媒体文件夹权限

ansible - 整个剧本仅运行一次Ansible处理程序

使用 group_by 时 Ansible 将看不到处理程序

ansible - 导入 ansible playbook 并使用主 playbook 中的变量

ansible - 无法将 list 脚本解析为 list

ansible - 如何在 Ansible 中执行任务之前强制运行处理程序?

ansible - 仅在需要时使用处理程序执行 Ansible 重启