# Is there a sane way to build a list of dictionaries from two lists?

**URL:** <https://forum.ansible.com/t/is-there-a-sane-way-to-build-a-list-of-dictionaries-from-two-lists/22547>\
**Category:** Ansible Project\
**Created:** [September 14, 2016, 8:16pm UTC](https://forum.ansible.com/t/is-there-a-sane-way-to-build-a-list-of-dictionaries-from-two-lists/22547 "2016-09-14T20:16:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cohen.ben](https://avatars.discourse-cdn.com/v4/letter/c/c89c15/32.png) [@cohen.ben](https://forum.ansible.com/u/cohen.ben)\
**Post date:** [September 14, 2016, 8:16pm UTC](https://forum.ansible.com/t/is-there-a-sane-way-to-build-a-list-of-dictionaries-from-two-lists/22547/1 "2016-09-14T20:16:02Z")

</div>

I want to use with\_nested to assemble a list by iterating over two lists and extracting particular keys from both – ideally something like …:

- name: “Build flat list of security group rules that should exist”  
set\_fact:  
rulesList:
- proto: “{{ item.1.proto }}”  
from\_port: “{{ item.1.from\_port }}”  
to\_port: “{{ item.1.to\_port }}”  
cidr\_ip: “{{ item.0 }}”  
with\_nested:
- “{{ server\_ips }}”
- “{{ webservice\_security\_rules }}”

I need to _build_ the list in advance so that I can pass the entire list into the ec2\_group module – as in:

ec2\_group:  
name: some\_group  
description: All security rules for some\_group  
vpc\_id: some\_vpc  
region: some\_region  
aws\_access\_key: “{{encrypted\_access\_key}}”  
aws\_secret\_key: “{{encrypted\_secret\_key}}”  
rules: “{{rulesList}}”

NOTE: I cannot use with\_nested as an argument to the ec2\_group module, that would result in multiple task invocations – but for idempotent behavior, I need to pass ALL the rules to ec2\_group module in one invocation.

What’s a good way to accomplish this?

On a side note: I can’t understand why ansible seems to go so far out of its way to make this harder than it needs to be … Ansible really seems to be sorely lacking in features that would make it easy to build up data-structures from other data-structures …
