# problem to fetch value from var files

**URL:** <https://forum.ansible.com/t/problem-to-fetch-value-from-var-files/30667>\
**Category:** Ansible Project\
**Created:** [June 27, 2019, 9:17am UTC](https://forum.ansible.com/t/problem-to-fetch-value-from-var-files/30667 "2019-06-27T09:17:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Chang](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@Eric\_Chang](https://forum.ansible.com/u/Eric_Chang)\
**Post date:** [June 27, 2019, 9:17am UTC](https://forum.ansible.com/t/problem-to-fetch-value-from-var-files/30667/1 "2019-06-27T09:17:07Z")

</div>

I have a yml file named: client\_hosts.yml  
it looks like below

> client\_hosts:  
> [abc.com](http://abc.com):
> 
> - host: dc021  
> ipv4: 192.168.11.21
> - host: dc022  
> ipv4: 192.168.11.22
> - host: dc023  
> ipv4: 192.168.11.23  
> iuser: True  
> user: john
> - host: dc024  
> ipv4: 192.168.11.24
> - host: dc025  
> ipv4: 192.168.11.25  
> iuser: True  
> user: Mary
> - host: dc026  
> ipv4: 192.168.11.26
> - host: dc027  
> ipv4: 192.168.11.27
> - host: dc028  
> ipv4: 192.168.11.28
> - host: dc029  
> ipv4: 192.168.11.29

I want to select user with iuser=True  
so I create a playbook , include the file

with the code below , I can get a list.

- name: load client\_host  
include\_vars:  
file: client\_hosts.yml  
name: ch

- name: get user from yml  
debug:  
msg: “{{ ch[‘client\_hosts’][‘[abc.com](http://abc.com)’]}}”

> ok: [[dc053.abc.com](http://dc053.abc.com)] =\> {  
> “msg”: [  
> {  
> “host”: “dc026”,  
> “ipv4”: “192.168.11.26”,  
> “iuser”: “True”,  
> “user”: [  
> “joy”  
> ]  
> },  
> {  
> “host”: “dc027”,  
> “ipv4”: “192.168.11.27”  
> },  
> {  
> “host”: “dc028”,  
> “ipv4”: “192.168.11.28”  
> },  
> {  
> “host”: “dc032”,  
> “hwaddress”: “30:85:a9:a3:f4:1d”,  
> “ipv4”: “192.168.11.32”  
> },  
> {  
> “host”: “dc033”,  
> “hwaddress”: “d8:cb:8a:c3:6a:f8”,  
> “ipv4”: “192.168.11.33”  
> },  
> {  
> “host”: “dc034”,  
> “hwaddress”: “70:4d:7b:a3:66:f1”,  
> “ipv4”: “192.168.11.34”,  
> “iuser”: “False”,  
> “user”: [  
> “changch”  
> ]  
> }  
> ]  
> }

but then how do I fecth the user value only when iuser is True ?

---

<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 27, 2019, 11:45am UTC](https://forum.ansible.com/t/problem-to-fetch-value-from-var-files/30667/2 "2019-06-27T11:45:04Z")

</div>

{{ ch['client\_hosts']['abc.com'] | selectattr('iuser', 'defined') | selectattr('iuser') | list }}
