Skip to main content
Home
badllama.com
  • Search
  • Log In

Iterating lists of lists using Ansible

cpeters's picture

Mon, 02/06/2017 - 05:00 by cpeters

Let's say you want to query some information for one of your instances, like the volumes associated with it:

Using the AWS CLI, you might do something like this to traverse the lists within lists:

aws ec2 describe-instances --instance-ids i-3e9217aa --query Reservations[*].Instances[*].BlockDeviceMappings[*]
.Ebs.VolumeId 

Assuming your using an Ansible dynamic inventory, you can traverse the sub-lists of your inventory like this:

 
 - debug:
      msg: "{{ item.1.volume_id  }}"
    with_subelements:
      - "{{ ec2.instances }}"
      - "block_device_mapping"
Tags: 
dynamic inventory ansible ec2 aws
Powered by Backdrop CMS