Ansible:我们可以异步运行包含剧本吗? [英] Ansible: Can we run an include playbook Asynchronously?
问题描述
我有兴趣了解Ansible是否可以异步运行附带的剧本吗?
I'm interested to learn if Ansible can run an included playbook asynchronously?
基本上,我想做的是运行一个任务点火后忘记,稍后再检查".当我稍后检查它时,我也想发送带有结果的松弛通知.
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.
我想要的是异步将随附的剧本进行松弛通知,以便当前剧本继续播放.
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') }}"
在上面的剧本中,发送松弛成功消息"任务要花费很长时间才能完成类似这样的简单任务.即使我已经明确指出它应该似乎也不异步运行.
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内容:
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
P.S.但是我不明白,如果您之前有一个长期运行的任务(async
和retries
的数字很高),那么单个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屋!