Launch ec2 instances in multiple availability zones in single play

753 views
Skip to first unread message

Igor Cicimov

unread,
Jun 5, 2015, 1:05:31 AM6/5/15
to ansible...@googlegroups.com
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",
                            "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",
                            "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",
                            "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",
                            "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",
                            "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",
                            "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

benno joy

unread,
Jun 5, 2015, 1:57:14 AM6/5/15
to ansible...@googlegroups.com
wouldn't ec2_asg do that automatically for you ?



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b27240c8-42c5-44ec-8549-d99990fd2d1f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Igor Cicimov

unread,
Jun 5, 2015, 2:19:48 AM6/5/15
to ansible...@googlegroups.com
Thanks Benno, that's a good point, I usually associate ASG with launch config but for sure it is another way to do it.
Reply all
Reply to author
Forward
0 new messages