I'm running an Ansible play to provision an EC2 instance, but I am unable to extract the public_ip
parameter. Here's the play:
---
- name: Launch the new EC2 Instance
local_action:
module: ec2
group_id: "{{ webserver_firewall.group_id }}"
instance_type: "{{ instance_type}}"
image: "{{ ami_id }}"
region: "{{ aws_region }}"
count: "{{ count }}"
key_name: "{{ key_name }}"
wait: yes
volumes:
- device_name: /dev/sda1
volume_type: gp2
volume_size: 10
delete_on_termination: true
instance_tags:
Name: aws-webserver
register: ec2_webserver1
- debug: var="{{ item.public_ip }}"
with_items: "{{ ec2_webserver1.instances }}"
Here's part of the debug output:
ok: [localhost] => (item={u'kernel': None, u'root_device_type': u'ebs', <snip>, u'public_ip': u'1X.XX.XXX.X6', <snip>, u'root_device_name': u'/dev/sda1', u'hypervisor': u'xen'}) => {
"1X.XX.XXX.X6": "VARIABLE IS NOT DEFINED!",
"item": {
"ami_launch_index": "1",
"architecture": "x86_64",
<snip>
"public_ip": "1X.XX.XXX.X6",
"root_device_name": "/dev/sda1",
"root_device_type": "ebs",
"state": "running",
"hypervisor": "xen",
<snip>
}
}
If I try to provision one instance (count: 1
), then I can extract the IP using ec2_webserver1.instances[0]['public_ip']
- I'm not sure that this is the correct way to extract the IP. I'm guessing this may be the relevant part of the error:
"1X.XX.XXX.X6": "VARIABLE IS NOT DEFINED!",
but am not sure how to modify the play. What am I missing here?