I have tried to launch EC2 instance in AWS then allocating elastic IP for the instance. But it thrown error. I have posted both playbook and error code. Please fix the issue.
below is my playbook:
---
- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    region: us-east-1
  tasks:
    - name: To Launch EC2 Instance and Allocate EIP then Assigned to Instance
      ec2:
        region: "{{ region }}"
        key_name: sampleprivatekey
        instance_type: t2.micro
        image: ami-0b69ea66ff7391e80
        wait: yes
        group: webserver
      register: ec2
    - name: Associate new EIP for the Instance
      ec2_eip:
        in_vpc: yes
        reuse_existing_ip_allowed: yes
        state: present
        region: "{{ region }}"
        instance_id: "{{ ec2.instance_id[0] }}"
      register: instance_eip
    - debug: var=instance_eip.public_ip
    - name: Wait for SSH to Start
      wait_for:
        host: "{{ ec2.private_ip }}"
        port: 22
        timeout: 300
      delegate_to: "127.0.0.1"
below is error code:
TASK [Associate new EIP for the Instance] *****************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'instance_id'\n\nThe error appears to be in '/etc/ansible/launch-ec2witheip-playbook.yml': line 17, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      register: ec2\n    - name: Associate new EIP for the Instance\n      ^ here\n"}