# filter on attribute value

**URL:** <https://forum.ansible.com/t/filter-on-attribute-value/33016>\
**Category:** Ansible Project\
**Created:** [May 24, 2020, 5:58pm UTC](https://forum.ansible.com/t/filter-on-attribute-value/33016 "2020-05-24T17:58:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anuj\_Agrawal](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anuj\_Agrawal](https://forum.ansible.com/u/Anuj_Agrawal)\
**Post date:** [May 24, 2020, 5:58pm UTC](https://forum.ansible.com/t/filter-on-attribute-value/33016/1 "2020-05-24T17:58:06Z")

</div>

I want to validate my inventory to have only a certain number of hosts  
to have a certain value. I tried to do that using assert but I am  
unable to find a way to do so.

Example inventory (ini) file:

[nginx]  
10.52.130.31

[backends]  
10.52.194.41 read\_only="true"  
10.52.130.2 read\_only="false"  
10.52.130.31 read\_only="true"

Want to validate there is only one host in backends with read\_only as  
false. Tried a lot of things but could not get it to work.

Any help?

Thank you  
Anuj Agrawal

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 24, 2020, 6:48pm UTC](https://forum.ansible.com/t/filter-on-attribute-value/33016/2 "2020-05-24T18:48:09Z")

</div>

You could default that variable to true in a single location, so it’s easy to spot in the inventory?

---

<div class="post-metadata">

**Author:** ![Anuj\_Agrawal](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Anuj\_Agrawal](https://forum.ansible.com/u/Anuj_Agrawal)\
**Post date:** [May 25, 2020, 3:12am UTC](https://forum.ansible.com/t/filter-on-attribute-value/33016/3 "2020-05-25T03:12:40Z")

</div>

True, but I was hoping I could validate that the variable is not set  
to true for more than one host.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 14, 2020, 6:18pm UTC](https://forum.ansible.com/t/filter-on-attribute-value/33016/4 "2020-06-14T18:18:26Z")

</div>

Something like this should work

&nbsp;&nbsp;- assert:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that: groups['backends'] | map('extract', hostvars, 'read\_only') | select('equalto', 'false') | list | length == 1
