ec2 dict processing / volume provisioning

Hi,

I am having issues with passing ec2 instance provisioning results to other tasks in a playbook.

I can launch instances, and then use "
register: ec2

" to register a dictionary of the results. But I cannot seem to extract the instance id later.

Using debug I can see i get valid results:

`
TASK [debug] *******************************************************************

ok: [localhost] => {

“ec2.results”: [

{

“_ansible_no_log”: false,

“changed”: true,

“instance_ids”: [

“i-5bbc7cee”

],

“instances”: [

{

“ami_launch_index”: “0”,

“architecture”: “x86_64”,

“block_device_mapping”: {

“/dev/xvda”: {

“delete_on_termination”: true,

“status”: “attached”,

“volume_id”: “vol-352da188”

}

},

“dns_name”: “”,

“ebs_optimized”: false,

“groups”: {

“sg-2b37d84f”: “mongo”

},

“hypervisor”: “xen”,

“id”: “i-5bbc7cee”,

“image_id”: “ami-d1f482b1”,

“instance_type”: “t2.micro”,

“kernel”: null,

“key_name”: “keypair”,

“launch_time”: “2016-03-11T13:33:09.000Z”,

“placement”: “us-west-1c”,

“private_dns_name”: “ip-172-x-x-x.us-west-1.compute.internal”,

“private_ip”: “x.x.x.x”,

“public_dns_name”: “”,

“public_ip”: null,

“ramdisk”: null,

“region”: “us-west-1”,

“root_device_name”: “/dev/xvda”,

“root_device_type”: “ebs”,

“state”: “running”,

“state_code”: 16,

“tags”: {

“Name”: “mongo”,
},

“tenancy”: “default”,

“virtualization_type”: “hvm”

}

],

“invocation”: {

“module_args”: {

“assign_public_ip”: false,

“aws_access_key”: “xxxxxxxxxxxxxxxxxxxxxx”,

“aws_secret_key”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,

“count”: 1,

“count_tag”: {

“volumes”: “data”

},

“ebs_optimized”: false,

“ec2_url”: null,

“exact_count”: null,

“group”: [

“mongo”

],

“group_id”: null,

“id”: null,

“image”: “ami-d1f482b1”,

“instance_ids”: null,

“instance_profile_name”: null,

“instance_tags”: {

“Name”: “mongo”,

},

“instance_type”: “t2.micro”,

“kernel”: null,

“key_name”: “keypair”,

“monitoring”: false,

“network_interfaces”: null,

“placement_group”: null,

“private_ip”: null,

“profile”: null,

“ramdisk”: null,

“region”: “us-west-1”,

“security_token”: null,

“source_dest_check”: true,

“spot_price”: null,

“spot_type”: “one-time”,

“spot_wait_timeout”: 600,

“state”: “present”,

“tenancy”: “default”,

“termination_protection”: false,

“user_data”: null,

“validate_certs”: true,

“volumes”: null,

“vpc_subnet_id”: “subnet-703aa229”,

“wait”: true,

“wait_timeout”: 300,

“zone”: null

},

“module_name”: “ec2”

},

“item”: “mongo”,

“tagged_instances”:

}

]

}

TASK [debug] *******************************************************************

ok: [localhost] => {

“ec2.results.instances”: “VARIABLE IS NOT DEFINED!”

}

TASK [debug] *******************************************************************

ok: [localhost] => {

“ec2.results.instance_ids”: “VARIABLE IS NOT DEFINED!”

}

TASK [debug] *******************************************************************

ok: [localhost] => {

“ec2.results.tagged_instances”: “VARIABLE IS NOT DEFINED!”

}

TASK [debug] *******************************************************************

ok: [localhost] => {

“ec2.results.0.instances.0.id”: “i-5bbc7cee”

}
`

The ec2 examples show using:

`

  • ec2_vol:
    instance: "{{ item.id }} "
    volume_size: 5
    with_items: ec2.instances
    register: ec2_vol
    `

But that does not work, based on the above debugs, I tried:
`

  • ec2_vol:

instance: “{{ item.id }}”

volume_size: 10

with_items: ec2.results.0.instances

register: ec2_vol
`

Well, after some more painful trial and error, and pissing Amazon off for sure, here’s a working solution:

I hope this saves someone some pain in the future.

`

  • name: data volumes
    ec2_vol:
    aws_access_key: “{{ key }}”
    aws_secret_key: “{{ secret }}”
    instance: “{{ item.instances.0.id }}”
    region: “{{item.instances.0.region}}”
    name: “{{ item.instances.0.tags.Name }}_data”
    volume_size: {{data_vol_size}}
    volume_type: gp2
    device_name: /dev/xvdb
    with_items: ec2.results
    register: ec2_vol

`