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

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

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.