Reputation: 13
I have a situation where I want to loop over a list (with_items) and execute a command. However, the next item cannot start unless the first one has completed on all hosts. The amount of items that I will loop over will be huge. However there should not be any drift in the hosts as to the item that is being used. No host should start item2 unless item 1 has finished in the entire pool of hosts.
To create a proof of concept I hope to achieve the following
- name: proof of concept
command: bash -c /home/user/my_test_script.sh
with_items:
- item_1
- item_2
....
- item_n
Where my_test_script.sh has a different sleep(x) on each host simulating that some may be slower in executing a job.
I have already looked at, wait_for, wait_until, etc.. but have not yet found a solution to execute a parallel task (with_items), that runs (for each item in the list) 'in sync' (time wise not rsync) for a number of given items.
Any help would be appreciated.
Upvotes: 0
Views: 2586
Reputation: 1579
Related: Ansible: How to iterate over an role with an array?.
If you're using Ansible 2.3.0 or higher, check out include_role. You can write a role that will call your script with a variable parameter. Then your playbook can loop over the role.
role:
- name: proof of concept
command: bash -c /home/user/my_test_script.sh {{ script_param }}
playbook:
- include_role:
name: test_script
with_items:
- item1
- item2
- item3
vars:
script_param: "{{ item }}"
Upvotes: 0