Ansible: Can we run an include playbook Asynchronously?

你。 提交于 2019-12-06 08:15:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!