Creating multiple ec2 instances in different AZ's

Hi List,

I’m trying to create 1 (or more) instances inside different subnet’s where each subnet is a different AZ in Amazon. That way of using AZ’s makes for some better fail-safes.

So:

---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    #your region
    region: eu-west-1
    AWS_ACCESS_KEY_ID: snipped
    AWS_SECRET_ACCESS_KEY: snipped
    ceph_nodes:
      1a:
        subnet: subnet-snippeda
        count: 1
      1b:
        subnet: subnet-snippedb
        count: 1
      1c:
        subnet: subnet-snippedc
        count: 1
  tasks:
    - name: Maak 1 Ceph host in elke gekozen AZ
      local_action:
        module: ec2
        aws_access_key: "{{ AWS_ACCESS_KEY_ID }}"
        aws_secret_key: "{{ AWS_SECRET_ACCESS_KEY }}"
        region: "{{ region }}"
        vpc_subnet_id: "{{ item.value.subnet }}"
        key_name: snipped
        instance_type: t2.micro
        image: ami-30ff5c47
        wait: yes
        group: 'backend ceph'
        volumes:
          - device_name: /dev/sdb
            device_type: standard
            volume_size: 128
            delete_on_termination: false
        instance_tags:
          Name: ci.ceph.{{ item.key }}
          group: ceph{{ item.key }}
          environment: development
        exact_count: "{{ item.value.count }}"
        count_tag:
          group: ceph{{ item.key }}
      register: ec2
      with_dict: ceph_nodes

    - debug: var=ec2

when that runs, the variable “ec2” contains:

ok: [localhost] => {
    "ec2": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "changed": true, 
                "instance_ids": [
                    "i-snipped"
                ], 
                "instances": [
                    {
                        "ami_launch_index": "0", 
                        "architecture": "x86_64", 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "hypervisor": "xen", 
                        "id": "i-snipped", 
                        "image_id": "ami-30ff5c47", 
                        "instance_type": "t2.micro", 
                        "kernel": null, 
                        "key_name": "snipped", 
                        "launch_time": "2014-12-30T09:45:55.000Z", 
                        "placement": "eu-west-1a", 
                        "private_dns_name": "ip-10-220-225-9.eu-west-1.compute.internal", 
                        "private_ip": "10.220.225.9", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "eu-west-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "virtualization_type": "hvm"
                    }
                ], 
                "invocation": {
                    "module_args": "", 
                    "module_name": "ec2"
                }, 
                "item": {
                    "key": "1a", 
                    "value": {
                        "count": 1, 
                        "subnet": "subnet-snipped"
                    }
                }, 
                "tagged_instances": [
                    {
                        "ami_launch_index": "0", 
                        "architecture": "x86_64", 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "hypervisor": "xen", 
                        "id": "i-snipped", 
                        "image_id": "ami-30ff5c47", 
                        "instance_type": "t2.micro", 
                        "kernel": null, 
                        "key_name": "snipped", 
                        "launch_time": "2014-12-30T09:45:55.000Z", 
                        "placement": "eu-west-1a", 
                        "private_dns_name": "ip-10-220-225-9.eu-west-1.compute.internal", 
                        "private_ip": "10.220.225.9", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "eu-west-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "virtualization_type": "hvm"
                    }
                ]
            }, 
            {
                "changed": true, 
                "instance_ids": [
                    "i-snipped"
                ], 
                "instances": [
                    {
                        "ami_launch_index": "0", 
                        "architecture": "x86_64", 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "hypervisor": "xen", 
                        "id": "i-snipped", 
                        "image_id": "ami-30ff5c47", 
                        "instance_type": "t2.micro", 
                        "kernel": null, 
                        "key_name": "snipped", 
                        "launch_time": "2014-12-30T09:46:17.000Z", 
                        "placement": "eu-west-1c", 
                        "private_dns_name": "ip-10-220-227-66.eu-west-1.compute.internal", 
                        "private_ip": "10.220.227.66", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "eu-west-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "virtualization_type": "hvm"
                    }
                ], 
                "invocation": {
                    "module_args": "", 
                    "module_name": "ec2"
                }, 
                "item": {
                    "key": "1c", 
                    "value": {
                        "count": 1, 
                        "subnet": "subnet-snipped"
                    }
                }, 
                "tagged_instances": [
                    {
                        "ami_launch_index": "0", 
                        "architecture": "x86_64", 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "hypervisor": "xen", 
                        "id": "i-snipped", 
                        "image_id": "ami-30ff5c47", 
                        "instance_type": "t2.micro", 
                        "kernel": null, 
                        "key_name": "snipped", 
                        "launch_time": "2014-12-30T09:46:17.000Z", 
                        "placement": "eu-west-1c", 
                        "private_dns_name": "ip-10-220-227-66.eu-west-1.compute.internal", 
                        "private_ip": "10.220.227.66", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "eu-west-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "virtualization_type": "hvm"
                    }
                ]
            }, 
            {
                "changed": true, 
                "instance_ids": [
                    "i-snipped"
                ], 
                "instances": [
                    {
                        "ami_launch_index": "0", 
                        "architecture": "x86_64", 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "hypervisor": "xen", 
                        "id": "i-snipped", 
                        "image_id": "ami-30ff5c47", 
                        "instance_type": "t2.micro", 
                        "kernel": null, 
                        "key_name": "snipped", 
                        "launch_time": "2014-12-30T09:46:45.000Z", 
                        "placement": "eu-west-1b", 
                        "private_dns_name": "ip-10-220-226-191.eu-west-1.compute.internal", 
                        "private_ip": "10.220.226.191", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "eu-west-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "virtualization_type": "hvm"
                    }
                ], 
                "invocation": {
                    "module_args": "", 
                    "module_name": "ec2"
                }, 
                "item": {
                    "key": "1b", 
                    "value": {
                        "count": 1, 
                        "subnet": "subnet-snipped"
                    }
                }, 
                "tagged_instances": [
                    {
                        "ami_launch_index": "0", 
                        "architecture": "x86_64", 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "hypervisor": "xen", 
                        "id": "i-snipped", 
                        "image_id": "ami-30ff5c47", 
                        "instance_type": "t2.micro", 
                        "kernel": null, 
                        "key_name": "snipped", 
                        "launch_time": "2014-12-30T09:46:45.000Z", 
                        "placement": "eu-west-1b", 
                        "private_dns_name": "ip-10-220-226-191.eu-west-1.compute.internal", 
                        "private_ip": "10.220.226.191", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "eu-west-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "virtualization_type": "hvm"
                    }
                ]
            }
        ]
    }
}

when I then try to add these new hosts to a group:

    - name: Add new instances to group
      add_host: hostname={{ item.instances.value.private_ip }} groupname=ceph_launched
      with_items: ec2.results

I’m getting the error:

TASK: [Add new instances to group] ******************************************** 
fatal: [localhost] => One or more undefined variables: 'list object' has no attribute 'value'

FATAL: all hosts have already failed -- aborting

I’ve tried a few different ways of referencing the private_ip, but I’m not getting the syntax right.

Can anyone point me in the right direction here?

Thanks!
Mark

Unless I missed something, it looks like your expanded add_host is trying to reference:
ec2.results[0].instances.value.private_ip

which does not exist. Probable correct value seems (to me) to be:
ec2.results[0].instances.private_ip

If you are indeed trying to get to something where ‘value’ is a valid key, then you might be looking for:
ec2.results[0].instances.item.value.

Though that particular section doesn’t contain private_ip, so I suspect that’s not what you want. :slight_smile:

I think this would work better in your playbook:
`

  • name: Add new instances to group
    add_host: hostname={{ item.instances.private_ip }} groupname=ceph_launched
    with_items: ec2.results

`

(Hopefully this comes in after my first reply, since they have to be approved, the order might be weird!)

I messed up in my earlier response, I forgot that ec2.results.instances is an array. That’s going to be trickier than I thought, since you have an array of arrays you need to reference.
In the exact example you gave, where there’s only once instance per subnet, you could cheat and do something like:

hostname={{ item.instances[0].private_ip }}

But that’s not very helpful if you spin up more than one instance per subnet.

I’d be curious to see if this syntax works in Ansible (python slices)… can’t think of a quick and easy way to test it, or I’d give a more confident suggestion of it, but worth a shot:
… hostname={{ item.instances[:].private_ip }} …

with_items: ec2.results

Try

  • name: Add new instances to group
    add_host: hostname={{ item.private_ip }} groupname=ceph_launched
    with_items: ec2.instances

I tried that one, but the ec2 variable has received multiple “instances” blocks, so the default way of referencing those no longer seems to work.