# Help with async tasks.

**URL:** <https://forum.ansible.com/t/help-with-async-tasks/33704>\
**Category:** Ansible Project\
**Created:** [August 31, 2020, 9:38am UTC](https://forum.ansible.com/t/help-with-async-tasks/33704 "2020-08-31T09:38:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guannan\_Sun](https://avatars.discourse-cdn.com/v4/letter/g/df705f/32.png) [@Guannan\_Sun](https://forum.ansible.com/u/Guannan_Sun)\
**Post date:** [August 31, 2020, 9:38am UTC](https://forum.ansible.com/t/help-with-async-tasks/33704/1 "2020-08-31T09:38:38Z")

</div>

Dear Tech Team,

I’m Guannan from China. I’m new to Ansible and looking for some technical help.  
Actually I’m looking for a method to execute one role in parallel with loops of different configuration files. For example, I want to create 100 sites using role “create\_site” with 100 different configuration files. I simply write a playbook like this:

- hosts: localhost  
gather\_facts: no  
tasks:

- name: register files  
shell: “find /tmp/site\_files -name ‘\*.yml’ | sort”  
register: file\_names

- include\_role:  
name: create\_site  
vars\_from: “{{ item }}”  
with\_items: “{{ file\_names.stdout\_lines }}”

This will create 100 sites in serial. If create one site take 2 minutes, then the whole playbook will take over 200 minutes. I found ‘async’ option when execute tasks, however it seems only used for async jobs on different hosts. Do you guys have experience or methods on how to execute one role with different configuration files in parallel? Please send me E-mails.  
Much thanks for your help!

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [August 31, 2020, 9:45am UTC](https://forum.ansible.com/t/help-with-async-tasks/33704/2 "2020-08-31T09:45:15Z")

</div>

> Dear Tech Team,
> 
> I'm Guannan from China. I'm new to Ansible and looking for some technical help.  
> Actually I'm looking for a method to execute one role in parallel with loops of different configuration files. For  
> example, I want to create 100 sites using role "create\_site" with 100 different configuration files. I simply write a  
> playbook like this:
> 
> - hosts: localhost  
> gather\_facts: no  
> tasks:  
> - name: register files  
> shell: "find /tmp/site\_files -name '\*.yml' | sort"  
> register: file\_names
> 
> ```
> \- include\_role:
> name: create\_site
> vars\_from: &quot;\{\{ item \}\}&quot;
> with\_items: &quot;\{\{ file\_names\.stdout\_lines \}\}&quot;
> 
> ```
> 
> This will create 100 sites in serial. If create one site take 2 minutes, then the whole playbook will take over 200  
> minutes. I found 'async' option when execute tasks, however it seems only used for async jobs on different hosts. Do you  
> guys have experience or methods on how to execute one role with different configuration files in parallel? Please send  
> me E-mails.  
> Much thanks for your help!

What is the "create\_site" role actually doing?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Guannan\_Sun](https://avatars.discourse-cdn.com/v4/letter/g/df705f/32.png) [@Guannan\_Sun](https://forum.ansible.com/u/Guannan_Sun)\
**Post date:** [August 31, 2020, 9:57am UTC](https://forum.ansible.com/t/help-with-async-tasks/33704/3 "2020-08-31T09:57:00Z")

</div>

Thank you for your response.  
Well, in my condition, I using ‘create\_site’ role to create a site in a netbox. It will create a site with name, region, and some other fields, associate some devices and vlans with the site.  
You may just treat ‘create\_site’ as a role execute some tasks in serial.

在2020年8月31日星期一 UTC+8 下午5:45:56[brae...@gmail.com](mailto:brae...@gmail.com) 写道：

---

<div class="post-metadata">

**Author:** ![Martin\_Etcheverry](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@Martin\_Etcheverry](https://forum.ansible.com/u/Martin_Etcheverry)\
**Post date:** [August 31, 2020, 8:09pm UTC](https://forum.ansible.com/t/help-with-async-tasks/33704/4 "2020-08-31T20:09:44Z")

</div>

Hi ! maybe something like this :  
[https://medium.com/developer-space/parallel-playbook-execution-in-ansible-30799ccda4e0](https://medium.com/developer-space/parallel-playbook-execution-in-ansible-30799ccda4e0)

---

<div class="post-metadata">

**Author:** ![Guannan\_Sun](https://avatars.discourse-cdn.com/v4/letter/g/df705f/32.png) [@Guannan\_Sun](https://forum.ansible.com/u/Guannan_Sun)\
**Post date:** [September 1, 2020, 7:19am UTC](https://forum.ansible.com/t/help-with-async-tasks/33704/5 "2020-09-01T07:19:35Z")

</div>

Thank you for your response.  
I’ve tried to add ‘strategy: free’ in my playbook, however it seems not work as my expectation. I think it may because there is only single host in my scenario, while ‘strategy: free’ works well in scenario of multi hosts and multi tasks.  
We’re now trying to run multi jobs at the same time on AWX. One jobs call the role ‘create\_site’ and loop for 5 configuration files, so that 20 jobs will cover 100 configuration files. It seems work in my scenario.  
Thank you anyway, and good-looking signiture 🙂

在2020年9月1日星期二 UTC+8 上午4:10:14[mar...@etcheverri.com](mailto:mar...@etcheverri.com) 写道：

---

<div class="post-metadata">

**Author:** ![Martin\_Etcheverry](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@Martin\_Etcheverry](https://forum.ansible.com/u/Martin_Etcheverry)\
**Post date:** [September 2, 2020, 12:07am UTC](https://forum.ansible.com/t/help-with-async-tasks/33704/6 "2020-09-02T00:07:24Z")

</div>

well other option (i used only once) is run the shell command with gnu parallel .  
its pretty amazing! but i pretty stiff learning curve.  
maybe you could , give it a chance.

best regards!

---

<div class="post-metadata">

**Author:** ![Guannan\_Sun](https://avatars.discourse-cdn.com/v4/letter/g/df705f/32.png) [@Guannan\_Sun](https://forum.ansible.com/u/Guannan_Sun)\
**Post date:** [September 3, 2020, 1:31am UTC](https://forum.ansible.com/t/help-with-async-tasks/33704/7 "2020-09-03T01:31:39Z")

</div>

Thank you!  
Even I have not used gnu parallel before, I’ll also take it into consideration.  
If have any progress, I’d like to share.  
Best reagards!

在2020年9月2日星期三 UTC+8 上午8:07:53[mar...@etcheverri.com](mailto:mar...@etcheverri.com) 写道：
