--- - name: Determine SSH port hosts: all gather_facts: no tasks: - name: "Check port {{ ansible_port }}" wait_for: port: "{{ ansible_port }}" state: "started" host: "{{ ansible_host }}" connect_timeout: "5" timeout: "5" delegate_to: "localhost" ignore_errors: "yes" register: ssh_port - name: "Check port 22" wait_for: port: "22" state: "started" host: "{{ ansible_host }}" connect_timeout: "5" timeout: "5" delegate_to: "localhost" ignore_errors: "yes" register: ssh_port_default when: - ssh_port is defined - ssh_port.state is undefined - name: Set SSH port to 22 set_fact: ansible_port: "22" when: ssh_port_default.state is defined