# How to randomize hosts but not repeat the ones that already were selected

**URL:** <https://forum.ansible.com/t/how-to-randomize-hosts-but-not-repeat-the-ones-that-already-were-selected/29403>\
**Category:** Ansible Project\
**Created:** [January 9, 2019, 1:49pm UTC](https://forum.ansible.com/t/how-to-randomize-hosts-but-not-repeat-the-ones-that-already-were-selected/29403 "2019-01-09T13:49:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francis\_Santos](https://avatars.discourse-cdn.com/v4/letter/f/49beb7/32.png) [@Francis\_Santos](https://forum.ansible.com/u/Francis_Santos)\
**Post date:** [January 9, 2019, 1:49pm UTC](https://forum.ansible.com/t/how-to-randomize-hosts-but-not-repeat-the-ones-that-already-were-selected/29403/1 "2019-01-09T13:49:56Z")

</div>

Hi all,

With this script I can randomize any host:

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [January 9, 2019, 1:58pm UTC](https://forum.ansible.com/t/how-to-randomize-hosts-but-not-repeat-the-ones-that-already-were-selected/29403/2 "2019-01-09T13:58:52Z")

</div>

Do you mean that you want to work through all the hosts in a random order?

If so, just shuffle the list, then work through the list in shuffled order.

Regards, K.

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [January 9, 2019, 2:45pm UTC](https://forum.ansible.com/t/how-to-randomize-hosts-but-not-repeat-the-ones-that-already-were-selected/29403/3 "2019-01-09T14:45:05Z")

</div>

Are you trying to run the playbook on a list of randomized hosts except the first and 2nd host?

Maybe you can try

- hosts: “{{ (groups[‘all’] | shuffle)[2:] }}”

See the following for more details  
[https://docs.ansible.com/ansible/latest/user\_guide/intro\_patterns.html](https://docs.ansible.com/ansible/latest/user_guide/intro_patterns.html)

---

<div class="post-metadata">

**Author:** ![Francis\_Santos](https://avatars.discourse-cdn.com/v4/letter/f/49beb7/32.png) [@Francis\_Santos](https://forum.ansible.com/u/Francis_Santos)\
**Post date:** [January 9, 2019, 3:20pm UTC](https://forum.ansible.com/t/how-to-randomize-hosts-but-not-repeat-the-ones-that-already-were-selected/29403/4 "2019-01-09T15:20:42Z")

</div>

Nop, the idea is to randomize all hosts.  
For example,

POOL: host1 host2 host3

I randomize, host2 is chosen

I randomize again, but now host2 cannot be selected.

POOL: host1 host3  
I randomize, host1 is chosen

POOL: host3

I randomize, host3 is chosen

Once all hosts are chosen I can start the pool again.

POOL: host1 host2 host3

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [January 9, 2019, 9:27pm UTC](https://forum.ansible.com/t/how-to-randomize-hosts-but-not-repeat-the-ones-that-already-were-selected/29403/5 "2019-01-09T21:27:39Z")

</div>

Yes - so shuffle the pool, then use the first host from the pool for the first task, the second host from the pool for the second task, the third host from the pool for the third task and so on.

You can either specify the offset in the pool for each successive task, or slice the pool between tasks.

Regards, K.
