如果任何任务失败,则执行任务(或处理程序) [英] Execute task (or handler) if any task failed

查看:129
本文介绍了如果任何任务失败,则执行任务(或处理程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ansible将Django网站部署到我的服务器(生产,暂存等)中,并且我希望在且仅当任何任务失败时才收到通知(在这种情况下为松弛).

I am using Ansible to deploy a Django website into my servers (production, staging, etc), and I would like to get a notification (via slack in this case) if and only if any task fails.

我只能弄清楚如果指定的任务失败了怎么办(所以我想我可以为所有任务添加一个处理程序),但是直觉告诉我必须有一个更简单,更优雅的选择.

I can only figure out how to do it if a specified task fails (so I guess I could add a handler to all tasks), but intuition tells me there has to be an easier and more elegant option.

基本上我在想的是:

---
- hosts: "{{hosts_to_deploy}}"

- tasks: 

   [...]

  - name: notify slack of deploy failure
    local_action:
      module: slack
      token: "{{slack_token}}"
      msg: "Deploy failed on {{inventory_hostname}}"
    when: # any task failed

我一直在深入Ansible文档,特别是在错误处理部分, SO的答案和答案,但我一直在努力寻找问题的答案.因此,任何帮助将不胜感激.

I have been diving into the Ansible documentation, specially in the error handling section, and answers here at SO, but I'm struggling to find an answer to my question. So any help will be much appreciated.

推荐答案

我认为处理程序不是解决方案,因为仅当任务报告更改状态时才会通知处理程序.在失败状态下,不会通知处理程序.

I don't think a handler is a solution, because a handler will only be notified if the task reports a changed state. On a failed state the handler will not be notified.

此外,如果剧本失败,则默认情况下不会触发处理程序.但这可以改变.为此,您需要在ansible.cfg中设置:

Also, handlers by default will not be fired if the playbook failed. But that can be changed. For that you will need to set this in your ansible.cfg:

force_handlers = True

但是,是的,有更好的选择.

But yes, there are better options available.

如果您使用Ansible 2,则可以使用新的功能.将任务分组在一起,并有一个救援部分,只有在任何任务失败时才会触发.

If you use Ansible 2 you can use the new blocks feature. Blocks group tasks together and have a rescue section which will be only triggered if any of the tasks have failed.

tasks:
  - block:
      - here
      - go
      - all
      - your
      - tasks
    rescue:
      - name: notify slack of deploy failure
        local_action:
          module: slack
          token: "{{slack_token}}"
          msg: "Deploy failed on {{inventory_hostname}}"

另一个选择,如果您使用的是Ansible 1.x,则可能特别有趣,它可能是回调插件.顾名思义,您可以编写可在各种事件上触发的回调.

Another option and especially interesting if you're using Ansible 1.x might be callback plugins. As the name suggests with these kind of plugins you can write callbacks which can be fired on various events.

同样,如果您使用的是Ansible 2,那么您很幸运,因为已经有一个松弛的回调插件可用:

Again, if you're using Ansible 2 you're lucky, because there already is a slack callback plugin available: https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/slack.py

要使用此插件,您需要在ansible.cfg中启用它:

To use this plugin you need to enable it in your ansible.cfg:

callback_whitelist = slack

并在系统上定义一些环境变量以进行配置:

And define some environment variables on your system for configuration:

 This plugin makes use of the following environment variables:
    SLACK_WEBHOOK_URL (required): Slack Webhook URL
    SLACK_CHANNEL     (optional): Slack room to post in. Default: #ansible
    SLACK_USERNAME    (optional): Username to post as. Default: ansible
    SLACK_INVOCATION  (optional): Show command line invocation
                                  details. Default: False

该插件可能需要进行一些修改才能满足您的需求.如果是这种情况,请复制源并将其相对于您的剧本存储为callback_plugins/custom_slack.py,然后在您的ansible.cfg中启用它:

That plugin might need some modifications to fit your needs. If that's the case copy the source and store it relative to your playbook as callback_plugins/custom_slack.py and then enable it in your ansible.cfg:

callback_whitelist = custom_slack

如果您使用Ansible 1.x,则必须查看如何进行转换.该API是不同的,可以在这里找到旧API的示例: https://github .com/ansible/ansible/tree/v1.9.4-1/plugins/callbacks

If you use Ansible 1.x you'll have to see how you can convert it. The API is different, examples for the old API can be found here: https://github.com/ansible/ansible/tree/v1.9.4-1/plugins/callbacks

这篇关于如果任何任务失败,则执行任务(或处理程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆