# ec2 instance not reachable

**URL:** <https://forum.ansible.com/t/ec2-instance-not-reachable/33111>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [June 7, 2020, 4:14am UTC](https://forum.ansible.com/t/ec2-instance-not-reachable/33111 "2020-06-07T04:14:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanjay\_Chakraborty](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sanjay_chakraborty/32/10409_2.png) [@Sanjay\_Chakraborty](https://forum.ansible.com/u/Sanjay_Chakraborty)\
**Post date:** [June 7, 2020, 4:14am UTC](https://forum.ansible.com/t/ec2-instance-not-reachable/33111/1 "2020-06-07T04:14:34Z")

</div>

Hi guys,  
I need to set up some ec2 instances for my project and I am in the process of doing proof of concept.

My playbook creates an aws ec2 instance and I am using an already created security group that includes ssh however for some reason it is failing to open the port 22.  
launch\_east\_cost\_ec2.yml:

- name: Launch new EC2 instance  
hosts: localhost  
connection: local  
gather\_facts: false  
vars:  
pem: keyname  
reg: us-east-1

tasks:

- name: create ec2 instance with default values  
ec2:  
instance\_type: t2.micro  
image: ami-xxxxxxxxxxx  
region: “{{ reg }}”  
keypair: “{{ pem }}”  
aws\_access\_key: xx  
aws\_secret\_key: xxxxxxxxxxx
- name: add ssh to ec2  
ec2\_group:  
name: setup\_ssh\_connection  
description: a setup ssh connection for ec2  
vpc\_id: vpc-xxxx  
region: “{{ reg }}”  
rules:
- proto: tcp  
ports:
- 22  
cidr\_ip: [0.0.0.0/0](http://0.0.0.0/0)  
group\_id: xxxxx
- name: wait for rsystem to become reachable  
wait\_for\_connection:  
timeout: 3600 → i changed to few different values but no luck.

Output:

TASK [add ssh to ec2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[WARNING]: Ran out of time waiting for sg-xxxxx IpPermissions. Current: set([Rule(port\_range=(22, 22), protocol=u’tcp’, target=(‘2345423355’, ‘sg-xxxxxxxxxx’, None),  
target\_type=‘group’, description=None), Rule(port\_range=(22, 22), protocol=u’tcp’, target=‘[0.0.0.0/0](http://0.0.0.0/0)’, target\_type=‘ipv4’, description=None)]), Desired: [Rule(port\_range=(22, 22),  
protocol=u’tcp’, target=‘sg-xxxxxxxxxx’, target\_type=‘group’, description=None), Rule(port\_range=(22, 22), protocol=u’tcp’, target=‘[0.0.0.0/0](http://0.0.0.0/0)’, target\_type=‘ipv4’, description=None)]  
changed: [localhost]

TASK [wait fo rsystem to become reachable] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[WARNING]: Reset is not implemented for this connection  
ok: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Please advise what is missing.

Thank you in advance.  
Sanjay

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [June 7, 2020, 4:31am UTC](https://forum.ansible.com/t/ec2-instance-not-reachable/33111/2 "2020-06-07T04:31:33Z")

</div>

A security group has no effect until it is attached to an interface.

You will need to attach your new security group to the interface on the EC2 instance before you will be able to reach the instance on port 22.

Regards, K.

---

<div class="post-metadata">

**Author:** ![Sanjay\_Chakraborty](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sanjay_chakraborty/32/10409_2.png) [@Sanjay\_Chakraborty](https://forum.ansible.com/u/Sanjay_Chakraborty)\
**Post date:** [June 8, 2020, 2:18am UTC](https://forum.ansible.com/t/ec2-instance-not-reachable/33111/3 "2020-06-08T02:18:25Z")

</div>

Thank you Karl.  
Looks like interface instance attach failing. In order to get the instance id I am using ec2\_instance\_info and then use that to obtain the ec2 instance id to attach before ssh. But something wrong in syntax, any idea please ?

- name: get the instance info  
ec2\_instance\_info:  
filters:  
“tag:Name”: poc\_1  
register: ec2\_out
- name: Create an ENI and attach the interface  
ec2\_eni:  
subnet\_id: subnet-xxx  
#instance\_id: “{{ ec2\_out.instance.instance\_id }}”  
instance\_id: “{{ ec2\_out.[0] }}:{{instance.instance\_id }}”  
state: present  
attached: yes
- name: add ssh to ec2  
ec2\_group:  
name: setup\_ssh\_connection  
description: a setup ssh connection for ec2  
vpc\_id: vpc-xxx  
region: “{{ reg }}”  
rules:
- proto: tcp  
ports:
- 22  
cidr\_ip: [0.0.0.0/0](http://0.0.0.0/0)  
group\_id: sg-xxxxxx
- name: wait fo rsystem to become reachable  
wait\_for\_connection:  
timeout: 3600

TASK [Create an ENI and attach the interface] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/ansadmin/env\_aws/launch\_new\_ec2.yml:24  
fatal: [localhost]: FAILED! =\> {  
“msg”: “template error while templating string: expected name or number. String: {{ ec2\_out.[0] }}:{{instance.instance\_id }}”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=1 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0

Thanks.  
Sanjay
