aws ec2 module getting error - One or more undefined variables: 'instances' is undefined

Hello folks,

New to ansible, using version - 1.7.2. I am trying to write a play to launch an instance on AWS and install my packages on it. i need to automatically add the DNS name of the launched instance to my hosts file. But i am getting the error - One or more undefined variables: ‘instances’ is undefined

I am following the example here - http://docs.ansible.com/guide_aws.html

Below link to my playbook.
https://github.com/raj-andy1/macguffin/blob/master/aws-plays/aws-ec2.yaml

Please help

Below is the output i am getting

PLAY [localhost] **************************************************************

TASK: [make one instance] *****************************************************
changed: [localhost]

TASK: [debug var=ec2_info] ****************************************************
ok: [localhost] => {
“ec2_info”: {
“changed”: true,
“instance_ids”: [
“i-7e343873”
],
“instances”: [
{
“ami_launch_index”: “0”,
“architecture”: “x86_64”,
“dns_name”: “ec2-54-69-18-39.us-west-2.compute.amazonaws.com”,
“ebs_optimized”: false,
“hypervisor”: “xen”,
“id”: “i-7e343873”,
“image_id”: “ami-b6bdde86”,
“instance_type”: “t1.micro”,
“kernel”: “aki-fc37bacc”,
“key_name”: null,
“launch_time”: “2014-09-30T04:37:29.000Z”,
“placement”: “us-west-2b”,
“private_dns_name”: “ip-172-31-28-117.us-west-2.compute.internal”,
“private_ip”: “172.31.28.117”,
“public_dns_name”: “ec2-54-69-18-39.us-west-2.compute.amazonaws.com”,
“public_ip”: “54.69.18.39”,
“ramdisk”: null,
“region”: “us-west-2”,
“root_device_name”: “/dev/sda1”,
“root_device_type”: “ebs”,
“state”: “running”,
“state_code”: 16,
“virtualization_type”: “paravirtual”
}
],
“invocation”: {
“module_args”: “image=ami-b6bdde86 instance_type=t1.micro region=us-west-2 wait=true”,
“module_name”: “ec2”
},
“tagged_instances”:
}
}

TASK: [debug msg=“Assigned public DNS name is {{ instances.public_dns_name }}”] ***
fatal: [localhost] => One or more undefined variables: ‘instances’ is undefined

FATAL: all hosts have already failed – aborting

PLAY RECAP ********************************************************************
to retry, use: --limit @/Users/andyr/aws-ec2.yaml.retry

localhost : ok=2 changed=1 unreachable=1 failed=0

Reading your playbook above, there’s no point at which you register into a variable called “instances”, which is the problem.

Move your debug line up and see what is in it:

Hi Mike

Thanks for your response. I tried to follow the example on Ansible’s website(http://docs.ansible.com/guide_aws.html) but still no success. Not sure what is wrong
Can you please help?

Playbook location - https://github.com/raj-andy1/macguffin/blob/master/aws-plays/aws-ec2.yaml

Thanks
Andy

PLAY [localhost] **************************************************************

TASK: [make one instance] *****************************************************
changed: [localhost]

TASK: [debug var=ec2_info] ****************************************************
ok: [localhost] => {
“ec2_info”: {
“changed”: true,
“instance_ids”: [
“i-25918128”
],
“instances”: [
{
“ami_launch_index”: “0”,
“architecture”: “x86_64”,
“dns_name”: “ec2-54-69-33-81.us-west-2.compute.amazonaws.com”,
“ebs_optimized”: false,
“hypervisor”: “xen”,
“id”: “i-25918128”,
“image_id”: “ami-b6bdde86”,
“instance_type”: “t1.micro”,
“kernel”: “aki-fc37bacc”,
“key_name”: null,
“launch_time”: “2014-10-07T22:57:06.000Z”,
“placement”: “us-west-2b”,
“private_dns_name”: “ip-172-31-29-210.us-west-2.compute.internal”,
“private_ip”: “172.31.29.210”,
“public_dns_name”: “ec2-54-69-33-81.us-west-2.compute.amazonaws.com”,
“public_ip”: “54.69.33.81”,
“ramdisk”: null,
“region”: “us-west-2”,
“root_device_name”: “/dev/sda1”,
“root_device_type”: “ebs”,
“state”: “running”,
“state_code”: 16,
“virtualization_type”: “paravirtual”
}
],
“invocation”: {
“module_args”: “image=ami-b6bdde86 instance_type=t1.micro region=us-west-2 wait=true”,
“module_name”: “ec2”
},
“tagged_instances”:
}
}

TASK: [debug var=item with_items:ec2_info.instance_ids] ***********************
ok: [localhost] => (item={{ item }}) => {
“item”: “{{ item }}”
}

TASK: [debug msg=“Assigned public DNS name is {{ item.dns_name }}” with_items:ec2_info.instances] ***
fatal: [localhost] => One or more undefined variables: ‘item’ is undefined

FATAL: all hosts have already failed – aborting

PLAY RECAP ********************************************************************
to retry, use: --limit @/Users/andyr/aws-ec2.yaml.retry

localhost : ok=3 changed=1 unreachable=1 failed=0