# finding first host meeting a condition

**URL:** <https://forum.ansible.com/t/finding-first-host-meeting-a-condition/29370>\
**Category:** Ansible Project\
**Created:** [January 4, 2019, 3:02pm UTC](https://forum.ansible.com/t/finding-first-host-meeting-a-condition/29370 "2019-01-04T15:02:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [January 4, 2019, 3:02pm UTC](https://forum.ansible.com/t/finding-first-host-meeting-a-condition/29370/1 "2019-01-04T15:02:32Z")

</div>

Hey all-

I need to find the first host in the inventory running a particular process. I can query the hosts to look for the process using a shell pipeline and register the output in a variable (call it ps\_output). But I’m stuck on how to iterate over the hosts, examining the value of ps\_output, and stopping when I have found the first one. I don’t see a built in filter for this (maybe I need to write one?), I’ve struck out with templating, and I currently have it working with a complicated mess of storing the hostvars in a file and processing it with an external python script, but I have to believe there’s an easier way. Any suggestions?

Rob

---

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [January 4, 2019, 8:17pm UTC](https://forum.ansible.com/t/finding-first-host-meeting-a-condition/29370/2 "2019-01-04T20:17:40Z")

</div>

Nevermind; figured this out. Was actually pretty simple. I declared a variable as an empty string, iterated over the hosts w/ hostvars, updated the variable with the item, and used when: (variable | length \> 0 ) and (ps\_output | length \> 0). Including the variable in the when: test was key.

Rob
