Ansible:我们可以异步运行包含剧本吗? [英] Ansible: Can we run an include playbook Asynchronously?

查看:19
本文介绍了Ansible:我们可以异步运行包含剧本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣了解 Ansible 是否可以异步运行包含的剧本?

I'm interested to learn if Ansible can run an included playbook asynchronously?

基本上我要做的是运行一个任务即发即忘,稍后再检查".当我稍后检查它时,我还想发送一个带有结果的 slack 通知.

Basically what I'm trying to do is run a task "Fire and forget, check on it later." When I check on it later I also want to send a slack notification with the result.

但是,我注意到包含的松弛通知剧本需要比预期更长的时间才能完成,因此它可以支撑剧本的其余部分.

However I've notice the included playbook for slack notification takes a little longer than expected to complete and hence it holds up the rest of the playbook.

我想要的是异步包含的 playbook 以获取 slack 通知,以便当前的 playbook 继续.

What I want is to async the included playbook for slack notification so that the current playbook continues.

例如我有一个看起来像这样的 playbook.yml 文件:

For instance I have a playbook.yml file that looks like:

- hosts: localhost

  tasks:
  - name: Fire and forget task
    shell: some_task.sh
           chdir=/tmp/
    register: fire_and_forget_task
    async: 3600
    poll: 0


  - name: Check on fire and forget task
    async_status: jid={{ fire_and_forget_task.ansible_job_id }}
    register: task_status
    until: task_status.finished
    retries: 100
    ignore_errors: yes


  - name: Send slack success msg
    include: slack.yml msg="Fire and forget task SUCCESS"
    when: task_status.stdout is defined and 
          'SUCCESS' in fire_and_forget_task.stdout
    async: 3600
    poll: 0


  - name: Send slack failed msg
    include: slack.yml msg="Fire and forget task FAILED"
    when: task_status.stdout is defined and 
          'FAILED' in fire_and_forget_task.stdout
    async: 3600
    poll: 0

我的 slack.yml 文件如下所示:

My slack.yml file looks like:

  - name: Send notification message via Slack
    local_action:
      module: slack
      token: <REDACTED>
      attachments:
      - text: "{{ msg }}"
        color: "#83F52C"
        title: "Ansible Status {{ lookup('pipe','date') }}"

使用上面的剧本,Send slack success msg"任务需要非常长的时间来执行这样一个简单的任务.尽管我已经明确声明它应该异步运行,但它似乎不是异步运行的.

With the above playbook, the "Send slack success msg" task takes an awfully long time to execute for a simple task like that. It seems its not running asynchronously even though I have explicitly stated it should.

达到预期结果的最佳方法是什么?谢谢.

What is the best way to achieve the desired outcome? Thank you.

推荐答案

include 不能使用 async 关键字.
如果你的 slack.yml 就这么简单,只需用一个调用替换你的包含内容:

include can't use async keyword.
If your slack.yml is that simple, just replace your include stuff with a single call:

- name: Send notification message via Slack
  local_action:
    module: slack
    token: <REDACTED>
    attachments:
    - text: "Task result {{ (task_status.stdout is defined and 'SUCCESS' in task_status.stdout) | ternary('SUCCESS','FAILURE') }}"
      color: "#83F52C"
      title: "Ansible Status {{ lookup('pipe','date') }}"
  async: 3600
  poll: 0

附言但我不明白如果您有一个长时间运行的任务(具有高 asyncretries 数字),那么单个 Slack HTTP 调用是如何减慢您的剧本的...

P.S. but I don't understand how is single Slack HTTP-call slowing down your playbook if you have a long-running task (with high async and retries numbers) just before it...

这篇关于Ansible:我们可以异步运行包含剧本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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