# How do I handle a failure to connect to SSH on port 22?

**URL:** <https://forum.ansible.com/t/how-do-i-handle-a-failure-to-connect-to-ssh-on-port-22/14544>\
**Category:** Ansible Project\
**Created:** [February 11, 2014, 8:23pm UTC](https://forum.ansible.com/t/how-do-i-handle-a-failure-to-connect-to-ssh-on-port-22/14544 "2014-02-11T20:23:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Randolph\_Kahle](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@Randolph\_Kahle](https://forum.ansible.com/u/Randolph_Kahle)\
**Post date:** [February 11, 2014, 8:23pm UTC](https://forum.ansible.com/t/how-do-i-handle-a-failure-to-connect-to-ssh-on-port-22/14544/1 "2014-02-11T20:23:56Z")

</div>

I am still working on bootstrapping compute instances and exploring the boundary between where a provisioning service (e.g. Vagrant) stops and Ansible starts.

I prefer to run SSH on a non-standard port (2222) instead of 22. I could have Vagrant set up an instance with SSH running on port 2222, but I want to explore if I can still work with an initial SSH service on port 22.

If my provisioned compute instance starts out with SSH on port 22, is there a module that will test for the ability to connect on port 22 and if that fails, then register that fact so I do not run a bunch of steps in a playbook?

Thanks!

-- Randy

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 12, 2014, 2:50am UTC](https://forum.ansible.com/t/how-do-i-handle-a-failure-to-connect-to-ssh-on-port-22/14544/2 "2014-02-12T02:50:49Z")

</div>

So, untested … but, possibly using the wait\_for module to do the test and ignoring errors, and then setting the inventory variable named ‘ansible\_ssh\_port’ using set\_fact ?

I’m thinking maybe like:

- wait\_for: port=22 # some amount of delay  
register: port\_test  
failed\_when: False

- set\_fact: ansible\_ssh\_port=2222  
when: port\_test|failed

?

Again, untested. Sorry for me being quick.
