# Check if passwordless ssh works after checking telnet from localhost

**URL:** <https://forum.ansible.com/t/check-if-passwordless-ssh-works-after-checking-telnet-from-localhost/33506>\
**Category:** Ansible Project\
**Created:** [July 28, 2020, 3:57am UTC](https://forum.ansible.com/t/check-if-passwordless-ssh-works-after-checking-telnet-from-localhost/33506 "2020-07-28T03:57:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [July 28, 2020, 3:57am UTC](https://forum.ansible.com/t/check-if-passwordless-ssh-works-after-checking-telnet-from-localhost/33506/1 "2020-07-28T03:57:05Z")

</div>

I wish to list down all the hosts where it first checks for working telnet on port 22. If telnet succeeds; it should check for passwordless ssh and list down all hosts where telnet works but passwordless ssh fails.

The below playbook helps with the first part of checking all hosts where telnet works.

- name: Play 2- check telnet nodes

```auto
  hosts: localhost
  user: axmwapp
  vars:
    ansible_ssh_extra_args: -o StrictHostKeyChecking=no
    ansible_ssh_private_key_file: /app/axmw_id_rsa
  tasks:

   - name: Check all port numbers are accessible from current host
     include_tasks: /app/checkssh/innertelnet.yml
     with_items: "{{ groups['all_hosts'] }}"

cat /app/checkssh/innertelnet.yml

---
       - wait_for:
           host: "{{ item }}"
           port: 22
           state: started
           delay: 0
           timeout: 2
         ignore_errors: yes
         register: netstatoutput
         delegate_to: localhost

       - set_fact:
           telnetcheck: "{% if netstatoutput.failed == 'False' %} 'OPEN' {% else %} 'BLOCKED' {% endif %}"
         when: "{{ netstatoutput.failed }}"

       - debug:
           msg: "Telnet works on {{ item }}"
         when: not netstatoutput.failed

```

```auto
From the above successful telnet IPs, I wish to check & report hosts where passwordless ssh fails. But, I'm not sure how-to ?

```

```auto
I tried the below but it becomes interactive and prompts for a password rather than checking all seccessful telnet hosts for passwordless ssh. 

```

```auto
       - name: Check ssh connectivity
         raw: "ssh -i {{ ansible_ssh_private_key_file }} root@{{ item }} echo success"
         register: sshcheck
         delegate_to: localhost
         when: not netstatoutput.failed

       - set_fact:
           sshcheck: "Telnet Works but SSH Fails"
         when: not netstatoutput.failed and sshcheck.rc != 0

       - debug:
           msg: "INNERSSH: {{ sshcheck }}"

```

```auto
         when: not netstatoutput.failed and sshcheck.rc != 0 

```

```auto
Can you please guide?

```

```auto

```
