# Connecting to existing ec2 instances

**URL:** <https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752>\
**Category:** Ansible Project\
**Created:** [November 12, 2015, 9:37pm UTC](https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752 "2015-11-12T21:37:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathan\_chu](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@nathan\_chu](https://forum.ansible.com/u/nathan_chu)\
**Post date:** [November 12, 2015, 9:37pm UTC](https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752/1 "2015-11-12T21:37:05Z")

</div>

Hi,

I recently inherited an Ansible deployment that manages some EC2 instances and I have a couple questions. I am unable to find a pem file/ssh key on the local file system, but somehow Ansible is able to connect to the instances it creates and run shell scripts. How is this possible? I’m not trying to enhance the existing playbooks to update some data via a script on a central/master server in our deployment every time we create a new instance, but I’m having trouble connecting to the instance. How can I use whatever mechanism is in place for the recently provisioned instances to access this single (relatively static) instance?

Thanks,

Nate

---

<div class="post-metadata">

**Author:** ![nathan\_chu](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@nathan\_chu](https://forum.ansible.com/u/nathan_chu)\
**Post date:** [November 12, 2015, 10:10pm UTC](https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752/2 "2015-11-12T22:10:18Z")

</div>

\*typo: not → now. there’s a nice one-letter semantic change, lol.

---

<div class="post-metadata">

**Author:** ![nathan\_chu](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@nathan\_chu](https://forum.ansible.com/u/nathan_chu)\
**Post date:** [November 12, 2015, 10:17pm UTC](https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752/3 "2015-11-12T22:17:34Z")

</div>

My existing instances are provisioned thusly:

tasks:

- name: Provision an instance  
ec2: \>  
aws\_access\_key={{ec2\_access\_key}}  
aws\_secret\_key={{ec2\_secret\_key}}  
keypair={{mykeypair}}  
group\_id={{security\_group}}  
instance\_type={{instance\_type}}  
image={{image}}  
region={{region}}  
wait=true  
count=1  
vpc\_subnet\_id={{ subnet\_name }}  
instance\_tags=‘{“Name”:“{{ name }}”,“InternalName”:“{{ internal\_name }}”}’  
register: ec2\_info

- debug: var=ec2\_info

- debug: var=item  
with\_items: ec2\_info.instance\_ids

- debug: var=item  
with\_items: ec2\_info.instances

- name: add host to host list  
add\_host: hostname={{ item.public\_ip }} groupname=ec2hosts  
with\_items: ec2\_info.instances

- name: wait for instances to listen on port:22  
wait\_for:  
state=started  
host={{ item.private\_dns\_name }}  
port=22  
with\_items: ec2\_info.instances

- hosts: ec2hosts  
gather\_facts: True  
user: ec2-user  
sudo: True  
roles:

- { role: common, XXXextra instance variablesXXX } \<— this role (in main.yml) calls a number of scripts and shell commands on the instance. How can it connect? and how can I replicate that connection?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 12, 2015, 10:30pm UTC](https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752/4 "2015-11-12T22:30:44Z")

</div>

run ansible with -vvvvv and you should see the full ssh command used,  
that should point at a key, if not you likely have an agent or the ssh  
user/password info in inventory.

---

<div class="post-metadata">

**Author:** ![nathan\_chu](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@nathan\_chu](https://forum.ansible.com/u/nathan_chu)\
**Post date:** [November 13, 2015, 1:29pm UTC](https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752/5 "2015-11-13T13:29:59Z")

</div>

Thanks Brian. That’s some helpful debugging advice.

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [November 13, 2015, 1:46pm UTC](https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752/6 "2015-11-13T13:46:39Z")

</div>

I would grep -r your ansible directory for mykeypair. Since it is a variable specified in your playbook, either you need to define that on the command line with extra-vars, or the name of the keypair is written as a variable in a file somewhere (vars file, hosts file,etc).

grep -r “mykeypair” /etc/ansible

Joanna

---

<div class="post-metadata">

**Author:** ![nathan\_chu](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@nathan\_chu](https://forum.ansible.com/u/nathan_chu)\
**Post date:** [November 13, 2015, 2:36pm UTC](https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752/7 "2015-11-13T14:36:42Z")

</div>

Joanna - thanks; I don’t see any references to that variable in /etc/ansible, and the only places I see it in my playbook directory are references to the amazon key name, no files.

I think the problem may be orthogonal to what I was thinking of yesterday, though. I tried running my existing Ansible script with the debug output, as suggested earlier - this also failed with an SSH key problem. On further investigation, I realized that Ansible Tower provides a credentials repository, so, It might ‘just work’ through the Tower interface. I was hoping to debug my script through the terminal, but since everything else we do runs through Tower, I’ll try testing through there.

Thanks for the help!

Nate

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 13, 2015, 6:27pm UTC](https://forum.ansible.com/t/connecting-to-existing-ec2-instances/19752/8 "2015-11-13T18:27:25Z")

</div>

ah, that is totally different question, tower keeps it's credentials  
in it's own store. Also for tower questions go to support.ansible.com  
support@ansible.com
