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, 2019 in Ansible by Lakshminarayanan
• 1,370 points
1,143 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 launch an EC2 instance using Ansible Playbook?

Hi@akhtar, Ansible has one module named ec2. This ...READ MORE

answered Aug 12, 2020 in Ansible by MD
• 95,460 points
2,097 views
0 votes
1 answer

How to do SSH in EC2 instance using Ansible Playbook?

Hi@akhtar, To configure your inventory dynamically you need ...READ MORE

answered Sep 21, 2020 in Ansible by MD
• 95,460 points
2,449 views
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
7,112 views
0 votes
1 answer

Ansible playbook to automate security configurations of an EC2 instance

Hey @Kiara, your indentation is very wrong. ...READ MORE

answered Jan 24, 2019 in Ansible by Harsh
823 views
0 votes
1 answer

How do I handle different machines needing different user accounts or ports to log in with?

Setting inventory variables in the inventory file ...READ MORE

answered Jan 31, 2019 in Ansible by Ansi
869 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP