# EC2 module, exact\_count, and loops

**URL:** <https://forum.ansible.com/t/ec2-module-exact-count-and-loops/19467>\
**Category:** Ansible Project\
**Created:** [October 8, 2015, 6:23pm UTC](https://forum.ansible.com/t/ec2-module-exact-count-and-loops/19467 "2015-10-08T18:23:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guy\_Knights](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Guy\_Knights](https://forum.ansible.com/u/Guy_Knights)\
**Post date:** [October 8, 2015, 6:23pm UTC](https://forum.ansible.com/t/ec2-module-exact-count-and-loops/19467/1 "2015-10-08T18:23:50Z")

</div>

I have a playbook that fires up some ec2 instances in multiple subnets using exact\_count and count\_tag to control how many instances are created. I’m using when to loop over the subnets I want to use - in this particular case, there are 2 subnets in different availability zones, and I want to create one instance in each AZ.

The problem is that if I set exact\_count to 1, ansible creates the first instance and then doesn’t create the second instance in the second subnet as exact\_count is already reached. If I change exact\_count to 2, it will create 2 instances but they’re in the same subnet. Basically, the loop is irrelevant.

Is there any way around this issue?

Thanks,  
Guy

---

<div class="post-metadata">

**Author:** ![Guy\_Knights](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Guy\_Knights](https://forum.ansible.com/u/Guy_Knights)\
**Post date:** [October 8, 2015, 6:34pm UTC](https://forum.ansible.com/t/ec2-module-exact-count-and-loops/19467/2 "2015-10-08T18:34:12Z")

</div>

Actually, I just played with this a bit more and registered the output of the EC2 task that creates these instances. I’ve run through this a number of times so the 1 instance already exists, but the output of my Ansible run indicates that it thinks there are 2 instances. The following is the registered output:

`ok: [localhost] => { "var": { "app_var": { "changed": false, "msg": "All items completed", "results": [{ "changed": false, "skipped": true }, { "changed": false, "instance_ids": null, "instances": [], "invocation": { "module_args": "", "module_name": "ec2" }, "item": { "az": "us-west-1a", "cidr": "10.1.2.0/24", "id": "subnet-ecea39b5", "resource_tags": { "Name": "example-subnet-app" } }, "tagged_instances": [{ "ami_launch_index": "0", "architecture": "x86_64", "dns_name": "", "ebs_optimized": false, "groups": { "sg-3684fe53": "example-sg-app", "sg-3c84fe59": "example-sg-access" }, "hypervisor": "xen", "id": "i-cb3fe479", "image_id": "ami-df6a8b9b", "instance_type": "c4.large", "kernel": null, "key_name": "example_key", "launch_time": "2015-09-28T22:39:11.000Z", "placement": "us-west-1a", "private_dns_name": "ip-10-1-2-63.us-west-1.compute.internal", "private_ip": "10.1.2.63", "public_dns_name": "", "public_ip": null, "ramdisk": null, "region": "us-west-1", "root_device_name": "/dev/sda1", "root_device_type": "ebs", "state": "running", "state_code": 16, "tags": { "Environment": "prod", "Name": "example-ec2-app", "Subnet": "example-subnet-app", "Type": "app", "VPC": "example" }, "tenancy": "default", "virtualization_type": "hvm" }] }, { "changed": false, "instance_ids": null, "instances": [], "invocation": { "module_args": "", "module_name": "ec2" }, "item": { "az": "us-west-1b", "cidr": "10.1.3.0/24", "id": "subnet-921486f7", "resource_tags": { "Name": "example-subnet-app" } }, "tagged_instances": [{ "ami_launch_index": "0", "architecture": "x86_64", "dns_name": "", "ebs_optimized": false, "groups": { "sg-3684fe53": "example-sg-app", "sg-3c84fe59": "example-sg-access" }, "hypervisor": "xen", "id": "i-cb3fe479", "image_id": "ami-df6a8b9b", "instance_type": "c4.large", "kernel": null, "key_name": "example_key", "launch_time": "2015-09-28T22:39:11.000Z", "placement": "us-west-1a", "private_dns_name": "ip-10-1-2-63.us-west-1.compute.internal", "private_ip": "10.1.2.63", "public_dns_name": "", "public_ip": null, "ramdisk": null, "region": "us-west-1", "root_device_name": "/dev/sda1", "root_device_type": "ebs", "state": "running", "state_code": 16, "tags": { "Environment": "prod", "Name": "example-ec2-app", "Subnet": "example-subnet-app", "Type": "app", "VPC": "example" }, "tenancy": "default", "virtualization_type": "hvm" }] }, { "changed": false, "skipped": true }, { "changed": false, "skipped": true }, { "changed": false, "skipped": true }, { "changed": false, "skipped": true }, { "changed": false, "skipped": true }, { "changed": false, "skipped": true }, { "changed": false, "skipped": true } ] } } }`

It appears to me that Ansible thinks 2 instances exist, but I only see one in the AWS console.

---

<div class="post-metadata">

**Author:** ![Matthew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Matthew\_Morgan](https://forum.ansible.com/u/Matthew_Morgan)\
**Post date:** [October 8, 2015, 6:57pm UTC](https://forum.ansible.com/t/ec2-module-exact-count-and-loops/19467/3 "2015-10-08T18:57:58Z")

</div>

The problem will most likely be with your count\_tag. If you use a tag to include the subnet in the count, you can have exact\_count=1 and multiple subnets, and it will create a box in each subnet. If you want to create different numbers of boxes in each subnet, you just expand your subnet vars to include how many, like below.

`

exact\_count: “{{item.how\_many}}”  
count\_tag:  
deploy\_env: “{{deploy\_env}}”  
service: “{{service}}”  
az: “{{item.az}}”  
region: “{{region}}”  
with\_items: subnets

`

Then subnets would look like:

`  
subnets:

- { az: a, subnet: subnet-1a2b3c4d, how\_many: 2}
- { az: b, subnet: subnet-2b3c4d5e, how\_many: 1}

`

---

<div class="post-metadata">

**Author:** ![Guy\_Knights](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Guy\_Knights](https://forum.ansible.com/u/Guy_Knights)\
**Post date:** [October 8, 2015, 6:59pm UTC](https://forum.ansible.com/t/ec2-module-exact-count-and-loops/19467/4 "2015-10-08T18:59:07Z")

</div>

Ahhh…that makes so much sense. Thank you!
