# Unable to reach to EC2 instance using dynamic inventory with a single region

**URL:** <https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [September 16, 2016, 7:15am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566 "2016-09-16T07:15:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nirav\_Radia](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nirav\_Radia](https://forum.ansible.com/u/Nirav_Radia)\
**Post date:** [September 16, 2016, 7:15am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/1 "2016-09-16T07:15:39Z")

</div>

Hi all,

I am pretty new to Ansible. I am using ec2.py to connect to my EC2 instances and run my ansible scripts on them. Previously, it was working fine when I was using “all” regions. But now when I change the region to a specific one region (any of the available ones), in ec2.ini, it gives me below error for the first time and after second time onward, it works well.

`

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

```

`

What I do is use ec2 module to create a instance and then wait\_for port 22 on each host to be up. It wait successfully and moves forward but in the immediate next step when I try to connect to the instance, it gives above error first time. Here is my wait\_for task: (ec2\_server is the variable registered from ec2 module)

`

name: wait for ssh server to be running  
wait\_for: host={{ item.public\_dns\_name }} port=22  
with\_items: “{{ec2\_server.instances | default()}}”  
when: item.state == ‘running’

`

I ping in between to the instance using private ip and the ping succeeds. I suspect it is some timing issue or DNS name resolution issue with AWS. Has anyone faced such problem before?

Any help would be appreciated. Thanks !

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [September 17, 2016, 7:00am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/2 "2016-09-17T07:00:18Z")

</div>

Would you mind to post your ec2.ini with the credentials taken out? Also, what is the output of ec2.py when you run it alone?

---

<div class="post-metadata">

**Author:** ![Nirav\_Radia](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nirav\_Radia](https://forum.ansible.com/u/Nirav_Radia)\
**Post date:** [September 21, 2016, 8:05am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/3 "2016-09-21T08:05:51Z")

</div>

Thanks @Alexander for your reply. Here is **ec2.ini**

`

# Ansible EC2 external inventory script settings

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [September 22, 2016, 2:31am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/4 "2016-09-22T02:31:05Z")

</div>

Hi Nirav,

I have a vague recollection of at some point having had a similar issue with dynamic inventory that was solved by placing the availability zone in the region field instead of the region. My memory is far from a reliable source of information so if you decide to give that a shot don’t be surprised if it doesn’t work.

With that said, I just re-read your original post and your statement that this is only a problem on the first run stuck out because the script uses caching to avoid hitting the AWS api all the time. The related docs are here:

[http://docs.ansible.com/ansible/intro\_dynamic\_inventory.html#example-aws-ec2-external-inventory-script](http://docs.ansible.com/ansible/intro_dynamic_inventory.html#example-aws-ec2-external-inventory-script)

Though I’m sure you’ve read through those several times already, the bit I believe affects your specific situation is right at the bottom of that section:

> Note that the AWS inventory script will cache results to avoid repeated API calls, and this cache setting is configurable in ec2.ini. To explicitly clear the cache, you can run the ec2.py script with the `--refresh-cache` parameter:
> 
> ```auto
> # ./ec2.py --refresh-cache
> 
> ```

Have you tried adding the `--refresh-cache` option to your first run of the ec2.py script? If that works, then bully! but if it doesn’t, would you mind posting the related results from the first run of your playbook with the error and then also the related portions of the second one where it works?

Thanks,  
Alex

---

<div class="post-metadata">

**Author:** ![Nirav\_Radia](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nirav\_Radia](https://forum.ansible.com/u/Nirav_Radia)\
**Post date:** [September 22, 2016, 7:51am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/5 "2016-09-22T07:51:18Z")

</div>

Hi Alex,

1. I am not sure I understood “placing the availability zone in the region field instead of the region” correctly. Because when I exported “us-west-2a” as AWS\_DEFAULT\_REGION (which my ec2.py is using to filter region), it gave me error like this:

`

ERROR! The file inventory/ec2.py is marked as executable, but failed to execute correctly. If this is not supposed to be an executable script, correct this with `chmod -x inventory/ec2.py`.  
Inventory script (inventory/ec2.py) had an execution error: region name: us-west-2a likely not supported, or AWS is down. connection to region failed.  
inventory/ec2.py:3: Error parsing host definition ‘’‘’': No closing quotation

`

Let me know where can I place AZ instead of region.

1. Yes, I tried executing  
`./inventory/ec2.py --refresh-cache`  
between instance creation playbook and ping command, but with no luck ! And IMHO it doesn’t matter to refresh cache as I have “_cache\_max\_age = 0_” in ec2.ini. (Correct me if I am wrong.)

For the first time when I ping **doesn’t succeed** , verbose and console output is:

[Private\_IP1 and Private\_IP2 are the instances already running and matching the filter in ec2.py, **Private\_IP3** is the IP of the instance just launched and waited for port 22 successfully]

`

Using /etc/ansible/ansible.cfg as config file  
Loaded callback minimal of type stdout, v2.0  
\<Private\_IP1\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP3\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP3\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP3\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP1\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP1\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP1\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP3\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP3\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP3\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP1\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP1\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP1\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP3\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP3\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP3\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP1\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP1\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP1\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP3\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP3\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r Private\_IP3 ‘/bin/sh -c ‘"’"’( umask 77 && mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1474528176.32-147953530428586`” && echo ansible-tmp-1474528176.32-147953530428586=“`echo $HOME/.ansible/tmp/ansible-tmp-1474528176.32-147953530428586`” ) && sleep 0’“'”‘’  
\<Private\_IP1\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r Private\_IP1 ‘/bin/sh -c ‘"’"’( umask 77 && mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1474528176.32-242806970585719`” && echo ansible-tmp-1474528176.32-242806970585719=“`echo $HOME/.ansible/tmp/ansible-tmp-1474528176.32-242806970585719`” ) && sleep 0’“'”‘’  
\<Private\_IP2\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP2\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP2\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP2\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP2\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP2\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP2\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP2\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP2\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP2\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP2\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r Private\_IP2 ‘/bin/sh -c ‘"’"’( umask 77 && mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1474528176.32-96061508357666`” && echo ansible-tmp-1474528176.32-96061508357666=“`echo $HOME/.ansible/tmp/ansible-tmp-1474528176.32-96061508357666`” ) && sleep 0’“'”‘’  
\<Private\_IP1\> PUT /tmp/tmplSwpxr TO /home/centos/.ansible/tmp/ansible-tmp-1474528176.32-242806970585719/ping  
\<Private\_IP1\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP1\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP1\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP1\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP1\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP1\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP1\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP1\> SSH: PlayContext set sftp\_extra\_args: ()  
\<Private\_IP1\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP1\> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r ‘[Private\_IP1]’  
\<Private\_IP2\> PUT /tmp/tmptkiK31 TO /home/centos/.ansible/tmp/ansible-tmp-1474528176.32-96061508357666/ping  
\<Private\_IP2\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP2\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP2\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP2\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP2\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP2\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP2\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP2\> SSH: PlayContext set sftp\_extra\_args: ()  
\<Private\_IP2\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP2\> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r ‘[Private\_IP2]’  
\<Private\_IP1\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP1\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP1\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP1\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP1\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP1\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP1\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP1\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP1\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP1\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP1\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r -tt Private\_IP1 ‘/bin/sh -c ‘"’“‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /home/centos/.ansible/tmp/ansible-tmp-1474528176.32-242806970585719/ping; rm -rf “/home/centos/.ansible/tmp/ansible-tmp-1474528176.32-242806970585719/” \> /dev/null 2\>&1 && sleep 0’”’“‘’  
\<Private\_IP2\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP2\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP2\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP2\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=”/home/ubuntu/.ssh/MyKey.pem")  
\<Private\_IP2\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP2\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP2\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP2\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP2\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP2\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP2\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r -tt Private\_IP2 ‘/bin/sh -c ‘"’“‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /home/centos/.ansible/tmp/ansible-tmp-1474528176.32-96061508357666/ping; rm -rf “/home/centos/.ansible/tmp/ansible-tmp-1474528176.32-96061508357666/” \> /dev/null 2\>&1 && sleep 0’”’"‘’  
Private\_IP1 | SUCCESS =\> {  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“data”: null  
},  
“module\_name”: “ping”  
},  
“ping”: “pong”  
}  
Private\_IP2 | SUCCESS =\> {  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“data”: null  
},  
“module\_name”: “ping”  
},  
“ping”: “pong”  
}  
Private\_IP3 | UNREACHABLE! =\> {  
“changed”: false,  
“msg”: “Failed to connect to the host via ssh.”,  
“unreachable”: true  
}

`

On the second time (after half a min max) when it **succeeds** , the logs are:

`

Using /etc/ansible/ansible.cfg as config file  
Loaded callback minimal of type stdout, v2.0  
\<Private\_IP3\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP3\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP1\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP1\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP3\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP3\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP1\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP1\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP1\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP3\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP3\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP3\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP1\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP1\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP1\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP3\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP3\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP1\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP1\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP3\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP1\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r Private\_IP1 ‘/bin/sh -c ‘"’"’( umask 77 && mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1474528970.94-42038548997439`” && echo ansible-tmp-1474528970.94-42038548997439=“`echo $HOME/.ansible/tmp/ansible-tmp-1474528970.94-42038548997439`” ) && sleep 0’“'”‘’  
\<Private\_IP3\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r Private\_IP3 ‘/bin/sh -c ‘"’"’( umask 77 && mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1474528970.94-11790883317121`” && echo ansible-tmp-1474528970.94-11790883317121=“`echo $HOME/.ansible/tmp/ansible-tmp-1474528970.94-11790883317121`” ) && sleep 0’“'”‘’  
\<Private\_IP2\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP2\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP2\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP2\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP2\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP2\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP2\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP2\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP2\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP2\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP2\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r Private\_IP2 ‘/bin/sh -c ‘"’"’( umask 77 && mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1474528970.94-115245318606115`” && echo ansible-tmp-1474528970.94-115245318606115=“`echo $HOME/.ansible/tmp/ansible-tmp-1474528970.94-115245318606115`” ) && sleep 0’“'”‘’  
\<Private\_IP1\> PUT /tmp/tmpIzmsRg TO /home/centos/.ansible/tmp/ansible-tmp-1474528970.94-42038548997439/ping  
\<Private\_IP1\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP1\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP1\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP1\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP1\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP1\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP1\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP1\> SSH: PlayContext set sftp\_extra\_args: ()  
\<Private\_IP1\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP1\> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r ‘[Private\_IP1]’  
\<Private\_IP2\> PUT /tmp/tmppBD\_UL TO /home/centos/.ansible/tmp/ansible-tmp-1474528970.94-115245318606115/ping  
\<Private\_IP2\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP2\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP2\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP2\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP2\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP2\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP2\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP2\> SSH: PlayContext set sftp\_extra\_args: ()  
\<Private\_IP2\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP2\> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r ‘[Private\_IP2]’  
\<Private\_IP1\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP1\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP1\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP1\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP1\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP1\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP1\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP1\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP1\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP1\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP1\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r -tt Private\_IP1 ‘/bin/sh -c ‘"’“‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /home/centos/.ansible/tmp/ansible-tmp-1474528970.94-42038548997439/ping; rm -rf “/home/centos/.ansible/tmp/ansible-tmp-1474528970.94-42038548997439/” \> /dev/null 2\>&1 && sleep 0’”’“‘’  
\<Private\_IP2\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP2\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP2\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP2\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=”/home/ubuntu/.ssh/MyKey.pem")  
\<Private\_IP2\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP2\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP2\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP2\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP2\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP2\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP2\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r -tt Private\_IP2 ‘/bin/sh -c ‘"’“‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /home/centos/.ansible/tmp/ansible-tmp-1474528970.94-115245318606115/ping; rm -rf “/home/centos/.ansible/tmp/ansible-tmp-1474528970.94-115245318606115/” \> /dev/null 2\>&1 && sleep 0’”’“‘’  
Private\_IP1 | SUCCESS =\> {  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“data”: null  
},  
“module\_name”: “ping”  
},  
“ping”: “pong”  
}  
Private\_IP2 | SUCCESS =\> {  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“data”: null  
},  
“module\_name”: “ping”  
},  
“ping”: “pong”  
}  
\<Private\_IP3\> PUT /tmp/tmpQBmgof TO /home/centos/.ansible/tmp/ansible-tmp-1474528970.94-11790883317121/ping  
\<Private\_IP3\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP3\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP3\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=”/home/ubuntu/.ssh/MyKey.pem")  
\<Private\_IP3\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP3\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP3\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP3\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP3\> SSH: PlayContext set sftp\_extra\_args: ()  
\<Private\_IP3\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP3\> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r ‘[Private\_IP3]’  
\<Private\_IP3\> ESTABLISH SSH CONNECTION FOR USER: centos  
\<Private\_IP3\> SSH: ansible.cfg set ssh\_args: (-o)(ControlMaster=auto)(-o)(ControlPersist=60s)  
\<Private\_IP3\> SSH: ANSIBLE\_HOST\_KEY\_CHECKING/host\_key\_checking disabled: (-o)(StrictHostKeyChecking=no)  
\<Private\_IP3\> SSH: ANSIBLE\_PRIVATE\_KEY\_FILE/private\_key\_file/ansible\_ssh\_private\_key\_file set: (-o)(IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”)  
\<Private\_IP3\> SSH: ansible\_password/ansible\_ssh\_pass not set: (-o)(KbdInteractiveAuthentication=no)(-o)(PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey)(-o)(PasswordAuthentication=no)  
\<Private\_IP3\> SSH: ANSIBLE\_REMOTE\_USER/remote\_user/ansible\_user/user/-u set: (-o)(User=centos)  
\<Private\_IP3\> SSH: ANSIBLE\_TIMEOUT/timeout set: (-o)(ConnectTimeout=10)  
\<Private\_IP3\> SSH: PlayContext set ssh\_common\_args: ()  
\<Private\_IP3\> SSH: PlayContext set ssh\_extra\_args: ()  
\<Private\_IP3\> SSH: found only ControlPersist; added ControlPath: (-o)(ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r)  
\<Private\_IP3\> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘IdentityFile=“/home/ubuntu/.ssh/MyKey.pem”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=centos -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r -tt Private\_IP3 ‘/bin/sh -c ‘"’“‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /home/centos/.ansible/tmp/ansible-tmp-1474528970.94-11790883317121/ping; rm -rf “/home/centos/.ansible/tmp/ansible-tmp-1474528970.94-11790883317121/” \> /dev/null 2\>&1 && sleep 0’”’"‘’  
Private\_IP3 | SUCCESS =\> {  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“data”: null  
},  
“module\_name”: “ping”  
},  
“ping”: “pong”  
}

`

Let me know if you find something.

Thanks again,  
Nirav

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [September 22, 2016, 10:21am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/6 "2016-09-22T10:21:26Z")

</div>

Hi Nirav,

Sorry about the lack of clarity in my suggestion. I was referring to your ec2.ini, specifically:

`

# AWS regions to make calls to. Set this to ‘all’ to make request to all regions

# in AWS and merge the results together. Alternatively, set this to a comma

# separated list of regions. E.g. ‘us-east-1,us-west-1,us-west-2’

regions = all  
regions\_exclude = us-gov-west-1,cn-north-1  
`

would become:

`

# AWS regions to make calls to. Set this to ‘all’ to make request to all regions

# in AWS and merge the results together. Alternatively, set this to a comma

# separated list of regions. E.g. ‘us-east-1,us-west-1,us-west-2’

regions = us-west-2a  
regions\_exclude = us-gov-west-1,cn-north-1

`

If you were using that particular availability zone. I’d be surprised if that worked, however, what happens when you unset the environment variable and set the region in the ec2.ini file?

Alex

---

<div class="post-metadata">

**Author:** ![Nirav\_Radia](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nirav\_Radia](https://forum.ansible.com/u/Nirav_Radia)\
**Post date:** [September 22, 2016, 10:43am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/7 "2016-09-22T10:43:37Z")

</div>

Hi Alex,

Replacing availability zone in ec2.ini also gave me the same error:

`

ERROR! The file inventory/ec2.py is marked as executable, but failed to execute correctly. If this is not supposed to be an executable script, correct this with `chmod -x inventory/ec2.py`.  
Inventory script (inventory/ec2.py) had an execution error: region name: us-west-2a likely not supported, or AWS is down. connection to region failed.  
inventory/ec2.py:3: Error parsing host definition ‘’‘’': No closing quotation

`

Unsetting the environment variable and setting region in ec2.ini does not help and gives the same Unreachable error. ☹

Thanks,  
Nirav

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [September 23, 2016, 12:25am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/8 "2016-09-23T00:25:28Z")

</div>

Hi Nirav,

This is a tough nut to crack. Which version of Ansible are you using?

Which region is it specifically that is failing? Or is it just any region that is specified over ‘any’ region?

Alex

---

<div class="post-metadata">

**Author:** ![Nirav\_Radia](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nirav\_Radia](https://forum.ansible.com/u/Nirav_Radia)\
**Post date:** [September 26, 2016, 6:01am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/9 "2016-09-26T06:01:43Z")

</div>

Hi Alex

Sorry for late reply.

I am using Ansible 2.1.1.0.

It happens for any available region over AWS.

One more finding:

- If I remove port in wait\_for, it works ok.

---

<div class="post-metadata">

**Author:** ![Nirav\_Radia](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nirav\_Radia](https://forum.ansible.com/u/Nirav_Radia)\
**Post date:** [September 26, 2016, 7:29am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/10 "2016-09-26T07:29:59Z")

</div>

Hi Alex / everyone,

Another finding:

- This also works fine:

`  
wait\_for: host={{ item.public\_dns\_name }} port=22 delay=60 timeout=320 state=started

`

I want to understand why

`  
wait\_for: host={{ item.public\_dns\_name }} port=22

`

does not work when I query for a particular region? Does it return before port 22 is actually open or before machine is actually SSH connect-able?

Just FYI, I am using a custom AMI built on top of Official CentOS 7 AMI (If this helps or if it is an OS specific issue).

Thanks,  
Nirav

---

<div class="post-metadata">

**Author:** ![Nirav\_Radia](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nirav\_Radia](https://forum.ansible.com/u/Nirav_Radia)\
**Post date:** [September 26, 2016, 11:48am UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/11 "2016-09-26T11:48:14Z")

</div>

Hi,

While browsing the integration tests of Ansible modules, I cam across this:  
[https://github.com/ansible/ansible/blob/7f8e8ddca9cf103c05bf39d68ebb2e2ded4067f2/test/utils/ansible-playbook\_integration\_runner/ec2.yml](https://github.com/ansible/ansible/blob/7f8e8ddca9cf103c05bf39d68ebb2e2ded4067f2/test/utils/ansible-playbook_integration_runner/ec2.yml)

It has a task named “Wait a little longer for centos”. So anyone know why is it there? My target hosts run CentOS also, if this is the issue for the behavior mentioned in my question.

Thanks,  
Nirav

---

<div class="post-metadata">

**Author:** ![Nirav\_Radia](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@Nirav\_Radia](https://forum.ansible.com/u/Nirav_Radia)\
**Post date:** [October 7, 2016, 1:36pm UTC](https://forum.ansible.com/t/unable-to-reach-to-ec2-instance-using-dynamic-inventory-with-a-single-region/22566/12 "2016-10-07T13:36:03Z")

</div>

I could not find the real reason why the SSH on EC2 instance running CentOS was not able to connect. The workaround I applied was to write a module which try to connect host over SSH for given number of times and return success when it is successful so that further plays won’t fail because of unreachable error.

**wait\_for\_SSH.py**

`

#!/usr/bin/python  
‘’’  
module: wait\_for\_SSH  
short\_description: Waits for a host to be SSH connectable.  
description:

- Tries to Ansible ping(not ICMP ping) to host as per the passed parameters  
options:  
host:  
description:
- A resolvable hostname or IP address to ansible ping  
required: true  
retries:  
description:
- maximum number of times to retry  
required: false  
default: 10  
delay:  
description:
- number of seconds to wait between two consecutive pings  
required: false  
default: 5  
‘’’  
from ansible.module\_utils.basic import \*  
from subprocess import call  
import time

def validate\_params(module, retries, delay):  
if retries \< 0:  
module.fail\_json(msg=“retries should be greater than 0”)  
if delay \< 0:  
module.fail\_json(msg=“delay should be greater than 0”)  
return

def main():  
fields = {  
“host”: {“required”: True, “type”: “str”},  
“retries”: {“required”: False, “type”: “int”, “default”: 10},  
“delay”: {“required”: False, “type”: “int”, “default”: 5}  
}  
module = AnsibleModule(argument\_spec=fields)  
host = module.params[‘host’]  
retries = module.params[‘retries’]  
delay = module.params[‘delay’]  
count = 0  
output = 1  
validate\_params(module, retries, delay)  
while (count \< retries) and (output != 0):  
if delay:  
time.sleep(delay)  
output = call([“ansible”, “all”, “-i”, “,”+host, “-m”, “ping”])  
count += 1  
response = {“output” : output}  
module.exit\_json(changed=False, output=response)

if **name** == ‘ **main** ’:  
main()

`

And executed it after wait\_for port 22.

`

…

# Wait only for running instances because ‘ec2\_server’ might contain terminated instances to fulfil exact\_count condition

- name: wait for ssh server to be running  
wait\_for: host={{ item.public\_dns\_name }} port=22 search\_regex=OpenSSH  
with\_items: “{{ec2\_server.instances | default()}}”  
when: item.state == ‘running’

# wait\_for\_SSH is our custom module which tries to Ansible ping on created instances until it is successful as per retries specified(bug AD-3)

- name: Ensure SSH is running  
wait\_for\_SSH:  
host: “{{item.private\_ip}}”  
register: moduleoutput  
with\_items: “{{ec2\_server.instances | default()}}”  
when: item.state == ‘running’  
..\<\>…

`

Thanks,  
Nirav
