# Can we run roles in parallel for multiple hosts?

**URL:** <https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776>\
**Category:** Ansible Project\
**Created:** [June 26, 2022, 8:46pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776 "2022-06-26T20:46:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shweta](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@Shweta](https://forum.ansible.com/u/Shweta)\
**Post date:** [June 26, 2022, 8:46pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/1 "2022-06-26T20:46:39Z")

</div>

New ansible user here.

After deploying VMs, I want to run a folder called configs on the Jump server to components like Syslog, NTP etc. for post configurations.

This is the structure of how files are in the config folder

configs/├─ hosts  
├─ group\_vars  
├─ playbook.yml  
├─ roles/  
├─ Ntp/  
├─ tasks/  
├─ vars/  
├─ Syslog/  
├─ vars/  
├─ tasks/

This is how it is my main playbook - playbook.yml

- name: Setup config for ntp  
hosts: comp1  
roles: - comp1

- name: Setup config for syslog  
hosts: comp2  
roles: - comp2

Right now I run this command

ansible-playbook -i hosts playbook.yml --limit “ntp,syslog”

this runs the tasks sequentially. How can I execute this in parallel considering there is no dependency between the components?

---

<div class="post-metadata">

**Author:** ![skratbag](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/skratbag/32/4836_2.png) [@skratbag](https://forum.ansible.com/u/skratbag)\
**Post date:** [June 26, 2022, 9:57pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/2 "2022-06-26T21:57:18Z")

</div>

Morning Shweta,

This is documented here [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_strategies.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_strategies.html)

Regards  
David

---

<div class="post-metadata">

**Author:** ![Shweta](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@Shweta](https://forum.ansible.com/u/Shweta)\
**Post date:** [June 27, 2022, 2:46am UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/3 "2022-06-27T02:46:13Z")

</div>

Hello David!

Thank you for the link. Would this strategy and fork option work for roles as well?  
In the user guide, there was no mention of roles, that’s why I asked.

---

<div class="post-metadata">

**Author:** ![skratbag](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/skratbag/32/4836_2.png) [@skratbag](https://forum.ansible.com/u/skratbag)\
**Post date:** [June 27, 2022, 3:31am UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/4 "2022-06-27T03:31:55Z")

</div>

I put the setting in my ansible.cfg. I tend to use small roles and import each role as needed in a playbook so I’m not sure how that would work with just roles. If you are trying to use RH Satellite then that has a different ansible.cfg than the standard (in /etc/foreman-proxy/ansible.cfg)

You can also put it in the playbook

hosts: all  
strategy: free

I put the forks in the command-line as --forks 50 or whatever. There is a small calculation that you can find on line (basically ncpus \* 4 = number of forks but also if you have a chunk of memory then you can increase it)

Regards  
David

---

<div class="post-metadata">

**Author:** ![skratbag](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/skratbag/32/4836_2.png) [@skratbag](https://forum.ansible.com/u/skratbag)\
**Post date:** [June 27, 2022, 4:19am UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/5 "2022-06-27T04:19:27Z")

</div>

I reread your original post, I think you will find each task/role within the playbook will be run sequentially. The strategy links that I gave you will help with getting them running on multiple hosts in roughly parallel. I’m unsure if you can parallelize the tasks.

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [June 27, 2022, 3:04pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/6 "2022-06-27T15:04:38Z")

</div>

wondering about the ‘why’ of your question I I guess it is about speeding up execution?

There is an extension [mitogen4ansible](https://aperogeek.fr/ansible-mitogen/) which claims to speed up playbook execution significantly. If I understand that correctly it is related to parallelizing things in some way on a python level. Here is some [youtube food for thought](https://www.youtube.com/results?search_query=mitogen+ansible) on this

---

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [June 27, 2022, 3:17pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/7 "2022-06-27T15:17:06Z")

</div>

Using mitogen is a no-brainer to sped up ansible runs. I was able to  
reduce my ansible running times to like a fifth of the time running with  
stock ansible with ZERO changes to my roles, tasks, playbooks and no  
additional software or configuration needed on the targets. It all  
happens on the controller and is enabled in a minute.

Highly recommended!

Greetings  
Marc

---

<div class="post-metadata">

**Author:** ![Michael\_Stroder](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Michael\_Stroder](https://forum.ansible.com/u/Michael_Stroder)\
**Post date:** [June 27, 2022, 3:56pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/8 "2022-06-27T15:56:01Z")

</div>

I can confirm the speed-up achieved by mitogen but there are two issues:

1. Some tasks won't work properly, e.g. if you have to set ansible\_python\_interpreter for a task.

2. IIRC it's not maintained anymore and it does not work with ansible 2.10+.

Ciao, Michael.

---

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [June 27, 2022, 7:59pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/9 "2022-06-27T19:59:54Z")

</div>

> 2. IIRC it's not maintained anymore and it does not work with ansible 2.10+.

This might be weirdness with Debian version numbers, but my ansible  
installed from debian is Version 5.5.0, and it works great with mitogen:

> [4/5063]mh@drop:~ $ dpkg --list '\*ansible\*' | grep -v '^un'  
> Desired=Unknown/Install/Remove/Purge/Hold  
> \> Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend  
> \>/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)  
> \>\>/ Name Version Architecture Description  
> +++-===============-============-============-===============================================================  
> ii ansible 5.5.0-1 all Configuration management, deployment, and task execution system  
> ii ansible-core 2.12.4-1 all Configuration management, deployment, and task execution system  
> ii ansible-mitogen 0.3.2-1 all Fast connection strategy for Ansible  
> [5/5064]mh@drop:~ $

Greetings  
Marc

---

<div class="post-metadata">

**Author:** ![Michael\_Stroder](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Michael\_Stroder](https://forum.ansible.com/u/Michael_Stroder)\
**Post date:** [June 27, 2022, 8:20pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/10 "2022-06-27T20:20:33Z")

</div>

Glad to see that there are mitogen updates. Thanks for the heads up.

Currently it seems to not work with ansible-core 2.13.x though. But hopefully there will be an update soon.

Ciao, Michael.

---

<div class="post-metadata">

**Author:** ![Shweta](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@Shweta](https://forum.ansible.com/u/Shweta)\
**Post date:** [June 28, 2022, 12:52pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/11 "2022-06-28T12:52:55Z")

</div>

Hello, I tried mitogen for the execution of some other ansible script and saw a significant difference in the time taken to complete the play. So thank you for this tool!

But is there no way to execute these roles in parallel?

As I described before as well -  
My main playbook.yml file calls role-wise to initiate setup and then inside roles I have tasks for different components. All hosts are different and execution is independent of each other.

---

<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:** [June 28, 2022, 1:32pm UTC](https://forum.ansible.com/t/can-we-run-roles-in-parallel-for-multiple-hosts/36776/12 "2022-06-28T13:32:14Z")

</div>

Yes and No, you can run playbooks in parallel, you cannot run roles in  
parallel in the same play nor playbook.

While all tasks run in parallel and you can do things like async or  
free strategy this wont parallelize the roles themselves, they get  
executed in the order in which they are address in the same play. So  
if you want to run roles in parallel i suggest executing parallel  
instances of ansible-playbook, parallel, xargs and other utilities  
make this simple, or via a scheduler cron/incron/awx|tower|aap,  
jenkins, etc). Theoretically you can write a strategy plugin that does  
this, but this is far from trivial.

A note about mitogen, since it was mentioned, it does not change how  
parallelization works, but it does accelerate execution in several  
ways, one is by turning pipelining always on, others have to do with  
optimizing execution for a threaded model and a 'semi persistent'  
agent on the target among other things. Before you ask, this cannot be  
integrated into the main ansible w/o loosing functionality and the  
threading model is not always an improvement over the forks, depends a  
lot on the size of the load and how it is structured, it gets very  
complicated at on point.
