# Connecting to new EC2 instance using .pem file

**URL:** <https://forum.ansible.com/t/connecting-to-new-ec2-instance-using-pem-file/18536>\
**Category:** Ansible Project\
**Created:** [June 18, 2015, 8:08am UTC](https://forum.ansible.com/t/connecting-to-new-ec2-instance-using-pem-file/18536 "2015-06-18T08:08:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lokesh\_Vastrad](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@Lokesh\_Vastrad](https://forum.ansible.com/u/Lokesh_Vastrad)\
**Post date:** [June 18, 2015, 8:08am UTC](https://forum.ansible.com/t/connecting-to-new-ec2-instance-using-pem-file/18536/1 "2015-06-18T08:08:58Z")

</div>

Hi,

I am new to Ansible. I have setup Ansible using source from GIT. I am able to provision a new EC2 instance. I want to connect to this instance using a .pem file and not ssh.

I am able to manually connect to the new instance using the .pem file with the command “ssh -i abc.pem ec2-user@instance”. However, I am unable to connect using ansible parameter.

Command:

ansible-playbook abc.yml --private-key=/path/to/file.pem

Error: ip | FAILED! =\> SSH encountered an unknown error during the execution.

I also created an ansible.cfg file in the current directory and added the private key file path parameter. Nothing is working for me. Basically, none of the paramters are set from the ansible.cfg file.

Please let me know what I am doing wrong here.

Thanks,  
Lokesh

---

<div class="post-metadata">

**Author:** ![Andrew\_Burrow](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Andrew\_Burrow](https://forum.ansible.com/u/Andrew_Burrow)\
**Post date:** [June 19, 2015, 2:06am UTC](https://forum.ansible.com/t/connecting-to-new-ec2-instance-using-pem-file/18536/2 "2015-06-19T02:06:08Z")

</div>

Possibly start by running the trivial ping module in verbose mode:

---

<div class="post-metadata">

**Author:** ![Lokesh\_Vastrad](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@Lokesh\_Vastrad](https://forum.ansible.com/u/Lokesh_Vastrad)\
**Post date:** [June 23, 2015, 4:53am UTC](https://forum.ansible.com/t/connecting-to-new-ec2-instance-using-pem-file/18536/3 "2015-06-23T04:53:07Z")

</div>

Hi Andrew,

Thanks a lot for your reply. I was able to overcome the issue by creating a config file and adding few details under .ssh folder. I am now able to connect to the server.

Thanks,  
Lokesh

---

<div class="post-metadata">

**Author:** ![Alexey\_Shabalin](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Alexey\_Shabalin](https://forum.ansible.com/u/Alexey_Shabalin)\
**Post date:** [June 23, 2015, 3:28pm UTC](https://forum.ansible.com/t/connecting-to-new-ec2-instance-using-pem-file/18536/4 "2015-06-23T15:28:12Z")

</div>

Hi Lokesh,  
Could you please tell me in more details, what exactly you did to solve your issue?

---

<div class="post-metadata">

**Author:** ![hamzeh\_alsalhi](https://avatars.discourse-cdn.com/v4/letter/h/65b543/32.png) [@hamzeh\_alsalhi](https://forum.ansible.com/u/hamzeh_alsalhi)\
**Post date:** [June 23, 2015, 10:00pm UTC](https://forum.ansible.com/t/connecting-to-new-ec2-instance-using-pem-file/18536/5 "2015-06-23T22:00:31Z")

</div>

I don’t know what op did. I’ve solved this issue by doing:

`$exec ssh-agent bash`  
followed by:

`  
$ssh-add my\_ec2\_key.pem

`

Then I am able to run a playbook on the ec2 instance simply by listing the ip in the hosts file.
