Search code examples
ansibleansible-handlers

Ansible handler - not called from rescue block


Consider the following (reduced for brevity) playbook with a block and 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

Testing this in the happy path works and calls the handlers as expected.
But when I fail the tasks in my testing I do see the expected debug message, but the actual handler isn't called (I've traded it for a debug message to verify).

Yes, I've tried adding meta: flush_handlers.

How can I make this work?

Ansible version: 2.9.9


Solution

  • The 'error' in your slim version of the playbook is that debug will never report to ansible as changed and because of that, the handler wont trigger.

    In this playbook where i've replaced your rescue debug with a shell, the handler is called

    ---
    - 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
    

    results in

    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"
    }