details of instance value

Hi,
Could you please help … am unable to retrieve the public ip address of the ec2 instance to add it to the host inventory…

  • name: Create an ec2 instance
    ec2:
    key_name: “{{ keypair }}”
    group: sg_webserver_group
    instance_type: “{{ instance_type}}”
    image: “{{ ami }}”
    wait: true
    region: “{{ region }}”
    assign_public_ip: yes
    register: ec2

  • debug: var=ec2.instances.public_ip (or var=ec2.public_ip doesnt give the public ip rather gives undefined variable but whereas var=ec2 gives all the details)

  • name: Add the newly created host
    add_host:
    name: “{{ item.public_ip }}”
    groups: webserver
    with_items: “{{ ec2.instances }}”