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

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

问题描述

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

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 文档,特别是在 错误处理 部分,和答案在这里,但我正在努力寻找我的问题的答案.因此,任何帮助将不胜感激.

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,则可以使用新的 blocks 功能.块将任务组合在一起,并有一个救援部分,只有在任何任务失败时才会触发.

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 你很幸运,因为已经有一个可用的 slack 回调插件:https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/slack.py

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天全站免登陆