using nested items with json_query?

I’m trying to do the following:

  • From an object that stores multiple EC2 instance ids
  • Use those values to iterate over a second json object using each individual instance id as a filter

I’ve tried to do this with json_query with multiple approaches, including the following:

  • name: Terminate instances with tag Role = {{ ec2_tag_Role }}
    ec2:
    region: “{{ ec2_region }}”
    instance_ids: “{{ ec2_id }}”
    state: ‘absent’
    wait: true
    delegate_to: “localhost”
    register: ec2
  • name: Get node information from Shippable
    uri:
    url: https://api.shippable.com/clusterNodes
    method: GET
    headers:
    authorization: “apiToken {{ API_TOKEN }}”
    cache-control: “no-cache”
    content-type: “application/json”
    register: shippable
  • name: Get clusterNodeIds for Shippable nodes
    debug: var=item
    vars:
    query: “"json[?friendlyName==‘" + item[0] + "’].{id: node}"”
    with_nested:
  • “{{ ec2.instances.id }}”
  • “{{ shippable|json_query(query) }}”
    register: shippableClusterIds

The above attempt doesn’t work, as the vars: query statement is scoped once, not on each iteration of the nested loop. Is there any way to use the the values from the first item in the nested items inside the json_query statement? I don’t need to use json_query if there’s another way to accomplish this.