【问题标题】:Ansible handler - not called from rescue blockAnsible 处理程序 - 不从救援块调用
【发布时间】:2023-04-06 23:00:01
【问题描述】:

考虑以下(为简洁起见而简化)带有块和救援块的剧本

- 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

【问题讨论】:

    标签: ansible ansible-handlers


    【解决方案1】:

    您的剧本精简版中的“错误”是 debug 永远不会以 changed 的身份报告给 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"
    }
    

    【讨论】:

      【解决方案2】:

      仅在更改时调用处理程序,任务meta: flush_handlers 将执行处理程序仅当首先有要刷新的处理程序,这不是您的情况。

      正如我们所提到的,模块应该是幂等的,并且可以在它们对远程系统进行更改时进行中继。 Playbook 认识到这一点,并有一个基本的事件系统可用于响应变化

      来源:https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#handlers-running-operations-on-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
      

      相关:https://stackoverflow.com/a/62183353/2123530

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-24
        • 2012-02-26
        • 1970-01-01
        • 2023-01-13
        相关资源
        最近更新 更多