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

On Jan 4, 2023, at 7:28 AM, SysAdmin EM <[email protected]> wrote:

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?


--
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 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAGUDtnkSQxnygkCzH599qOWPNFmrEkVgMAC0UrcvEA7-a7u2aQ%40mail.gmail.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCAGUDtnkSQxnygkCzH599qOWPNFmrEkVgMAC0UrcvEA7-a7u2aQ%2540mail.gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7Cf60b41840ab949ce968a08daee4f3db1%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C638084321343356453%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=9fv30x7TaK0ZIAYR5riCuSU8eoCRsg7n4kPr1ct8Nx8%3D&reserved=0>.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CBBACBC6-6427-4D93-B463-4A65F3E89C35%40nist.gov.

Reply via email to