# I am not being able to ssh into EC2 after creation in ansible

**URL:** <https://forum.ansible.com/t/i-am-not-being-able-to-ssh-into-ec2-after-creation-in-ansible/23035>\
**Category:** Ansible Project\
**Created:** [November 15, 2016, 7:27pm UTC](https://forum.ansible.com/t/i-am-not-being-able-to-ssh-into-ec2-after-creation-in-ansible/23035 "2016-11-15T19:27:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![suyesh\_bhandari](https://avatars.discourse-cdn.com/v4/letter/s/e9a140/32.png) [@suyesh\_bhandari](https://forum.ansible.com/u/suyesh_bhandari)\
**Post date:** [November 15, 2016, 7:27pm UTC](https://forum.ansible.com/t/i-am-not-being-able-to-ssh-into-ec2-after-creation-in-ansible/23035/1 "2016-11-15T19:27:49Z")

</div>

I am new to ansible and trying to create EC2 and i have several other tasks to perform. here is the code,

`

- name: Create a Dataloading environment  
hosts: localhost  
gather\_facts: False

vars:  
key\_name: sims-admin  
instance\_type: t2.micro  
image: ami-e86f35ff  
region: us-east-1

tasks:

- name: Provision EC2 instances  
ec2:  
key\_name: “{{ key\_name }}”  
instance\_type: “{{ instance\_type }}”  
image: “{{ image }}”  
wait: true  
exact\_count: 1  
region: “{{ region }}”  
count\_tag:  
Name: CounttagANSIBLEDataloading  
instance\_tags:  
Name: ANSIBLEDataloading  
register: ec2

- name: Add new instance to host group  
add\_host: hostname={{ item.public\_ip }} groupname=amazon\_ec2  
with\_items: ‘{{ec2.instances}}’

- name: Wait for the instances to boot by checking the ssh port  
wait\_for: host={{item.public\_dns\_name}} port=22 delay=60 timeout=320 state=started  
with\_items: ‘{{ec2.instances}}’

- name: Configure instance  
hosts: amazon\_ec2  
become: True  
gather\_facts: True

tasks:

- name: Get the latest updates for instance  
apt:  
update\_cache: yes  
upgrade: dist

`

It works until breathing room but fails in configure instance. I get the error saying

`

```auto
fatal: [54.145.185.81]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}

```

`

At first i was struggling to even ssh to come up to work. I finally got it work somehow but now the configure instance is not working. i am using ansible 2 on mac.

I am not sure why Configure instance fails? how am i supposed to login to ec2 in ansible? i have the key.pem in ~/.ssh/key.pem

Please help me with explanation and maybe links to relevant links. Alsthough i have been to several links and since my basic understanding on ansible is low i dont quite get it.
