How to launch ec2 instance with allocating EIP?

0 votes
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"}
Sep 10 in Ansible by Latchu
• 270 points
29 views
Is your instance_id defined correctly?

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Ansible

0 votes
1 answer

How to use Ansible git module pull a branch with local changes?

You cannot achieve it using the git ...READ MORE

answered Jul 9, 2018 in Ansible by Atul
• 10,240 points
1,429 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Playbook to add ec2 instance to ELB

Hey, @Kishore, try something like this: - hosts: ...READ MORE

answered Mar 25 in Ansible by Yesha
20 views
0 votes
0 answers

Error while executing playbook to start ec2 instance

I have a playbook that starts an ...READ MORE

Mar 29 in Ansible by Kalgi
140 views
0 votes
1 answer
0 votes
1 answer