0

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?

techraf
  • 64,883
  • 27
  • 193
  • 198
rahuL
  • 3,330
  • 11
  • 54
  • 79

1 Answers1

1

It is a correct way to refer to the value.


The problem is in the usage of the debug module. You should either use:

- debug: msg="{{ item.public_ip }}"

or:

- debug: var=item.public_ip

When you use a template in var argument it is resolved, so Ansible thinks you ask for the value of variable named 1X.XX.XXX.X6 (which, of course, does not exist).

techraf
  • 64,883
  • 27
  • 193
  • 198