Iterate over dinamyc array inside a task

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.