Roman Zinger
Roman Zinger

Reputation: 152

Ansible - run two tasks in parallel

My playbook is as following... this is related to one host not several at once:

---------------- Deregister from TG's

- name: DeRegister instance ID from a Public TG
  elb_target:
    target_group_name: "{{ TG_public }}"
    target_id: "{{ Instance_ID }}"
    state: absent
    target_status: unused                            ## wait until this status
    target_status_timeout: 300                       ## in seconds
    profile: "{{ profile_ID }}"                      ## specify AWS profile 
  delegate_to: 127.0.0.1                             ## Execute this stage locally

- name: DeRegister instance ID from a Private TG
  elb_target:
    target_group_name: "{{ TG_private }}"
    target_id: "{{ Instance_ID }}"
    state: absent
    target_status: unused                            ## wait until this status
    target_status_timeout: 300                       ## in seconds
    profile: "{{ profile_ID }}"                      ## specify AWS profile
  delegate_to: 127.0.0.1                             ## Execute this stage locally

----------------------------------

The question how to execute above tasks in parallel... Please help

Upvotes: 4

Views: 8946

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68024

Use async and set

        poll: 0

For example, in the playbook below, 2 commands sleep (10 seconds each) are used instead of the 2 commands from the question. The output shows that both tasks started the commands and terminated. The commands ran in parallel. The first task async_status waited for 3 iterations (3 seconds each) until the first command completed. The second task async_status completed immediately. The whole playbook ran for 11 seconds.

- hosts: localhost
  tasks:
    - debug:
        msg: "{{ lookup('pipe', 'date') }}"
    - command: /usr/bin/sleep 10  # Public TG
      async: 45
      poll: 0
      register: public
    - command: /usr/bin/sleep 10  # Private TG
      async: 45
      poll: 0
      register: private
    - debug:
        msg: "{{ lookup('pipe', 'date') }}"
    - async_status:
        jid: "{{ public.ansible_job_id }}"
      register: job_result
      until: job_result.finished
      retries: 100
      delay: 3
    - debug:
        msg: "{{ lookup('pipe', 'date') }}"
    - async_status:
        jid: "{{ private.ansible_job_id }}"
      register: job_result
      until: job_result.finished
      retries: 100
      delay: 3
    - debug:
        msg: "{{ lookup('pipe', 'date') }}"

gives (abridged)

TASK [debug] ****
  msg: Mon 12 Oct 2020 09:42:21 PM CEST

TASK [command] ****

TASK [command] ****

TASK [debug] ****
  msg: Mon 12 Oct 2020 09:42:21 PM CEST

TASK [async_status] ****
FAILED - RETRYING: async_status (100 retries left).
FAILED - RETRYING: async_status (99 retries left).
FAILED - RETRYING: async_status (98 retries left).

TASK [debug] ****
  msg: Mon 12 Oct 2020 09:42:32 PM CEST

TASK [async_status] ****

TASK [debug] ****
  msg: Mon 12 Oct 2020 09:42:32 PM CEST

It is possible to wait for more jobs in a single loop. For example

    - async_status:
        jid: "{{ item.ansible_job_id }}"
      loop:
        - "{{ public }}"
        - "{{ private }}"
      register: job_result
      until: job_result.finished
      retries: 100
      delay: 3

Upvotes: 5

Related Questions