# Gathering list of unique variable values and number of servers with each value

**URL:** <https://forum.ansible.com/t/gathering-list-of-unique-variable-values-and-number-of-servers-with-each-value/17780>\
**Category:** Ansible Project\
**Created:** [March 18, 2015, 10:48pm UTC](https://forum.ansible.com/t/gathering-list-of-unique-variable-values-and-number-of-servers-with-each-value/17780 "2015-03-18T22:48:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![js12](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@js12](https://forum.ansible.com/u/js12)\
**Post date:** [March 18, 2015, 10:48pm UTC](https://forum.ansible.com/t/gathering-list-of-unique-variable-values-and-number-of-servers-with-each-value/17780/1 "2015-03-18T22:48:02Z")

</div>

Hi,

I have an inventory file set up like this:

[loc1]  
server1  
server2

[loc1:vars]  
loc\_name=“location1”

[loc2]  
server3

[loc2:vars]  
loc\_name=“location2”

[loc3]  
server4  
server5  
server6

[loc3:vars]  
loc\_name=“location3”

I want to create a configuration file on all the servers containing all the different locations and the number of members in each location. Example:

location1: 2,  
location2: 1,  
location3: 3

The number of different loc\_names can vary. Is there a simple Jinja2 template that can yield those results? I’m worried that I’m approaching this from the wrong angle or that I set up the inventory in a way that is anti-Ansible; I want to avoid creating a convoluted template file if at all possible.

Any suggestions? Thanks!

---

<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:** [March 19, 2015, 12:40am UTC](https://forum.ansible.com/t/gathering-list-of-unique-variable-values-and-number-of-servers-with-each-value/17780/2 "2015-03-19T00:40:11Z")

</div>

you have all the locations in the groups dictionary and you can get  
the number of hosts by looking at the length of each group's host  
list.

{% for loc in groups %}  
{{loc +': ' + groups[loc]|length,}}  
...

---

<div class="post-metadata">

**Author:** ![js12](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@js12](https://forum.ansible.com/u/js12)\
**Post date:** [March 19, 2015, 1:06pm UTC](https://forum.ansible.com/t/gathering-list-of-unique-variable-values-and-number-of-servers-with-each-value/17780/3 "2015-03-19T13:06:07Z")

</div>

Unfortunately, I may have made the example too simple. The actual inventory file has more groups and not all the groups are just locations. Furthermore, the group names are not necessarily the same as the “loc\_name” variable. For example, in addition to the location groups, we could have groups like:

[master-server]  
server4

[loadbalancer]  
server6

Which should not show up in the breakdown.

Thanks.

---

<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:** [March 19, 2015, 1:32pm UTC](https://forum.ansible.com/t/gathering-list-of-unique-variable-values-and-number-of-servers-with-each-value/17780/4 "2015-03-19T13:32:19Z")

</div>

add an {% if loc.startswith('location') %}

---

<div class="post-metadata">

**Author:** ![js12](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@js12](https://forum.ansible.com/u/js12)\
**Post date:** [March 19, 2015, 3:17pm UTC](https://forum.ansible.com/t/gathering-list-of-unique-variable-values-and-number-of-servers-with-each-value/17780/5 "2015-03-19T15:17:39Z")

</div>

Alright, so it looks like the only way would be to key off a specifically formatted group name?

I was hoping to be able to rely on the variable alone. Ideally, an inventory could look like this (no groups):

server1 loc\_name=“location1”  
server2 loc\_name=“location1”  
server3 loc\_name=“location2”  
server4 loc\_name=“location3”  
server5 loc\_name=“location3”  
server6 loc\_name=“location3”

But that might be getting away from proper Ansible design.

I appreciate the input you’ve given, thanks!

---

<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:** [March 19, 2015, 3:22pm UTC](https://forum.ansible.com/t/gathering-list-of-unique-variable-values-and-number-of-servers-with-each-value/17780/6 "2015-03-19T15:22:13Z")

</div>

you could just create a variable with the list of 'location groups'  
and iterate over that.
