# Using loops with sub-elements/parameters ansible, particularly ec2 module

**URL:** <https://forum.ansible.com/t/using-loops-with-sub-elements-parameters-ansible-particularly-ec2-module/19959>\
**Category:** Ansible Project\
**Created:** [December 8, 2015, 5:12am UTC](https://forum.ansible.com/t/using-loops-with-sub-elements-parameters-ansible-particularly-ec2-module/19959 "2015-12-08T05:12:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dinil](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dinil](https://forum.ansible.com/u/Dinil)\
**Post date:** [December 8, 2015, 5:12am UTC](https://forum.ansible.com/t/using-loops-with-sub-elements-parameters-ansible-particularly-ec2-module/19959/1 "2015-12-08T05:12:11Z")

</div>

Hi,  
Am trying to use Loop for ‘Volumes’ parameter of ec2 -

ec2:

prop1:

prop2:

volumes:

- device\_name : {{ item.key1 }}

- volume\_size: {{ item.key2 }}

with\_items: {{ var}}

var:

{ ‘key1’ : ‘value1’ , ‘key2’ : ‘value2’}

{ ‘key1’ : ‘value3’ , ‘key2’ : ‘value4’}

I also did try with\_dict but the loop does not correspond to the sub-element ‘volumes’, instead when it works without syntax errors, it creates multiple EC2 instances - each for one in the loop with the volume value.

Please advise how to use the ec2 module with loop for volumes, or if I have got looping sub-items incorrectly please hint on what am doing wrong.

Regards

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 8, 2015, 11:14pm UTC](https://forum.ansible.com/t/using-loops-with-sub-elements-parameters-ansible-particularly-ec2-module/19959/2 "2015-12-08T23:14:49Z")

</div>

The loop is for the whole task, that is why you get multiple instances. What you want is to pass the datastucture to the volumes key, jinja filters should help you with this.
