# Parallel execution of tasks in playbook

**URL:** <https://forum.ansible.com/t/parallel-execution-of-tasks-in-playbook/29026>\
**Category:** Ansible Project\
**Created:** [November 14, 2018, 12:27pm UTC](https://forum.ansible.com/t/parallel-execution-of-tasks-in-playbook/29026 "2018-11-14T12:27:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![e9893ac46c6125c219ad](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@e9893ac46c6125c219ad](https://forum.ansible.com/u/e9893ac46c6125c219ad)\
**Post date:** [November 14, 2018, 12:27pm UTC](https://forum.ansible.com/t/parallel-execution-of-tasks-in-playbook/29026/1 "2018-11-14T12:27:03Z")

</div>

Hi Experts,  
Can you please share the tips to improve the execution time of Ansible playbook by executing the tasks in parallel? Currently all tasks are executed in sequential manner and waiting for prior task to complete the execution. Is there anyway each ask can execute in parallel?

-Build admin

---

<div class="post-metadata">

**Author:** ![Lucas\_Jackson](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@Lucas\_Jackson](https://forum.ansible.com/u/Lucas_Jackson)\
**Post date:** [November 14, 2018, 12:47pm UTC](https://forum.ansible.com/t/parallel-execution-of-tasks-in-playbook/29026/2 "2018-11-14T12:47:05Z")

</div>

See this, async tasks, may be what you’re looking for

[https://docs.ansible.com/ansible/2.5/user\_guide/playbooks\_async.html](https://docs.ansible.com/ansible/2.5/user_guide/playbooks_async.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 14, 2018, 2:06pm UTC](https://forum.ansible.com/t/parallel-execution-of-tasks-in-playbook/29026/3 "2018-11-14T14:06:52Z")

</div>

I would look at the 'free' strategy, but you have others:

[https://docs.ansible.com/ansible/latest/plugins/strategy.html#plugin-list](https://docs.ansible.com/ansible/latest/plugins/strategy.html#plugin-list)

---

<div class="post-metadata">

**Author:** ![e9893ac46c6125c219ad](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@e9893ac46c6125c219ad](https://forum.ansible.com/u/e9893ac46c6125c219ad)\
**Post date:** [November 15, 2018, 3:40pm UTC](https://forum.ansible.com/t/parallel-execution-of-tasks-in-playbook/29026/4 "2018-11-15T15:40:33Z")

</div>

Thank you for your reply.  
I tried using strategy as free and unable to use async in the task as task has dependency task. Could you please suggest code change or alternate method for running the task in parallel.

`

```auto
---
- name: Main playbook
  gather_facts: no
  hosts: 127.0.0.1
  strategy: free

  tasks:
    - name: Create csv file and html file
      file:
        path: "{{ item }}"
        state: touch
      delegae_to: localhost
      become_user: awx
      become: no
      with_items:
        - /tmp/apache.csv
        - /tmp/apache.html

    - include_vars: apache_sever_list.yaml

    - include_tasks: apache_task.yaml
      with_items: '{{ apacheSevers }}'

    - name: Run the csv2html script
      shell: |
        echo "<h3>List of failed Apache servers</h3>"
        echo "<table>" ;
        echo "<table><tr><th>Hostname</th><th>Port</th></tr>"
        while read INPUT; do
        echo "<tr><td>${INPUT//,/</td><td>}</td></tr>";
        done < /tmp/apache.csv
        echo "</table>"
      delegae_to: localhost
      become_user: awx
      become: no        

    - name: append
      lineinfile:
        dest: /tmp/apache.html
        line: "{{ output.stdout }}"
        insertafter: EOF
      delegae_to: localhost
      become_user: awx
      become: no

```

`

**apche\_task.yaml**

`

```auto
    - name: Check the apache server status
      uri:
        url: "{{ item.hostname }}:{{ item.port }}"
        method: GET
        status_code: 200
        body_format: raw
        follow_redirects: all
        return_content: yes
        validate_certs: no
        force: yes
      delegae_to: localhost
      become_user: awx
      become: no

    - name: append output to file
      lineinfile:
        dest: /tmp/apache.csv
        line: "{{ item.hostname }},{{ item.port }}"
        insertafter: EOF
      delegae_to: localhost
      become_user: awx
      become: no 

```

`

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [November 16, 2018, 8:35am UTC](https://forum.ansible.com/t/parallel-execution-of-tasks-in-playbook/29026/5 "2018-11-16T08:35:25Z")

</div>

Hello,

Ansible will run tasks in parallel against groups of hosts, so I suggest you convert your`

```auto
apache_sever_list.yaml

```

`  
file into ansible inventory format

and put all the hosts in it into a group called ‘apache’

then you can run tasks against

- hosts: apache  
tasks:
- name: any tasks here run against all the hosts in apache group simultaneously

What its nice is you can still use ‘delegate\_to: localhost’ for your uri task and it will still run the task from the ansible controller (but run it in parallel for each host in your ‘apache’ group).

Also, have a look at ‘template’ module I think you will find it a lot easier to create the html file than the combination of shell commands and lineinfile.

I don’t have time to test this today but try experimenting with organising things as follows:

Create an inventory file for your apache hosts:

`

# file: apache\_hosts

# this is an ansible inventory file (using ‘ini’ format but can use yaml)

# see [https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html) for more about inventory

[apache]  
host1 ap\_port=8081  
host2 ap\_port=80  
host3 ap\_port=8080  
host4 ap\_port=8008  
host5 ap\_port=8123  
`

Create a template file like this:

`

# ansible template file: apache.html.j2

# see [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

# for how to use variables in templates.

### List of failed Apache servers

\<%for host\_result in apache\_check\_result %\> {{ again use apache\_check\_result to get at the port used | default('Failed' }}

| Hostname | Port |
| --- | --- |
| {{ examine apache\_check\_result to find hostname from results }} |

`

Create a playbook like this:

`

# playbook: apache\_check.yml

- name: report on apache status  
gather\_facts: yes  
hosts: apache

tasks:

- name: Check the apache server status  
uri:  
url: “{{ ansible\_hostname }}:{{ ap\_port }}”  
method: GET  
status\_code: 200  
body\_format: raw  
follow\_redirects: all  
return\_content: yes  
validate\_certs: no  
force: yes  
register: apache\_check\_result  
ignore\_errors: yes  
delegate\_to: localhost

- name: show results for debugging purposes  
debug:  
var: apache\_check\_result

- name: template out the results  
template:  
src: apache\_html.j2  
dest: /var/apache.html  
delegate\_to: localhost

`

and run the playbook like this

`  
ansible-playbook -i apache\_hosts apache\_check.yml

`

Sorry I haven’t got time to debug this but I hope the above will illustrate that by using ansible’s inventory you can get your tasks to run in parallel (and also that using the ‘template’ module is a great way to create files from ansible.

Hope this helps get you on the right track.

All the best,

Jon

---

<div class="post-metadata">

**Author:** ![Saravanan](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@Saravanan](https://forum.ansible.com/u/Saravanan)\
**Post date:** [November 18, 2018, 11:58pm UTC](https://forum.ansible.com/t/parallel-execution-of-tasks-in-playbook/29026/6 "2018-11-18T23:58:21Z")

</div>

Thanks a bunch Jon. It helps a lot.
