问题
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.
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
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.
回答1:
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. 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...
来源:https://stackoverflow.com/questions/39197845/ansible-can-we-run-an-include-playbook-asynchronously