# Launch ec2 instances in multiple availability zones in single play

**URL:** <https://forum.ansible.com/t/launch-ec2-instances-in-multiple-availability-zones-in-single-play/18414>\
**Category:** Ansible Project\
**Created:** [June 5, 2015, 5:05am UTC](https://forum.ansible.com/t/launch-ec2-instances-in-multiple-availability-zones-in-single-play/18414 "2015-06-05T05:05:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![igorc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igorc/32/5491_2.png) [@igorc](https://forum.ansible.com/u/igorc)\
**Post date:** [June 5, 2015, 5:05am UTC](https://forum.ansible.com/t/launch-ec2-instances-in-multiple-availability-zones-in-single-play/18414/1 "2015-06-05T05:05:31Z")

</div>

Hi all,

I’ve been looking for the best way to achieve what is mentioned in the title of the message but haven’t found any. At least not one that will produce a satisfactory result which can be used further down in the same playbook.

For example I’ve been testing the following loop:

- name: create instance  
ec2: image=“{{ images[item.0.region] }}”  
keypair={{ keypair }}  
instance\_type={{ instance\_type }}  
instance\_tags=“{{ tags }}”  
vpc\_subnet\_id={{ item.1.subnet }}  
region={{ item.0.region }}  
group\_id={{ group\_id }}  
assign\_public\_ip=yes  
wait=true  
wait\_timeout={{ wait\_timeout }}  
with\_subelements:
- vpc
- subnets  
when: item.0.name == ec2\_env  
register: ec2\_info

which does what I want, launches one instance per AZ in the chosen region, but the output registered is way too complicated to be of any use:

TASK: [debug var=ec2\_info] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“var”: {  
“ec2\_info”: {  
“changed”: true,  
“msg”: “All items completed”,  
“results”: [  
{  
“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  
},  
{  
“changed”: true,  
“instance\_ids”: [  
“i-xxxxxxxx”  
],  
“instances”: [  
{  
“ami\_launch\_index”: “0”,  
“architecture”: “x86\_64”,  
“dns\_name”: “[xxxxxxxx.eu-west-1.compute.amazonaws.com](http://xxxxxxxx.eu-west-1.compute.amazonaws.com)”,  
“ebs\_optimized”: false,  
“groups”: {  
“sg-xxxxxxxx”: “sg-xxxxxxxx”  
},  
“hypervisor”: “xen”,  
“id”: “i-xxxxxxxx”,  
“image\_id”: “ami-47a23a30”,  
“instance\_type”: “t2.micro”,  
“kernel”: null,  
“key\_name”:“xxxxxxxx”,  
“launch\_time”: “2015-06-05T02:48:14.000Z”,  
“placement”: “eu-west-1a”,  
“private\_dns\_name”: “xxxxxxxx.eu-west-1.compute.internal”,  
“private\_ip”: “xxxxxxxx”,  
“public\_dns\_name”: “[xxxxxxxx.eu-west-1.compute.amazonaws.com](http://xxxxxxxx.eu-west-1.compute.amazonaws.com)”,  
“public\_ip”: “xxxxxxxx”,  
“ramdisk”: null,  
“region”: “eu-west-1”,  
“root\_device\_name”: “/dev/sda1”,  
“root\_device\_type”: “ebs”,  
“state”: “running”,  
“state\_code”: 16,  
“tags”: {},  
“tenancy”: “default”,  
“virtualization\_type”: “hvm”  
}  
],  
“invocation”: {  
“module\_args”: “image="ami-47a23a30" keypair=xxxxxxxx instance\_type=t2.micro instance\_tags="{‘Environment’: u’xxxxxxxx’, ‘Role’: u’server’, ‘Type’: ‘type’, ‘Name’: u’ec2-type-xxxxxxxx’, ‘Datacenter’: u’xxxxxxxx’}" vpc\_subnet\_id=subnet-xxxxxxxx region=eu-west-1 group\_id=sg-xxxxxxxx assign\_public\_ip=yes wait=true wait\_timeout=300”,  
“module\_name”: “ec2”  
},  
“item”: [  
{  
“cidr”: “xxxxxxxx”,  
“name”: “xxxxxxxx”,  
“region”: “eu-west-1”,  
“subnets\_app”: [  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1a”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1b”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1c”  
}  
],  
“subnets\_db”: [  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1a”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1b”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1c”  
}  
]  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1a”  
}  
],  
“tagged\_instances”:   
},  
{  
“changed”: true,  
“instance\_ids”: [  
“i-xxxxxxxx”  
],  
“instances”: [  
{  
“ami\_launch\_index”: “0”,  
“architecture”: “x86\_64”,  
“dns\_name”: “[xxxxxxxx.eu-west-1.compute.amazonaws.com](http://xxxxxxxx.eu-west-1.compute.amazonaws.com)”,  
“ebs\_optimized”: false,  
“groups”: {  
“sg-xxxxxxxx”: “sg-xxxxxxxx”  
},  
“hypervisor”: “xen”,  
“id”: “i-xxxxxxxx”,  
“image\_id”: “ami-47a23a30”,  
“instance\_type”: “t2.micro”,  
“kernel”: null,  
“key\_name”:“xxxxxxxx”,  
“launch\_time”: “2015-06-05T02:48:46.000Z”,  
“placement”: “eu-west-1b”,  
“private\_dns\_name”: “xxxxxxxx.eu-west-1.compute.internal”,  
“private\_ip”: “xxxxxxxx”,  
“public\_dns\_name”: “[xxxxxxxx.eu-west-1.compute.amazonaws.com](http://xxxxxxxx.eu-west-1.compute.amazonaws.com)”,  
“public\_ip”: “xxxxxxxx”,  
“ramdisk”: null,  
“region”: “eu-west-1”,  
“root\_device\_name”: “/dev/sda1”,  
“root\_device\_type”: “ebs”,  
“state”: “running”,  
“state\_code”: 16,  
“tags”: {},  
“tenancy”: “default”,  
“virtualization\_type”: “hvm”  
}  
],  
“invocation”: {  
“module\_args”: “image="ami-47a23a30" keypair=xxxxxxxx instance\_type=t2.micro instance\_tags="{‘Environment’: u’xxxxxxxx’, ‘Role’: u’server’, ‘Type’: ‘type’, ‘Name’: u’ec2-type-xxxxxxxx’, ‘Datacenter’: u’xxxxxxxx’}" vpc\_subnet\_id=subnet-xxxxxxxx region=eu-west-1 group\_id=sg-xxxxxxxx assign\_public\_ip=yes wait=true wait\_timeout=300”,  
“module\_name”: “ec2”  
},  
“item”: [  
{  
“cidr”: “xxxxxxxx”,  
“name”: “xxxxxxxx”,  
“region”: “eu-west-1”,  
“subnets\_app”: [  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1a”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1b”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1c”  
}  
],  
“subnets\_db”: [  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1a”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1b”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1c”  
}  
]  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1b”  
}  
],  
“tagged\_instances”:   
},  
{  
“changed”: true,  
“instance\_ids”: [  
“i-xxxxxxxx”  
],  
“instances”: [  
{  
“ami\_launch\_index”: “0”,  
“architecture”: “x86\_64”,  
“dns\_name”: “[xxxxxxxx.eu-west-1.compute.amazonaws.com](http://xxxxxxxx.eu-west-1.compute.amazonaws.com)”,  
“ebs\_optimized”: false,  
“groups”: {  
“sg-xxxxxxxx”: “sg-xxxxxxxx”  
},  
“hypervisor”: “xen”,  
“id”: “i-xxxxxxxx”,  
“image\_id”: “ami-47a23a30”,  
“instance\_type”: “t2.micro”,  
“kernel”: null,  
“key\_name”:“xxxxxxxx”,  
“launch\_time”: “2015-06-05T02:49:14.000Z”,  
“placement”: “eu-west-1c”,  
“private\_dns\_name”: “xxxxxxxx.eu-west-1.compute.internal”,  
“private\_ip”: “xxxxxxxx”,  
“public\_dns\_name”: “[xxxxxxxx.eu-west-1.compute.amazonaws.com](http://xxxxxxxx.eu-west-1.compute.amazonaws.com)”,  
“public\_ip”: “xxxxxxxx”,  
“ramdisk”: null,  
“region”: “eu-west-1”,  
“root\_device\_name”: “/dev/sda1”,  
“root\_device\_type”: “ebs”,  
“state”: “running”,  
“state\_code”: 16,  
“tags”: {},  
“tenancy”: “default”,  
“virtualization\_type”: “hvm”  
}  
],  
“invocation”: {  
“module\_args”: “image="ami-47a23a30" keypair=xxxxxxxx instance\_type=t2.micro instance\_tags="{‘Environment’: u’xxxxxxxx’, ‘Role’: u’server’, ‘Type’: ‘type’, ‘Name’: u’ec2-type-xxxxxxxx’, ‘Datacenter’: u’xxxxxxxx’}" vpc\_subnet\_id=subnet-xxxxxxxx region=eu-west-1 group\_id=sg-xxxxxxxx assign\_public\_ip=yes wait=true wait\_timeout=300”,  
“module\_name”: “ec2”  
},  
“item”: [  
{  
“cidr”: “xxxxxxxx”,  
“name”: “xxxxxxxx”,  
“region”: “eu-west-1”,  
“subnets\_app”: [  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1a”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1b”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1c”  
}  
],  
“subnets\_db”: [  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1a”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1b”  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1c”  
}  
]  
},  
{  
“subnet”: “subnet-xxxxxxxx”,  
“zone”: “eu-west-1c”  
}  
],  
“tagged\_instances”:   
},  
{  
“changed”: false,  
“skipped”: true  
},  
{  
“changed”: false,  
“skipped”: true  
}  
]  
}  
}  
}

So, any idea how to make this usable? Or maybe share your experience if you have already done it?

Thanks,  
Igor

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [June 5, 2015, 5:57am UTC](https://forum.ansible.com/t/launch-ec2-instances-in-multiple-availability-zones-in-single-play/18414/2 "2015-06-05T05:57:10Z")

</div>

wouldn’t ec2\_asg do that automatically for you ?

---

<div class="post-metadata">

**Author:** ![igorc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igorc/32/5491_2.png) [@igorc](https://forum.ansible.com/u/igorc)\
**Post date:** [June 5, 2015, 6:19am UTC](https://forum.ansible.com/t/launch-ec2-instances-in-multiple-availability-zones-in-single-play/18414/3 "2015-06-05T06:19:47Z")

</div>

Thanks Benno, that’s a good point, I usually associate ASG with launch config but for sure it is another way to do it.
