# Find number of servers where something is true

**URL:** <https://forum.ansible.com/t/find-number-of-servers-where-something-is-true/24755>\
**Category:** Ansible Project\
**Created:** [June 18, 2017, 11:39pm UTC](https://forum.ansible.com/t/find-number-of-servers-where-something-is-true/24755 "2017-06-18T23:39:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tommy\_Ash](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@Tommy\_Ash](https://forum.ansible.com/u/Tommy_Ash)\
**Post date:** [June 18, 2017, 11:39pm UTC](https://forum.ansible.com/t/find-number-of-servers-where-something-is-true/24755/1 "2017-06-18T23:39:32Z")

</div>

Hi, I’m trying to get a count of how many servers have a specific file on them. For example, if /tmp/testFile is on 8 of 25 hosts that I’m running a playbook against, I’d like to register a variable and give it the value of 8. Is this possible?

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [June 19, 2017, 4:13pm UTC](https://forum.ansible.com/t/find-number-of-servers-where-something-is-true/24755/2 "2017-06-19T16:13:00Z")

</div>

[http://docs.ansible.com/ansible/group\_by\_module.html](http://docs.ansible.com/ansible/group_by_module.html)

will let you make a group by fact, you could then just have a task  
on localhost output '{{ groups["has\_that\_file"] \_ length }}".

There's a set\_fact task you can use to set a (boolean) fact on hosts,  
or just bite the bullet and write a custom fact.

It seems a bit of an odd requirement to be honest, that's the best I can  
suggest from what you've posted.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [June 19, 2017, 4:13pm UTC](https://forum.ansible.com/t/find-number-of-servers-where-something-is-true/24755/3 "2017-06-19T16:13:47Z")

</div>

sorry that should be:

{{ groups["has\_that\_file"] | length }}

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 19, 2017, 4:27pm UTC](https://forum.ansible.com/t/find-number-of-servers-where-something-is-true/24755/4 "2017-06-19T16:27:57Z")

</div>

This can also be accomplished using the `stat` module, and then a little jinja2 pipeline:

- hosts: all  
tasks:

- stat:  
path: /tmp  
register: stat\_result

- debug:  
msg: “{{ hostvars|dictsort|selectattr(‘1.stat\_result.stat.exists’)|list|length }}”  
run\_once: true

This would output something like the following for me:

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [[example.org](http://example.org)] =\> {  
“msg”: “8”  
}
