# Ansible : query an output array

**URL:** <https://forum.ansible.com/t/ansible-query-an-output-array/23996>\
**Category:** Ansible Project\
**Created:** [March 17, 2017, 10:27am UTC](https://forum.ansible.com/t/ansible-query-an-output-array/23996 "2017-03-17T10:27:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Z-obaze](https://avatars.discourse-cdn.com/v4/letter/z/7c8e57/32.png) [@Z-obaze](https://forum.ansible.com/u/Z-obaze)\
**Post date:** [March 17, 2017, 10:27am UTC](https://forum.ansible.com/t/ansible-query-an-output-array/23996/1 "2017-03-17T10:27:19Z")

</div>

Hi everyone,

I’m wondering if there is a simple way to display the value of on exact item for each row whatever the number of entries we have ?

I’ll explain myself, I have an ouput which looks like this :

`ok: [server1] => { "requirements": { "changed": false, "msg": "All items completed", "results": [ { "_ansible_item_result": true, "_ansible_no_log": false, "_ansible_parsed": true, "changed": false, "invocation": { "module_args": { "conf_file": null, "disable_gpg_check": false, "disablerepo": null, "enablerepo": null, "exclude": null, "install_repoquery": true, "list": "first_package", "name": null, "state": "installed", "update_cache": false, "validate_certs": true }, "module_name": "yum" }, "item": "first_package", "results": [`{` `“arch”: “x86\_64”,` `“epoch”: “0”,` `“name”: “name\_of\_the\_package”,` `“nevra”: “0:first\_package”,` `“release”: “release\_of\_the\_package”,` `“repo”: “repo”,` `“version”: “version\_of\_the\_package”,` `“yumstate”: “available”` `}`  
]  
},  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“conf\_file”: null,  
“disable\_gpg\_check”: false,  
“disablerepo”: null,  
“enablerepo”: null,  
“exclude”: null,  
“install\_repoquery”: true,  
“list”: “second\_package”,  
“name”: null,  
“state”: “installed”,  
“update\_cache”: false,  
“validate\_certs”: true  
},  
“module\_name”: “yum”  
},  
“item”: “second\_package”,  
“results”: [  
{  
“arch”: “x86\_64”,  
“epoch”: “0”,  
“name”: “`name_of_the_package`”,  
“nevra”: “0:second\_package”,  
“release”: “`release_of_the_package`”,  
“repo”: “repo”,  
“version”: "`version_of_the_package"`,  
“yumstate”: “available”  
}  
]

`

And with that output, i’d like to display the value of “yumstate” entry for all of the items, here we have two items, but we could have 3, 4 or more.

Within my playbook i tried to do something like this, but of course it doesn’t work :

`

- name: debug  
debug:  
var: ‘requirements.results[_].results[_].yumstate’  
`

I also tried with a loop, but I haven’t found anything.

So does anyone have a clue to do this?

Thanks for your help

---

<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:** [March 17, 2017, 4:51pm UTC](https://forum.ansible.com/t/ansible-query-an-output-array/23996/2 "2017-03-17T16:51:19Z")

</div>

- debug: var=item.results.0.yumstate  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ requirements.results }}"

---

<div class="post-metadata">

**Author:** ![Z-obaze](https://avatars.discourse-cdn.com/v4/letter/z/7c8e57/32.png) [@Z-obaze](https://forum.ansible.com/u/Z-obaze)\
**Post date:** [March 20, 2017, 5:35pm UTC](https://forum.ansible.com/t/ansible-query-an-output-array/23996/3 "2017-03-20T17:35:17Z")

</div>

Thanks a lot, it was just what I needed.

---

<div class="post-metadata">

**Author:** ![Z-obaze](https://avatars.discourse-cdn.com/v4/letter/z/7c8e57/32.png) [@Z-obaze](https://forum.ansible.com/u/Z-obaze)\
**Post date:** [March 28, 2017, 9:44am UTC](https://forum.ansible.com/t/ansible-query-an-output-array/23996/4 "2017-03-28T09:44:46Z")

</div>

Hi,

I’m finally stuck with another case.

Sometimes I could get something like this :

> ok: [server1] =\> {  
> “requirements”: {  
> “changed”: false,  
> “msg”: “All items completed”,  
> “results”: [  
> {  
> “\_ansible\_item\_result”: true,  
> “\_ansible\_no\_log”: false,  
> “\_ansible\_parsed”: true,  
> “changed”: false,  
> “invocation”: {  
> “module\_args”: {  
> “conf\_file”: null,  
> “disable\_gpg\_check”: false,  
> “disablerepo”: null,  
> “enablerepo”: null,  
> “exclude”: null,  
> “install\_repoquery”: true,  
> “list”: “first\_package”,  
> “name”: null,  
> “state”: “installed”,  
> “update\_cache”: false,  
> “validate\_certs”: true  
> },  
> “module\_name”: “yum”  
> },  
> “item”: “first\_package”,  
> “results”: [  
> {  
> “arch”: “x86\_64”,  
> “epoch”: “0”,  
> “name”: “name\_of\_the\_package”,  
> “nevra”: “0:first\_package”,  
> “release”: “release\_of\_the\_package”,  
> “repo”: “repo”,  
> “version”: “version\_of\_the\_package”,  
> “yumstate”: “available”  
> }  
> {  
> “arch”: “x86\_64”,  
> “epoch”: “0”,  
> “name”: “name\_of\_the\_package”,  
> “nevra”: “0:first\_package”,  
> “release”: “release\_of\_the\_package”,  
> “repo”: “installed”,  
> “version”: “version\_of\_the\_package”,  
> “yumstate”: “installed”  
> }  
> ]  
> },  
> {  
> “\_ansible\_item\_result”: true,  
> “\_ansible\_no\_log”: false,  
> “\_ansible\_parsed”: true,  
> “changed”: false,  
> “invocation”: {  
> “module\_args”: {  
> “conf\_file”: null,  
> “disable\_gpg\_check”: false,  
> “disablerepo”: null,  
> “enablerepo”: null,  
> “exclude”: null,  
> “install\_repoquery”: true,  
> “list”: “second\_package”,  
> “name”: null,  
> “state”: “installed”,  
> “update\_cache”: false,  
> “validate\_certs”: true  
> },  
> “module\_name”: “yum”  
> },  
> “item”: “second\_package”,  
> “results”: [  
> {  
> “arch”: “x86\_64”,  
> “epoch”: “0”,  
> “name”: “name\_of\_the\_package”,  
> “nevra”: “0:second\_package”,  
> “release”: “release\_of\_the\_package”,  
> “repo”: “repo”,  
> “version”: “version\_of\_the\_package”,  
> “yumstate”: “available”  
> }  
> ]

So here, I have two results for the same package.  
With the tip you gave me, I can only look into the first one, I know I could add “item.results.1.yumstate” but I don’t want to cause I could get more than 2 results.

I tried with the “with\_subelements” but it split the array by the number of results I have and in that case I want to compare “item.results.0.yumstate” and “item.results.1.yumstate”? But as I said I could get more.

Thanks for your help

---

<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:** [March 29, 2017, 8:42am UTC](https://forum.ansible.com/t/ansible-query-an-output-array/23996/5 "2017-03-29T08:42:07Z")

</div>

To do this you need a loop in a loop.

Put the inner most loop in a yaml file and use include with with\_itmes to loop the outer item and use loop\_control to change the variable name.  
[https://docs.ansible.com/ansible/playbooks\_loops.html#loop-control](https://docs.ansible.com/ansible/playbooks_loops.html#loop-control)

---

<div class="post-metadata">

**Author:** ![Z-obaze](https://avatars.discourse-cdn.com/v4/letter/z/7c8e57/32.png) [@Z-obaze](https://forum.ansible.com/u/Z-obaze)\
**Post date:** [March 29, 2017, 2:14pm UTC](https://forum.ansible.com/t/ansible-query-an-output-array/23996/6 "2017-03-29T14:14:08Z")

</div>

Thanks.

And is there a way to not use include and to keep the inner loop into the same playbook than the outer loop?  
Cause I don’t like the way it splits the playbook into two yaml files.

---

<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:** [March 29, 2017, 2:35pm UTC](https://forum.ansible.com/t/ansible-query-an-output-array/23996/7 "2017-03-29T14:35:04Z")

</div>

No, that is the only way to do it in Ansible.

If you are only using the data in a template then you can do loop in a loop in Jinja2.

---

<div class="post-metadata">

**Author:** ![Z-obaze](https://avatars.discourse-cdn.com/v4/letter/z/7c8e57/32.png) [@Z-obaze](https://forum.ansible.com/u/Z-obaze)\
**Post date:** [March 30, 2017, 9:14am UTC](https://forum.ansible.com/t/ansible-query-an-output-array/23996/8 "2017-03-30T09:14:54Z")

</div>

OK thanks.

And do you know if there is a way with the yum module to not have those two entries when the latest package is installed ? I’d like to have only the one with “yumstate=installed” and not the one with available state.  
Cause actually I just want to know if one package is installed or not, but I don’t want to install it, just check, that’s why I don’t want to use the “state=installed” or even “present” cause it will install the package if it’s not here.  
I tried a lot of thing but so far didn’t find what I wanted.  
does it make sense?

Thanks
