running parallel different task Ansible 2.2.0

871 Views Asked by At

I want to know if it's possible deploy 6 different tasks in parallel (3|3) for 2 different hosts in ansible 2.2.0, because i'm having issues run them in parallel with one playbook, they just run sequencial.

2

There are 2 best solutions below

1
Konstantin Suvorov On

You can do this with async tasks. Something like this:

- hosts: host1,host2
  tasks:
    - command: /task1a.sh
      async: 1000
      poll: 0
      register: task1a
      when: inventory_hostname == 'host1'
    - command: /task1b.sh
      async: 1000
      poll: 0
      register: task1b
      when: inventory_hostname == 'host2'
    # ... repeat for tasks 2 and 3
    - command: /check_done.sh
      register: check_done
      until: check_done | success
      retries: 30

This will start task1a.sh and task1b.sh as async jobs on host1 and host2 respectively and then wait for check_done.sh to return zero.

0
Strahinja Kustudic On

You can see strategy: free in a combination with when: inventory_hostname == 'hostXX', for example:

- hosts: all
  strategy: free
  tasks:
    - block:
        - name: task1
          ...
        - name: task2
          ...
        - name: task3
          ...
      when: inventory_hostname == 'host1'

    - block:
        - name: task4
          ...
        - name: task5
          ...
        - name: task6
          ...
      when: inventory_hostname == 'host2'

strategy: free means all tasks will run on all hosts in parallel as fast as they can, not waiting for each task to finish on each host. Which task is going to be run on which host is decided by the when clause.

I used block here, so that you only need to write one when for each block, but you don't have to use it, you can write a when on each task.