EC2 module, exact_count, and loops

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

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.

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}

`

Ahhh…that makes so much sense. Thank you!