Ansible : "Failed to connect to the host via ssh: error

Hello Rocky Linux Forum.

I am learning Ansible, and so I ask this question very carefully. Please don’t box me :slight_smile:

I have successfully installed Ansible on my system running on Rocky 9.2 via the commands:

sudo dnf install epel-release

Then installed Ansible:

sudo dnf -y install ansible

Then I placed my inventory file in the Ansibles global inventory file in the

/etc/ansible/hosts

But when I run the Ad-Hoc Ansible command:

ansible -i [my_inventory_file] [server_group] -m ping -u <user>

I get the following error:

$ ansible -i hosts.ini example -m ping -u jil
192.168.200.40 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: jil@192.168.200.40: Permission denied (publickey,password).",
    "unreachable": true
}

If I ssh to the same server using the same user and server IP address:

$ ssh jil@192.168.200.40
jil@192.168.200.40's password: 

I can successfully log in to the server.

where am I making a mistake? PLease educate me.

Add --ask-pass parameter to the ansible command line, or generate an SSH key and use that key instead to connect to the host over ssh.

Usernames can also be passed on the command line, if your current user on the ansible host is not jil.

1 Like

Thank you so much @iwalker . It now works

$ sudo ansible -i hosts.ini example -m ping -u jil --ask-pass
SSH password: 
192.168.200.40 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

Thank you.

1 Like

I have set up a passwordless (key-based) login for SSH and worked fine too.

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.