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

**URL:** <https://forums.rockylinux.org/t/ansible-failed-to-connect-to-the-host-via-ssh-error/11659>\
**Category:** Rocky Linux Help & Support\
**Tags:** rocky-linux-9\
**Created:** [October 22, 2023, 5:08pm UTC](https://forums.rockylinux.org/t/ansible-failed-to-connect-to-the-host-via-ssh-error/11659 "2023-10-22T17:08:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jil](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/jil/32/1089_2.png) [@Jil](https://forums.rockylinux.org/u/Jil)\
**Post date:** [October 22, 2023, 5:08pm UTC](https://forums.rockylinux.org/t/ansible-failed-to-connect-to-the-host-via-ssh-error/11659/1 "2023-10-22T17:08:25Z")

</div>

Hello Rocky Linux Forum.

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

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

```auto
sudo dnf install epel-release

```

Then installed Ansible:

```auto
sudo dnf -y install ansible

```

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

```auto
/etc/ansible/hosts

```

But when I run the Ad-Hoc Ansible command:

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

```

I get the following error:

```auto
$ 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:

```auto
$ 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.

---

<div class="post-metadata">

**Author:** ![iwalker](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/iwalker/32/2599_2.png) [@iwalker](https://forums.rockylinux.org/u/iwalker)\
**Post date:** [October 22, 2023, 5:10pm UTC](https://forums.rockylinux.org/t/ansible-failed-to-connect-to-the-host-via-ssh-error/11659/2 "2023-10-22T17:10:09Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Jil](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/jil/32/1089_2.png) [@Jil](https://forums.rockylinux.org/u/Jil)\
**Post date:** [October 22, 2023, 5:35pm UTC](https://forums.rockylinux.org/t/ansible-failed-to-connect-to-the-host-via-ssh-error/11659/3 "2023-10-22T17:35:21Z")

</div>

Thank you so much @iwalker . It now works

```auto
$ 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.

---

<div class="post-metadata">

**Author:** ![Jil](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/jil/32/1089_2.png) [@Jil](https://forums.rockylinux.org/u/Jil)\
**Post date:** [October 22, 2023, 6:38pm UTC](https://forums.rockylinux.org/t/ansible-failed-to-connect-to-the-host-via-ssh-error/11659/4 "2023-10-22T18:38:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/rockylinux/original/1X/91b7219eec10e30013422e4df76c1d898711a5d5.svg) [@system](https://forums.rockylinux.org/u/system)\
**Post date:** [December 21, 2023, 6:38pm UTC](https://forums.rockylinux.org/t/ansible-failed-to-connect-to-the-host-via-ssh-error/11659/5 "2023-12-21T18:38:42Z")

</div>

Closed on December 21, 2023, 6:38pm UTC

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