# Access items in a sub-array

**URL:** <https://forum.ansible.com/t/access-items-in-a-sub-array/37693>\
**Category:** Ansible Project\
**Created:** [February 23, 2023, 7:31pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693 "2023-02-23T19:31:31Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 23, 2023, 7:31pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/1 "2023-02-23T19:31:31Z")

</div>

I am using the ovirt\_snapshot\_info module to obtain information about our VM snapshots. I register the output and it contains an array of values, 1 for each VM, then within that output, there’s an array of values called ovirt\_snapshots (See sample below). I’m trying to get to the “description” value for each snapshot, but for the life of me, I can’t get it to work. I’d appreciate any thoughts or ideas.

My sample output:

TASK [Show results] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: {  
“changed”: false,  
“msg”: “All items completed”,  
“results”: [  
{  
“ansible\_loop\_var”: “item”,  
“changed”: false,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“description”: null,  
“fetch\_nested”: false,  
“nested\_attributes”: ,  
“snapshot\_id”: null,  
“vm”: “vm1”  
}  
},  
“item”: “vm1”,  
“ovirt\_snapshots”: [  
{  
“cdroms”: ,  
“date”: “2022-05-12 13:13:19.186000+00:00”,  
“description”: “Snapshot before 12.2 update may122022”,  
“disks”: ,  
“href”: “/ovirt-engine/api/vms/1eadb46c-e8a9-4ecd-b375-15b93079e1b3/snapshots/187b0431-b7fa-4b29-9dbc-d5298c0ba3e4”,  
“id”: “187b0431-b7fa-4b29-9dbc-d5298c0ba3e4”,  
“nics”: ,  
“persist\_memorystate”: true,  
“snapshot\_status”: “ok”,  
“snapshot\_type”: “regular”,

My playbook code:

- name: Show results  
ansible.builtin.debug:  
msg: “{{ result }}”  
with\_nested:
- “{{ item.ovirt\_snapshots }}”  
loop:
- “{{ result.results }}”

Thanks,  
Harry

---

<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:** [February 23, 2023, 7:34pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/2 "2023-02-23T19:34:03Z")

</div>

- name: Show results  
ansible.builtin.debug:  
msg: “{{ item }}”  
loop:
- “{{ result.results.ovirt\_snapshots }}”

How about this?

Walter

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 23, 2023, 7:37pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/3 "2023-02-23T19:37:26Z")

</div>

Nope. I get: "list object has no attribute ‘ovirt\_snapshots’.

Thanks,  
Harry

---

<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:** [February 23, 2023, 7:51pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/4 "2023-02-23T19:51:54Z")

</div>

- name: Show results  
ansible.builtin.debug:

msg: “{{ item }}”  
loop:

- “{{ result.results[0].ovirt\_snapshots }}”

Walter

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 23, 2023, 7:59pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/5 "2023-02-23T19:59:22Z")

</div>

That works, but it only shows the info for the first VM. There are 22 total. What I’d like to have is the VM name and list of all of that VMs snapshots, so I need to be able to traverse through the output at the VM level, then travers that VM’s ovirt\_snapshots data.

Thanks,  
Harry

---

<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:** [February 23, 2023, 8:12pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/6 "2023-02-23T20:12:38Z")

</div>

How about this?

- name: Show results  
ansible.builtin.debug:

msg: “{{ item }}”  
loop:

- “{{ result.results | json\_query(‘[\*].ovirt\_snapshots’) }}”

Walter

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 23, 2023, 8:21pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/7 "2023-02-23T20:21:34Z")

</div>

So that gives me the data under each VM’s ovirt\_snapshots, but I still can’t access the “description” data. I tried this:

- name: Show results  
ansible.builtin.debug:  
msg: “{{ item }}”  
with\_nested:
- “{{ item[0].description }}”  
loop:
- “{{ result.results | json\_query(‘[\*].ovirt\_snapshots’) }}”

And it shows me the same information with or without the “with\_nested” clause in there.

Thanks,  
Harry

---

<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:** [February 23, 2023, 8:32pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/8 "2023-02-23T20:32:15Z")

</div>

- name: Show results  
ansible.builtin.debug:

msg: “{{ item.description }}”  
loop:

- “{{ result.results | json\_query(‘[\*].ovirt\_snapshots’) }}”

Walter

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 23, 2023, 8:35pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/9 "2023-02-23T20:35:14Z")

</div>

Still no good:

TASK [Show results] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘description’ is undefined. ‘description’ is undefined\n\nThe error appears to be in ‘/root/vm\_snapshot\_info.yml’: line 48, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Show results\n ^ here\n”}

- name: Show results  
ansible.builtin.debug:  
msg: “{{ item,description }}”  
loop:
- “{{ result.results | json\_query(‘[\*].ovirt\_snapshots’) }}”

Thanks,  
Harry

---

<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:** [February 23, 2023, 8:40pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/10 "2023-02-23T20:40:19Z")

</div>

Can you share the output from this:

- name: Show results  
ansible.builtin.debug:

msg: “{{ item }}”  
loop:

- “{{ result.results | json\_query(‘[\*].ovirt\_snapshots’) }}”

I suspect at this point it is just a matter of constructing the right reference in ‘item’.

Walter

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 24, 2023, 2:26am UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/11 "2023-02-24T02:26:48Z")

</div>

Here ya go (I pulled out a bunch of output that just muddied up everything:

“msg”: [  
[  
{  
“date”: “2021-04-10 00:19:25.785000+00:00”,  
“description”: “Active VM”,  
“href”: “/ovirt-engine/api/vms/e0abbe35-3c4c-463b-8d08-587618d2465d/snapshots/22e82416-71b4-4808-b315-4cf7d713ae31”,  
“id”: “22e82416-71b4-4808-b315-4cf7d713ae31”,  
“persist\_memorystate”: false,  
“snapshot\_status”: “ok”,  
“snapshot\_type”: “active”  
},  
{  
“cdroms”: ,  
“date”: “2023-02-22 09:02:48.843000+00:00”,  
“description”: “Snapshot of VM1 2023-02-22”,  
“disks”: ,  
/\* Redacted extra output _/  
},  
{  
“cdroms”: [],  
“date”: “2023-02-23 09:03:23.519000+00:00”,  
“description”: “Snapshot of VM1 2023-02-23”,  
“disks”: [],  
/_ Redacted extra output \*/  
}  
]  
]  
}

Thanks,  
Harry

---

<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:** [February 24, 2023, 12:10pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/12 "2023-02-24T12:10:51Z")

</div>

Try this reference:

- name: Show results  
ansible.builtin.debug:

msg: “{{ item[‘description’] }}”  
loop:

- “{{ result.results | json\_query(‘[\*].ovirt\_snapshots’) }}”

Walter

---

<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:** [February 24, 2023, 12:17pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/13 "2023-02-24T12:17:56Z")

</div>

That was close, but you used a comma instead of a period in “{{ item.description }}”.

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 24, 2023, 11:20pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/14 "2023-02-24T23:20:49Z")

</div>

I tried it 4 ways:  
msg: “{{ item.description }}”  
msg: “{{ item[‘description’] }}”  
msg: “{{ item[0].description }}”  
msg: “{{ item[0][‘description’] }}”

and all 4 give me the following error:

TASK [Show results] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘description’. ‘list object’ has no attribute ‘description’\n\nThe error appears to be in ‘/root/vm\_snapshot\_info.yml’: line 48, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Show results\n ^ here\n”}

Thanks,  
Harry

---

<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:** [February 27, 2023, 2:22pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/15 "2023-02-27T14:22:41Z")

</div>

Again, without the play including the “loop:” we have no idea what “item” looks like.

When you get something like the error you quoted, where it’s looking at an object (a list object by that message) and you don’t know how to reference data contained within, just take off all the other bits and have debug show you

msg: “{{ item }}”

Then work your way one level at a time until you get the expression right.

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 27, 2023, 3:00pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/16 "2023-02-27T15:00:25Z")

</div>

I get what you’re saying, but when I just debug print {{ item }} using the results variable, I see all of the snapshots listed. If I add in the loop and use {{ item[0] }}, I see the same information, and description is included. But when I try to add description to the output, I am told that it doesn’t exist. So I’m at the “until you get the expression right” phase, but no matter what I use, the expression isn’t right.

- name: Gather VM Snapshot info  
redhat.rhv.ovirt\_snapshot\_info:  
auth: “{{ ovirt\_auth }}”  
vm: “{{ item }}”  
register: result  
with\_items:

- “vm1”

- name: Show results  
ansible.builtin.debug:  
msg: “{{ item[0].description }}”  
loop:

- “{{ result.results | json\_query(‘[\*].ovirt\_snapshots’) }}”

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 27, 2023, 3:14pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/17 "2023-02-27T15:14:31Z")

</div>

OK, I think I have it:

- name: Show results  
ansible.builtin.debug:  
msg: “{{ item }}”  
loop:
- “{{ result.results | json\_query(‘[_].ovirt\_snapshots[_].description’) }}”

This gives me what I want, to a point. I would like to print the VM name too, and that is at result.results[\*].item. Any ideas on how I can adde that?

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 27, 2023, 3:26pm UTC](https://forum.ansible.com/t/access-items-in-a-sub-array/37693/18 "2023-02-27T15:26:53Z")

</div>

OK, finally figured it out. Used with\_nested and was able to print both items:

- name: Show results  
ansible.builtin.debug:  
msg: “{{ item[0] }} - {{ item[1] }}”  
with\_nested:
- “{{ result.results | json\_query(‘[\*].item’) }}”
- “{{ result.results | json\_query(‘[_].ovirt\_snapshots[_].description’) }}”

Thanks,  
Harry
