# Looping through a play with a list such that n number of loops are always active

**URL:** <https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085>\
**Category:** Get Help\
**Tags:** ansible-core\
**Created:** [May 30, 2024, 3:03pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085 "2024-05-30T15:03:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lurk-er](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lurk-er/32/3613_2.png) [@lurk-er](https://forum.ansible.com/u/lurk-er)\
**Post date:** [May 30, 2024, 3:03pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085/1 "2024-05-30T15:03:26Z")

</div>

Hi, I have a question about using loops for a single play running on localhost.

I have a list of things that is being looped over in a play. This list can be run in the play all at once but that will be too much for the local host system so what i want to do is loop through the list and the play n number of times such that n number of instances of that play are running untill the list is exhausted.

The example below should run item\_1,item\_2, item\_3 at the same time and start item\_4 when either of those completes untill the end of the list is reached.

For example:

```auto
the program: this_is_a_long_script.sh

- name: run program
  ansible.builtin.command:
    cmd: "/bin/bash -c this_is_a_long_script.sh --list {{ item }}"
  loop:
    - item_1....
    - item_4....
    - item_7....
    - item_13....
    - item_17....
    - ....and so on

```

---

<div class="post-metadata">

**Author:** ![motorbass](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@motorbass](https://forum.ansible.com/u/motorbass)\
**Post date:** [May 30, 2024, 4:03pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085/2 "2024-05-30T16:03:15Z")

</div>

Hi  
If parallels tasks are too much for your localhost, why don’t you simply run it one by one within your loop ?  
Or maybe you just need to split parallels execution 3 by 3 ? i’m not sure to 100% get your need 🙂

---

<div class="post-metadata">

**Author:** ![lurk-er](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lurk-er/32/3613_2.png) [@lurk-er](https://forum.ansible.com/u/lurk-er)\
**Post date:** [May 30, 2024, 4:18pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085/3 "2024-05-30T16:18:26Z")

</div>

the goal is to run the script n number of times (once with each item) on the localhost. running one by one in the loop is the normal way and is slow if the called application is slow but the system has power to run the script multiple times. I’m looking to get around the bottle neck of looping to each item one at a time and instead loop through each item and start 3 of them at the same time replacing them in a cyclic fashion until the items list is empty

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 30, 2024, 4:18pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085/4 "2024-05-30T16:18:55Z")

</div>

loops are not executed in parallel, only tasks/hosts, if you are targeting a single host, each loop item will run in sequence on it.

---

<div class="post-metadata">

**Author:** ![lurk-er](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lurk-er/32/3613_2.png) [@lurk-er](https://forum.ansible.com/u/lurk-er)\
**Post date:** [May 30, 2024, 4:19pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085/5 "2024-05-30T16:19:26Z")

</div>

is it possible to execute a loop in parallel?

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 30, 2024, 4:20pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085/6 "2024-05-30T16:20:21Z")

</div>

no, parallelization is on the host/task level. You CAN basically parallelize actions on a single host by defining multiple aliases to it.

---

<div class="post-metadata">

**Author:** ![lurk-er](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lurk-er/32/3613_2.png) [@lurk-er](https://forum.ansible.com/u/lurk-er)\
**Post date:** [May 30, 2024, 4:22pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085/7 "2024-05-30T16:22:39Z")

</div>

Thanks, solved. could you give an example?

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 30, 2024, 4:30pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085/8 "2024-05-30T16:30:59Z")

</div>

it is as simple as just targeting the host and using the loop:

```auto
- hosts: localhost
  tasks:
  - command: “/bin/bash -c this_is_a_long_script.sh --list {{ item }}”
     loop: '{{big_list_of_items}}'

```

---

<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 29, 2024, 4:31pm UTC](https://forum.ansible.com/t/looping-through-a-play-with-a-list-such-that-n-number-of-loops-are-always-active/6085/9 "2024-06-29T16:31:16Z")

</div>

Closed on June 29, 2024, 4:31pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
