# Random return host in groups and balanced accross the cluster

**URL:** <https://forum.ansible.com/t/random-return-host-in-groups-and-balanced-accross-the-cluster/20609>\
**Category:** Ansible Project\
**Created:** [February 18, 2016, 12:13am UTC](https://forum.ansible.com/t/random-return-host-in-groups-and-balanced-accross-the-cluster/20609 "2016-02-18T00:13:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [February 18, 2016, 12:13am UTC](https://forum.ansible.com/t/random-return-host-in-groups-and-balanced-accross-the-cluster/20609/1 "2016-02-18T00:13:43Z")

</div>

Hi,

I have an inventory file with the bellow servers :

# servers

`  
[dbservers]  
db01  
db02  
db03  
db04

`

In my template I want to populate the config by choosing randomly one of the servers from the dbservers group .

Unfortunately the bellow will return all servers :

`  
{% for host in groups[‘dbservers’] %}  
server\_address = {{ hostvars[host][‘inventory\_hostname’] }}  
{% endfor %}

`

1. I need Ansible to return only 1 server (random) .

2. I will run this on a big cluster of servers so I would like that random is actually balanced across the cluster equally or else a db servers will get more connections than the others and could hit a capacity issue.

Can someone help me ?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Paul\_Markham](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Paul\_Markham](https://forum.ansible.com/u/Paul_Markham)\
**Post date:** [February 18, 2016, 2:07am UTC](https://forum.ansible.com/t/random-return-host-in-groups-and-balanced-accross-the-cluster/20609/2 "2016-02-18T02:07:59Z")

</div>

There’s a random number filter; see [http://docs.ansible.com/ansible/playbooks\_filters.html#random-number-filter](http://docs.ansible.com/ansible/playbooks_filters.html#random-number-filter).

Try something like:

`server_address = {{ groups['dbservers'] | random }}`

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [February 18, 2016, 7:52pm UTC](https://forum.ansible.com/t/random-return-host-in-groups-and-balanced-accross-the-cluster/20609/3 "2016-02-18T19:52:26Z")

</div>

Thank you Paul , that seems to work . However I’m not completely satisfied with how balanced the random filter has worked.

Do you know if there is a better way to have the server\_address more equally balanced across a big bluster of servers ?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 21, 2016, 12:15am UTC](https://forum.ansible.com/t/random-return-host-in-groups-and-balanced-accross-the-cluster/20609/4 "2016-02-21T00:15:18Z")

</div>

you could probably do a mod on the length of the group and assign depending on a sequence.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [February 23, 2016, 12:50am UTC](https://forum.ansible.com/t/random-return-host-in-groups-and-balanced-accross-the-cluster/20609/5 "2016-02-23T00:50:56Z")

</div>

Brian Coca can you be more specific ? I’m trying something like the bellow but I"m stuck, it’s deploying all the servers in every run instead of only a random one …

`  
{% for host in groups[‘dbservers’] %}  
{% if loop.index % (loop.length + 1) %}  
server\_address = {{ host }}  
{{ loop.index % loop.length + 1 }}  
{% endif %}  
{% endfor %}

`
