Get all key from output

60 views
Skip to first unread message

SysAdmin EM

unread,
Jan 4, 2023, 7:29:03 AM1/4/23
to ansible...@googlegroups.com
Hi all, I need to get the id of all available volumes in amazon aws, to make it I have created the following

---
- name: "volumes"
 hosts: localhost
 gather_facts: no
 tasks:
   - name: "list avail volumes"
     amazon.aws.ec2_vol_info:
       region: us-east-1
       filters:
         status: available
     register: aws_volume

   - name: "show"
     debug:
       msg: "{{ aws_volume.volumes[0].id }}"

The output of the variable only shows me one result and should show me all.

ok: [localhost] => {
   "msg": "vol-0c7b50c2896e422d4"
}

when I call the variable the results are many example:

ok: [localhost] => {
   "msg": {
       "changed": false,
       "failed": false,
       "volumes": [
           {
               "attachment_set": [],
               "create_time": "2019-05-30T15:49:14.238000+00:00",
               "encrypted": false,
               "id": "vol-xxxxxxxxxx",
               "iops": 100,
               "region": "us-east-1",
               "size": 32,
               "snapshot_id": "snap-xxxxxxxx",
               "status": "available",
               "tags": {
                   "Backup": "false",
                   "Environment": "PROD",
                   "Name": "app2"
                   "Zone": "1C"
               },
               "type": "gp2",
               "zone": "us-east-1c"
           },
           {
               "attachment_set": [],
               "create_time": "2019-07-18T18:06:16.555000+00:00",
               "encrypted": false,
               "id": "vol-xxxxxxxxx",
               "iops": 100,
               "region": "us-east-1",
               "size": 32,
               "snapshot_id": "snap-xxxxxxxxxxxx",
               "status": "available",
               "tags": {
                   "Backup": "true",
                   "Environment": "PROD",
                   "Name": "app1"
               },
               "type": "gp2",
               "zone": "us-east-1c"
           },


I should use to loop over the variable "aws_volume" and then call it as {{ item.volumes[0]. id}}?

Any helps?

Rowe, Walter P. (Fed)

unread,
Jan 4, 2023, 7:44:29 AM1/4/23
to ansible...@googlegroups.com
You can use json_query to create a list of just the volume IDs.

---

- name: get aws volume IDs

  hosts: localhost

  gather_facts: false

  become: false

  vars:

    aws_volume: { 

      changed: false, 

      failed: false, 

      volumes: [ 

        { 

          attachment_set: [], 

          create_time: "2019-05-30T15:49:14.238000+00:00", 

          encrypted: false, 

          id: "vol-xxxxxxxxxx", 

          iops: 100, 

          region: "us-east-1", 

          size: 32, 

          snapshot_id: "snap-xxxxxxxx", 

          status: "available", 

          tags: { 

            Backup: "false", 

            Environment: "PROD", 

            Name: "app2",

            Zone: "1C" 

          }, 

          type: "gp2", 

          zone: "us-east-1c" 

        }, 

        { 

          attachment_set: [], 

          create_time: "2019-07-18T18:06:16.555000+00:00", 

          encrypted: false, 

          id: "vol-xxxxxxxxx", 

          iops: 100, 

          region: "us-east-1", 

          size: 32, 

          snapshot_id: "snap-xxxxxxxxxxxx", 

          status: "available", 

          tags: { 

            Backup: "true", 

            Environment: "PROD", 

            Name: "app1" 

          }, 

          type: "gp2", 

          zone: "us-east-1c" 

        }

      ]

    }


  tasks:

    - name: get volume IDs from JSON list

      set_fact:

        vol_ids: "{{ aws_volume.volumes | json_query('[].id') | list }}"

    - name: show volume IDs

      debug: var=vol_ids




PLAY [get aws volume IDs] **********************************************************************************************


TASK [get volume IDs from JSON list] ***********************************************************************************

ok: [localhost]


TASK [show volume IDs] *************************************************************************************************

ok: [localhost] => {

    "vol_ids": [

        "vol-xxxxxxxxxx",

        "vol-xxxxxxxxx"

    ]

}


PLAY RECAP *************************************************************************************************************

localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   



Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGUDtnkSQxnygkCzH599qOWPNFmrEkVgMAC0UrcvEA7-a7u2aQ%40mail.gmail.com.

Todd Lewis

unread,
Jan 4, 2023, 7:49:14 AM1/4/23
to ansible...@googlegroups.com, uto...@gmail.com
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 }}"

SysAdmin EM

unread,
Jan 4, 2023, 7:36:27 PM1/4/23
to ansible...@googlegroups.com
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
}


---
- name: "test"
 hosts: localhost
 gather_facts: no
 vars:
   aws_volume_id:
     - vol-xxxxxxxxxx
 tasks:
   - name: "create snapshot test"
     amazon.aws.ec2_snapshot:
       volume_id: "{{ item }}"
       wait_timeout: 900
       snapshot_tags:
           MarkedForDeletion: true
     register: snapshot_info
     with_items: "{{ aws_volume_id }}"


   - set_fact:
       snap_id: "{{ snapshot_info | json_query('snapshot_id')}}"


   - name: "Debug"
     debug:
       msg: "{{ snap_id }}"

This is the output

changed: [localhost] => (item=vol-xxxxxxxxxxxx) => {
   "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-xxxxxxxxxxx",
           "wait": true,
           "wait_timeout": 900
       }
   },
   "item": "vol-xxxxxxxxx",
   "snapshot_id": "snap-xxxxxxxxxxxx",

What am I doing wrong?

Rowe, Walter P. (Fed)

unread,
Jan 5, 2023, 7:19:01 AM1/5/23
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Jan 5, 2023, 7:21:37 AM1/5/23
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

SysAdmin EM

unread,
Jan 5, 2023, 9:25:01 AM1/5/23
to ansible...@googlegroups.com

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:

---
- name: "test"
  hosts: localhost
  gather_facts: no
  vars:
    aws_volume_id:
      - vol-xxxxxxxxxxxxxxxxx
      - vol-xxxxxxxxxxxxxxxxx
  tasks:
    - name: "test"

      amazon.aws.ec2_snapshot:
        volume_id: "{{ item }}"
        wait_timeout: 900
        snapshot_tags:
            MarkedForDeletion: true
      register: snapshotid
      with_items: "{{ aws_volume_id }}"

    - name: "Debug"
      debug:
        msg: "{{ item.snapshot_id }}"
      with_items: "{{ snapshotid }}"


Mike Eggleston

unread,
Jan 5, 2023, 9:42:50 AM1/5/23
to ansible...@googlegroups.com
Are you missing a parameter “snapshot_id” in your definition of “ec2_snapshot”?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Jan 5, 2023, 10:08:38 AM1/5/23
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

SysAdmin EM

unread,
Jan 5, 2023, 10:13:44 AM1/5/23
to ansible...@googlegroups.com
ok: [localhost] => {
   "msg": "var=snapshotid\""
}

i think the "register" not work

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

SysAdmin EM

unread,
Jan 5, 2023, 10:31:19 AM1/5/23
to ansible...@googlegroups.com
I don’t know what I’m doing wrong, I’m desperate.
any help?

Todd Lewis

unread,
Jan 5, 2023, 10:39:04 AM1/5/23
to ansible...@googlegroups.com
The register worked fine, but there's a typo in your debug task, which you have not shown us.

SysAdmin EM

unread,
Jan 5, 2023, 10:42:07 AM1/5/23
to ansible...@googlegroups.com
i Try this tasks:

---
- name: "Volumenes sin usar"
 hosts: localhost
 gather_facts: yes
 vars:
   aws_volume_id:
     - vol-xxxxxxxxxxxxxxxx
 tasks:
   - name: "Creando snapshot desde el volumen"
     amazon.aws.ec2_snapshot:
       volume_id: "{{ item }}"
       wait_timeout: 900
       snapshot_tags:
         MarkedForDeletion: true
     register: show_id_snapshot
     with_items: "{{ aws_volume_id }}"

   - name: "mostrando"
     debug:
       msg: var=show_id_snapshot

All debug is here: https://pastebin.pl/view/eb4b1e29

Thanks for the help.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Jan 5, 2023, 10:44:01 AM1/5/23
to ansible...@googlegroups.com
   - 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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

SysAdmin EM

unread,
Jan 5, 2023, 10:53:57 AM1/5/23
to ansible...@googlegroups.com
Ok, the output shows me what I have been showing in previous answers.

Here my playbook
          
---
- name: "Volumenes sin usar"
 hosts: localhost
 gather_facts: yes
 vars:
   aws_volume_id:
     - vol-xxxxxxxxxxxxx
 tasks:
   - name: "Creando snapshot desde el volumen"
     amazon.aws.ec2_snapshot:
       volume_id: "{{ item }}"
       wait_timeout: 900
       snapshot_tags:
         MarkedForDeletion: true
     register: show_id_snapshot
     with_items: "{{ aws_volume_id }}"

   - name: "mostrando"
     debug: var=show_id_snapshot.snapshot_id

Here the debug https://pastebin.pl/view/20bf3d1e

will I be using some reserved word? I don’t understand where the problem is the key exists in the command output.

Rowe, Walter P. (Fed)

unread,
Jan 5, 2023, 11:05:20 AM1/5/23
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

SysAdmin EM

unread,
Jan 5, 2023, 11:25:17 AM1/5/23
to ansible...@googlegroups.com

Rowe, Walter P. (Fed)

unread,
Jan 5, 2023, 11:38:14 AM1/5/23
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

SysAdmin EM

unread,
Jan 5, 2023, 11:54:25 AM1/5/23
to ansible...@googlegroups.com
Thanks. I’m ashamed I hope you can forgive me for bothering so much.

Rowe, Walter P. (Fed)

unread,
Jan 5, 2023, 12:46:35 PM1/5/23
to ansible...@googlegroups.com
We all learn from experience and the help of others. Nothing to be ashamed about.


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
On Jan 5, 2023, at 11:53 AM, SysAdmin EM <ema...@gmail.com> wrote:

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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages