# How to Random Roles

**URL:** <https://forum.ansible.com/t/how-to-random-roles/29358>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [January 3, 2019, 1:09pm UTC](https://forum.ansible.com/t/how-to-random-roles/29358 "2019-01-03T13:09:39Z")\
**Posts on this page:** 4\
**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 3, 2019, 1:09pm UTC](https://forum.ansible.com/t/how-to-random-roles/29358/1 "2019-01-03T13:09:39Z")

</div>

I gotta random both the hosts and the roles.  
I successfully randomed the hosts, but I not getting the roles to random.  
Here is what I got

---

<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 3, 2019, 1:20pm UTC](https://forum.ansible.com/t/how-to-random-roles/29358/2 "2019-01-03T13:20:56Z")

</div>

Are you trying to randomly select a role, randomly select a host, then run the selected role on the selected host?

---

<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 3, 2019, 3:21pm UTC](https://forum.ansible.com/t/how-to-random-roles/29358/3 "2019-01-03T15:21:48Z")

</div>

No, the picture is that the role is not on the hosts.

First I random a host (that script does that).

Once a host is selected then the role is randomized.

---

<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 3, 2019, 5:41pm UTC](https://forum.ansible.com/t/how-to-random-roles/29358/4 "2019-01-03T17:41:53Z")

</div>

I figured out.

I came with this:

**- hosts: “{{ (groups[‘foo’] | shuffle)[0:1] }}”** #THIS SCRIPT RANDOMS THE HOST  
become: true  
vars:

- ansible\_sudo\_pass: \*\*\*\*\*\*\*  
roles:  
**- role: “{{ [‘reboot’,‘shutdown’] | random }}”** #THIS SCRIPT RANDOMS THE ROLE

So I guess we can consider this issue solved.

Thanks anyways, guys
