# how i will get rid of dictionary or only grab first match in loop output with satisfying when condition.

**URL:** <https://forum.ansible.com/t/how-i-will-get-rid-of-dictionary-or-only-grab-first-match-in-loop-output-with-satisfying-when-condition/37569>\
**Category:** Ansible Project\
**Created:** [January 23, 2023, 2:42pm UTC](https://forum.ansible.com/t/how-i-will-get-rid-of-dictionary-or-only-grab-first-match-in-loop-output-with-satisfying-when-condition/37569 "2023-01-23T14:42:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [January 23, 2023, 2:42pm UTC](https://forum.ansible.com/t/how-i-will-get-rid-of-dictionary-or-only-grab-first-match-in-loop-output-with-satisfying-when-condition/37569/1 "2023-01-23T14:42:02Z")

</div>

Hi Team,

how i will get rid of dictionary or only grab first match in loop output with satisfying when condition.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 23, 2023, 10:08pm UTC](https://forum.ansible.com/t/how-i-will-get-rid-of-dictionary-or-only-grab-first-match-in-loop-output-with-satisfying-when-condition/37569/2 "2023-01-23T22:08:37Z")

</div>

That is strange, because I copy-n-pasted you play and data, and it worked for me. Does your output before your last step look like mine?

$ ansible-playbook isilon.yml

PLAY [Isilon tests] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [First Read isilon cluster csv file from mount point for login] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [Print The inventry Full File in List format] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
{  
“Business Purpose”: “KPHC”,  
“Cluster Name”: “cnnndcisip-hc01”,  
“Data Center”: “NDC”,  
“Dedicated region”: “SC”,  
“Environment”: “Production”,  
“IP Address”: “172.19.94.62”,  
“Serial No.”: “CF2ZJ203800133”  
},  
{  
“Business Purpose”: “KPHC”,  
“Cluster Name”: “”,  
“Data Center”: “NDC”,  
“Dedicated region”: “”,  
“Environment”: “Production”,  
“IP Address”: “172.19.94.63”,  
“Serial No.”: “CF2ZJ204300024”  
},  
{  
“Business Purpose”: “KPHC”,  
“Cluster Name”: “”,  
“Data Center”: “NDC”,  
“Dedicated region”: “”,  
“Environment”: “Production”,  
“IP Address”: “172.19.94.64”,  
“Serial No.”: “CF2ZJ204300026”  
},  
{  
“Business Purpose”: “KPHC”,  
“Cluster Name”: “massdcisip-hc01”,  
“Data Center”: “SSDC”,  
“Dedicated region”: “MA”,  
“Environment”: “Production”,  
“IP Address”: “172.30.146.231”,  
“Serial No.”: “JACNT205160062”  
},  
{  
“Business Purpose”: “KPHC”,  
“Cluster Name”: “”,  
“Data Center”: “SSDC”,  
“Dedicated region”: “”,  
“Environment”: “Production”,  
“IP Address”: “172.30.146.232”,  
“Serial No.”: “JACNT210260055”  
},  
{  
“Business Purpose”: “KPHC”,  
“Cluster Name”: “”,  
“Data Center”: “SSDC”,  
“Dedicated region”: “”,  
“Environment”: “Production”,  
“IP Address”: “172.30.146.233”,  
“Serial No.”: “JACNT210260073”  
},  
{  
“Business Purpose”: “KPHC”,  
“Cluster Name”: “”,  
“Data Center”: “SSDC”,  
“Dedicated region”: “”,  
“Environment”: “Production”,  
“IP Address”: “172.30.146.234”,  
“Serial No.”: “JACNT210260083”  
}  
]  
}

TASK [Get isilon host from isilon cluster file.] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost] =\> (item={‘Data Center’: ‘NDC’, ‘Environment’: ‘Production’, ‘Business Purpose’: ‘KPHC’, ‘Serial No.’: ‘CF2ZJ203800133’, ‘Cluster Name’: ‘cnnndcisip-hc01’, ‘Dedicated region’: ‘SC’, ‘IP Address’: ‘172.19.94.62’})  
skipping: [localhost] =\> (item={‘Data Center’: ‘NDC’, ‘Environment’: ‘Production’, ‘Business Purpose’: ‘KPHC’, ‘Serial No.’: ‘CF2ZJ204300024’, ‘Cluster Name’: ‘’, ‘Dedicated region’: ‘’, ‘IP Address’: ‘172.19.94.63’})  
skipping: [localhost] =\> (item={‘Data Center’: ‘NDC’, ‘Environment’: ‘Production’, ‘Business Purpose’: ‘KPHC’, ‘Serial No.’: ‘CF2ZJ204300026’, ‘Cluster Name’: ‘’, ‘Dedicated region’: ‘’, ‘IP Address’: ‘172.19.94.64’})  
ok: [localhost] =\> (item={‘Data Center’: ‘SSDC’, ‘Environment’: ‘Production’, ‘Business Purpose’: ‘KPHC’, ‘Serial No.’: ‘JACNT205160062’, ‘Cluster Name’: ‘massdcisip-hc01’, ‘Dedicated region’: ‘MA’, ‘IP Address’: ‘172.30.146.231’})  
skipping: [localhost] =\> (item={‘Data Center’: ‘SSDC’, ‘Environment’: ‘Production’, ‘Business Purpose’: ‘KPHC’, ‘Serial No.’: ‘JACNT210260055’, ‘Cluster Name’: ‘’, ‘Dedicated region’: ‘’, ‘IP Address’: ‘172.30.146.232’})  
skipping: [localhost] =\> (item={‘Data Center’: ‘SSDC’, ‘Environment’: ‘Production’, ‘Business Purpose’: ‘KPHC’, ‘Serial No.’: ‘JACNT210260073’, ‘Cluster Name’: ‘’, ‘Dedicated region’: ‘’, ‘IP Address’: ‘172.30.146.233’})  
skipping: [localhost] =\> (item={‘Data Center’: ‘SSDC’, ‘Environment’: ‘Production’, ‘Business Purpose’: ‘KPHC’, ‘Serial No.’: ‘JACNT210260083’, ‘Cluster Name’: ‘’, ‘Dedicated region’: ‘’, ‘IP Address’: ‘172.30.146.234’})

TASK [Print The File System Name] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: [  
“massdcisip-hc01”,  
“172.30.146.231”  
]  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

---

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [January 24, 2023, 10:38am UTC](https://forum.ansible.com/t/how-i-will-get-rid-of-dictionary-or-only-grab-first-match-in-loop-output-with-satisfying-when-condition/37569/3 "2023-01-24T10:38:42Z")

</div>

Yes, that was same , i also got the perfect list of dictionaries , but it giving bad error

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 24, 2023, 12:36pm UTC](https://forum.ansible.com/t/how-i-will-get-rid-of-dictionary-or-only-grab-first-match-in-loop-output-with-satisfying-when-condition/37569/4 "2023-01-24T12:36:11Z")

</div>

I like to use json\_query to search a JSON list. This saves “looping” in ansible. I find it faster. The result is a list, but you can always select your\_list[0] for the first one.

# query json list for data center and region match

- set\_fact:  
**my\_isilons** : “{{ isilon\_cluster\_output.list | **json\_query(‘[?`Data Center`==`'+primary_data_center+'` && `Dedicated region` ==`'+region+'`]’)** | list }}”

# display entire result of query

- debug:  
msg: "{{ **my\_isilons** }}’

# display first item if query returned 1 or more

- debug:  
msg: "{{ **my\_isilons** \*\*[0]\*\* }}’

when: **my\_isilons** is iterable and len( **my\_isilons** ) \> 0

The added benefit of this method is you can now test in later tasks whether the query result is interable and has a length greater than zero to determine whether you found a match.

# get cluster name and ip address if query returned 1 or more

- set\_fact:

isilon\_cluster\_name\_p: “{{ **my\_isilons** \*\*[0]**[‘Cluster Name’] }}"  
isilon\_ip\_address\_p: "{{ my\_isilons\*\*[0]**[‘IP Address’] }}”

when: **my\_isilons** is iterable and len( **my\_isilons** ) \> 0

You also can act on more than one item if the json query matched multiple items.

# do some operation on all the returned results

- set\_fact:

isilon\_cluster\_name\_p: “{{ **item** [‘Cluster Name’] }}”  
isilon\_ip\_address\_p: “{{ **item** [‘IP Address’] }}”

loop: “{{ **my\_isilons** }}”  
when: **my\_isilons** is iterable and len( **my\_isilons** ) \> 0

Walter

---

<div class="post-metadata">

**Author:** ![javed\_khan\_siddque](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/javed_khan_siddque/32/4964_2.png) [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Post date:** [January 27, 2023, 1:31pm UTC](https://forum.ansible.com/t/how-i-will-get-rid-of-dictionary-or-only-grab-first-match-in-loop-output-with-satisfying-when-condition/37569/5 "2023-01-27T13:31:34Z")

</div>

Thanks .

The issue was with my csv file , i created new file and pasted same data(with same format ) and it worked  
both json\_query and item['‘column\_data’] working properly
