# Ansible fails to connect to newly provisioned EC2 on 3rd task after successful running first 2 tasks

**URL:** <https://forum.ansible.com/t/ansible-fails-to-connect-to-newly-provisioned-ec2-on-3rd-task-after-successful-running-first-2-tasks/26009>\
**Category:** Ansible Project\
**Tags:** ubuntu, aws\
**Created:** [November 13, 2017, 6:16pm UTC](https://forum.ansible.com/t/ansible-fails-to-connect-to-newly-provisioned-ec2-on-3rd-task-after-successful-running-first-2-tasks/26009 "2017-11-13T18:16:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddrake2012](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@ddrake2012](https://forum.ansible.com/u/ddrake2012)\
**Post date:** [November 13, 2017, 6:16pm UTC](https://forum.ansible.com/t/ansible-fails-to-connect-to-newly-provisioned-ec2-on-3rd-task-after-successful-running-first-2-tasks/26009/1 "2017-11-13T18:16:24Z")

</div>

I have a playbook that creates EC2 instances and adds them to an in memory group using the add\_host module. I am then able to connect to the in memory group and perform two successful commands before a third fails.

I am seeing this problem just running the same file module to create directories. I have something like this in my main playbook (ec2hosts is the in-memory group creating after provisioning)

- hosts: ec2hosts  
user: ubuntu  
gather\_facts: false  
name: try the setup  
tasks:

- name: Get EC2 facts  
ec2\_metadata\_facts:  
register: ec2\_facts

- name: import configure role  
import\_role:  
name: configure  
vars:  
efs\_ids: “{{ efs\_id }}”

The configure role is very simple:

- name: Make the aws credentials directory  
file:  
state: directory  
path: ~/.aws
- name: Make the hi directory  
file:  
state: directory  
path: ~/.hi
- name: Make a temp directory  
file:  
state: directory  
path: ~/.temp
- name: Make a bar directory  
file:  
state: directory  
path: ~/.bar

And this fails at the Make a temp directory task. The failed output with -vvv looks like:

\<35.160.185.188\> (0, ‘’, “Warning: Permanently added ‘35.160.185.188’ (ECDSA) to the list of known hosts.\r\n”) \<35.160.185.188\> ESTABLISH SSH CONNECTION FOR USER: ubuntu \<35.160.185.188\> SSH: EXEC ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i AnsibleTest.pem -o ‘IdentityFile=“[omitted\_full\_path]/AnsibleTest.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -tt 35.160.185.188 ‘/bin/sh -c ‘"’"’/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-tmp-1510596698.75-58373657425242/file.py; rm -rf “/home/ubuntu/.ansible/tmp/ansible-tmp-1510596698.75-58373657425242/” \> /dev/null 2\>&1 && sleep 0’“'”‘’ \<35.160.185.188\> (255, ‘’, ‘ssh\_exchange\_identification: read: Connection reset by peer\r\n’) fatal: [35.160.185.188]: UNREACHABLE! =\> { “changed”: false, “msg”: “Failed to connect to the host via ssh: ssh\_exchange\_identification: read: Connection reset by peer\r\n”, “unreachable”: true }

I am using the following ssh\_args in my ansible.cfg for the playbook:

ssh\_args = -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i “AnsibleTest.pem”

Does anyone know what’s happening here? This seems pretty weird and I’m stuck.  
Thanks!

---

<div class="post-metadata">

**Author:** ![ddrake2012](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@ddrake2012](https://forum.ansible.com/u/ddrake2012)\
**Post date:** [November 16, 2017, 4:35pm UTC](https://forum.ansible.com/t/ansible-fails-to-connect-to-newly-provisioned-ec2-on-3rd-task-after-successful-running-first-2-tasks/26009/2 "2017-11-16T16:35:18Z")

</div>

I eventually fixed this by adding a retries parameter to the [ssh\_connection] section of my ansible.cfg so it looks like the below.

[ssh\_connection]

ssh\_args = -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no  
retries = 10

Pretty lame! If anyone finds a better solution please let me know…

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 16, 2017, 4:37pm UTC](https://forum.ansible.com/t/ansible-fails-to-connect-to-newly-provisioned-ec2-on-3rd-task-after-successful-running-first-2-tasks/26009/3 "2017-11-16T16:37:24Z")

</div>

Perhaps the instance was not actually up and accessible yet. Did you use `wait_for` or `wait_for_connection` after creating the instance to wait and ensure they are up and accessible before moving on?

---

<div class="post-metadata">

**Author:** ![ddrake2012](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@ddrake2012](https://forum.ansible.com/u/ddrake2012)\
**Post date:** [December 8, 2017, 7:42pm UTC](https://forum.ansible.com/t/ansible-fails-to-connect-to-newly-provisioned-ec2-on-3rd-task-after-successful-running-first-2-tasks/26009/4 "2017-12-08T19:42:48Z")

</div>

Yes, I use the wait parameter in the ec2 module. And the weirdest thing is that two of the tasks work before the third fails, so the connection is up and working and then just stops working. I’ve seen this when uploading files as well with messages like “the sftp file transfer mechansi failed”, but the retries work and come to the rescue.

From where I sit, I pretty much don’t see a way to do any of this EC2 stuff without the retries and am surprised no one else has seen this.
