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

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:

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

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 :slight_smile:

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

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.

1 Like

is it possible to execute a loop in parallel?

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

1 Like

Thanks, solved. could you give an example?

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

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

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