# Iterate over dinamyc array inside a task

**URL:** <https://forum.ansible.com/t/iterate-over-dinamyc-array-inside-a-task/23697>\
**Category:** Ansible Project\
**Created:** [February 9, 2017, 6:47pm UTC](https://forum.ansible.com/t/iterate-over-dinamyc-array-inside-a-task/23697 "2017-02-09T18:47:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mariano\_Tewel](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@Mariano\_Tewel](https://forum.ansible.com/u/Mariano_Tewel)\
**Post date:** [February 9, 2017, 6:47pm UTC](https://forum.ansible.com/t/iterate-over-dinamyc-array-inside-a-task/23697/1 "2017-02-09T18:47:01Z")

</div>

Hi, how can i solve this problem? I have the following task:

- name: Provision EC2  
ec2:  
key\_name: “{{ ec2\_keypair }}”  
group\_id: “{{ ec2\_security\_group }}”  
instance\_type: “{{ ec2\_instance\_type }}”  
image: “{{ ec2\_image }}”  
vpc\_subnet\_id: “{{ item }}”  
region: “{{ ec2\_region }}”  
instance\_tags: '{“Name”:“{{ec2\_tag\_Name}}_{{ app\_env }}“,“Ansible\_Name”:”{{ec2\_tag\_Name}}_{{ app\_env }}_{{item}}“,“Type”:”{{ec2\_tag\_Type}}“,“Environment”:”{{ec2\_tag\_Environment}}"}’  
assign\_public\_ip: no  
wait: true  
exact\_count: “{{ ec2\_count\_x\_zone }}”  
count\_tag:  
Ansible\_Name: "{{ec2\_tag\_Name}}_{{ app\_env }}\_{{item}}”  
with\_items: “{{ec2\_subnet\_ids}}”  
register: ec2c2\_subnet\_ids}}"

register: ec2

- name: Add new instances to host group  
add\_host:  
name: “{{ item.tagged\_instances[0].private\_ip }}”  
groups: “tag\_Name\_{{ ec2\_tag\_Name }}\_{{ app\_env }}”  
with\_items: “{{ ec2.results}}”

This work when only if ec2\_count\_x\_zone is equal 2, but if for example ec2\_count\_x\_zone is 2 i must add :

- name: Add new instances to host group

add\_host:  
name: “{{ item.tagged\_instances[1].private\_ip }}”  
groups: “tag\_Name\_{{ ec2\_tag\_Name }}\_{{ app\_env }}”  
with\_items: “{{ ec2.results}}”

I need to iterate over item.tagged\_instances[0], item.tagged\_instances[2] or the lenght of array. I mean the lenght of array is dynamic. Is this posible?

Tia.

Mariano.
