Ansible: How to check SSH access

Viewed 57

Good morning all,

I'm racking my brains over a simple subject.

I'm on a "master" server and I would like to check if he manages to connect in SSH on a server list.

Example

ansible-playbook -i inventaire_test test_ssh.yml
---

      tasks:
    
      - name: test unreachable
        ansible.builtin.ping:
        register: test_ssh
        ignore_unreachable: true
    
      - name: test
        fail:
          msg: "test"
        when: test_ssh.unreachable is defined
    
      - name: header CSV
        lineinfile:
            insertafter: EOF
            dest: /home/list.csv
            line: "Server;OS;access"
        delegate_to:localhost
    
      - name: Info
        lineinfile:
            dest: /home/list.csv
            line: "{{ inventory_hostname }};OK"
            state: present
        when: test_ssh is successful
        delegate_to:localhost
    
      - name: Info csv
        lineinfile:
            dest: /home/list.csv
            line: "{{ inventory_hostname }};KO"
            state: present
        when: test_ssh.unreachable is undefined
        delegate_to:localhost

I can't find a check_ssh module. There is ansible.builtin.ssh but I can't use it.

Do you have an idea?

Thanks in advance.

1 Answers

Regarding

I'm on a "master" server and I would like to check if he manages to connect in SSH on a server list. ... I can't find a check_ssh module.

According the documentation there is a

ping module – Try to connect to host, verify a usable python and return pong on success

... test module, this module always returns pong on successful contact. It does not make sense in playbooks, but it is useful from /usr/bin/ansible to verify the ability to login and that a usable Python is configured.

which seems to be doing what you are looking for.

Related