# Get all key from output

**URL:** <https://forum.ansible.com/t/get-all-key-from-output/37517>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [January 4, 2023, 12:28pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517 "2023-01-04T12:28:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 4, 2023, 12:28pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/1 "2023-01-04T12:28:22Z")

</div>

Hi all, I need to get the id of all available volumes in amazon aws, to make it I have created the following

---

<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 4, 2023, 12:44pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/2 "2023-01-04T12:44:10Z")

</div>

You can use json\_query to create a list of just the volume IDs.

---

<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 4, 2023, 12:49pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/3 "2023-01-04T12:49:03Z")

</div>

When you say “aws\_volume.volumes[0]”, you’re explicitly looking at only the first volume, so your statement “… and should show me all” is incorrect; it should show you only the first volume.

Try something like this (untested):

- name: “show”  
debug:  
msg: “{{ item.id }}”  
loop: “{{ aws\_volume.volumes }}”

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 5, 2023, 12:35am UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/4 "2023-01-05T00:35:41Z")

</div>

I understand, I could make it work, but not by using the ec2 snapshot module.

I try to save a key as a variable but the result is empty

ok: [localhost] =\> {  
“ansible\_facts”: {  
“snap\_id”: “”  
},  
“changed”: false  
}

---

<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 5, 2023, 12:18pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/5 "2023-01-05T12:18:44Z")

</div>

You need to first look at the registered out snapshot\_info to see what its dictionary looks like. After you snapshot the EC2, insert a debug: var=snapshot\_info to see what you are being given back. With that you can to then construct a proper reference to the snapshot\_id.

Walter

---

<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 5, 2023, 12:21pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/6 "2023-01-05T12:21:15Z")

</div>

According to the ansible docs you get a single snapshot\_id.

snapshot\_info.snapshot\_id should get you the reference you need. It isn’t clear from the docs whether that is a list if the EC2 has multiple EBS volumes attached.

Walter

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 5, 2023, 2:24pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/7 "2023-01-05T14:24:10Z")

</div>

Hello! I can’t solve the problem.

This is the output

changed: [localhost] =\> (item=vol-xxxxxxxxxxxxxxxx) =\> {  
“ansible\_loop\_var”: “item”,  
“changed”: true,  
“invocation”: {  
“module\_args”: {  
“aws\_access\_key”: null,  
“aws\_ca\_bundle”: null,  
“aws\_config”: null,  
“aws\_secret\_key”: null,  
“debug\_botocore\_endpoint\_logs”: false,  
“description”: null,  
“device\_name”: null,  
“ec2\_url”: null,  
“instance\_id”: null,  
“last\_snapshot\_min\_age”: 0,  
“profile”: null,  
“region”: null,  
“security\_token”: null,  
“snapshot\_id”: null,  
“snapshot\_tags”: {  
“MarkedForDeletion”: true  
},  
“state”: “present”,  
“validate\_certs”: true,  
“volume\_id”: “vol-xxxxxxxxxxxxxxxx”,  
“wait”: true,  
“wait\_timeout”: 900  
}  
},  
“item”: “vol-xxxxxxxxxxxxxxxx”,  
“snapshot\_id”: “snap-xxxxxxxxxxxxxxxx”,  
“snapshots”: [  
{  
“description”: “”,  
“encrypted”: false,  
“owner\_id”: “xxxxxxxxxxxxxxxxxxx”,  
“progress”: “”,  
“response\_metadata”: {  
“http\_headers”: {  
“cache-control”: “no-cache, no-store”,  
“content-length”: “674”,  
“content-type”: “text/xml;charset=UTF-8”,  
“date”: “Thu, 05 Jan 2023 14:17:16 GMT”,  
“server”: “AmazonEC2”,  
“strict-transport-security”: “max-age=31536000; includeSubDomains”,  
“x-amzn-requestid”: “xxxxxxxxxxxxxxxxxxxx”  
},  
“http\_status\_code”: 200,  
“request\_id”: “xxxxxxxxxxxxxxxxxxxxxxxxx”,  
“retry\_attempts”: 0  
},  
“snapshot\_id”: “snap-xxxxxxxxxxx”,  
“start\_time”: “2023-01-05T14:17:16.785000+00:00”,  
“state”: “pending”,  
“tags”: {  
“MarkedForDeletion”: “True”  
},  
“volume\_id”: “vol-xxxxxxxxxx”,  
“volume\_size”: 400  
}  
],  
“tags”: {  
“MarkedForDeletion”: “True”  
},  
“volume\_id”: “vol-xxxxxxxxxxxxx”,  
“volume\_size”: 400  
}

i see this error

TASK [Debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/ec2/create\_ec2\_instance/playbook/test.yaml:19  
fatal: [localhost]: FAILED! =\> {  
“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘snapshot\_id’\n\nThe error appears to be in ‘/etc/ans  
ible/ec2/create\_ec2\_instance/playbook/test.yaml’: line 19, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line app  
ears to be:\n\n\n - name: "Debug"\n ^ here\n”  
}

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

Here my playbook:

---

<div class="post-metadata">

**Author:** ![Mike\_Eggleston](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Mike\_Eggleston](https://forum.ansible.com/u/Mike_Eggleston)\
**Post date:** [January 5, 2023, 2:42pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/8 "2023-01-05T14:42:36Z")

</div>

Are you missing a parameter “snapshot\_id” in your definition of “ec2\_snapshot”?

---

<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 5, 2023, 3:08pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/9 "2023-01-05T15:08:26Z")

</div>

Please change this:

- name: “Debug”  
debug:  
msg: “{{ item.snapshot\_id }}”  
with\_items: “{{ snapshotid }}”

to this:

- name: “Debug”  
debug: var=snapshotid

So you can see the entire registered result returned from the prior task.

Walter

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 5, 2023, 3:13pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/10 "2023-01-05T15:13:01Z")

</div>

ok: [localhost] =\> {  
“msg”: “var=snapshotid"”  
}

i think the “register” not work

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 5, 2023, 3:30pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/11 "2023-01-05T15:30:26Z")

</div>

I don’t know what I’m doing wrong, I’m desperate.  
any help?

---

<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 5, 2023, 3:38pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/12 "2023-01-05T15:38:52Z")

</div>

The register worked fine, but there’s a typo in your debug task, which you have not shown us.

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 5, 2023, 3:41pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/13 "2023-01-05T15:41:19Z")

</div>

i Try this tasks:

---

<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 5, 2023, 3:43pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/14 "2023-01-05T15:43:47Z")

</div>

- name: “mostrando”  
debug:  
msg: var=show\_id\_snapshot

should be

- name: “mostrando”  
debug: var=show\_id\_snapshot

or

- name: “mostrando”  
debug:  
var: show\_id\_snapshot

or

- name: “mostrando”  
debug:  
msg: “{{ show\_id\_snapshot }}”

Walter

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 5, 2023, 3:53pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/15 "2023-01-05T15:53:02Z")

</div>

Ok, the output shows me what I have been showing in previous answers.

Here my playbook

---

<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 5, 2023, 4:05pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/16 "2023-01-05T16:05:05Z")

</div>

Change this:

- name: “mostrando”  
debug: var=show\_id\_snapshot.snapshot\_id

to

- name: “mostrando”  
debug: var=show\_id\_snapshot

so we can see the entirety of what is registered.

Walter

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 5, 2023, 4:24pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/17 "2023-01-05T16:24:32Z")

</div>

registered output here [https://pastebin.pl/view/b43d2333](https://pastebin.pl/view/b43d2333)

---

<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 5, 2023, 4:37pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/18 "2023-01-05T16:37:55Z")

</div>

Looks like the reference to snapshot\_id should be:

set\_fact:  
snap\_id: “{{ show\_id\_snapshot.results[0].snapshot\_id }}”

or to get a list if there are multiples:

set\_fact:  
snap\_id: {{ show\_id\_snapshot.results | json\_query(‘.snapshot\_id’) | list }}

Walter

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [January 5, 2023, 4:53pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/19 "2023-01-05T16:53:36Z")

</div>

Thanks. I’m ashamed I hope you can forgive me for bothering so much.

---

<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 5, 2023, 5:46pm UTC](https://forum.ansible.com/t/get-all-key-from-output/37517/20 "2023-01-05T17:46:19Z")

</div>

We all learn from experience and the help of others. Nothing to be ashamed about.

Walter
