I'm new to ansible but are having basic issues reaching multiple hosts via ansible. I'm able to reach all hosts via ssh and also if I use ansible to target any specific host in my inventory. It successfully reaches one of my hosts but fails all the others.

If I run:

ansible all -i inventory.yml -u oytal -m ping

It returns: | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    "changed": false,
    "ping": "pong"
    "changed": false,
    "msg": "Failed to connect to the host via ssh: oytal@ Permission denied (publickey).",
    "unreachable": true
    "changed": false,
    "msg": "Failed to connect to the host via ssh: oytal@ Permission denied (publickey).",
    "unreachable": true
    "changed": false,
    "msg": "Failed to connect to the host via ssh: oytal@ Permission denied (publickey).",
    "unreachable": true

It's not consistent which host is unreachable, I shifted around the order of my hosts and even removed the successful one, and it will reach one of the others instead but still fail the rest.

My inventory:

  • Try the hosts group tag, make sure that oytal is in the sudo group on each host, or at least has the proper perms for ssh - try 'ansible -i inventory.yml -l hosts -u oytal -m ping, so using the --limit parameter on the group
    – jdopenvpn
    Jan 16 at 15:49
  • I'm not sure, if "grouping" the hosts will resolve the problems. I think, there is a different problem that hosts can be sometimes reached via SSH and sometimes not. That can't be a problem with Ansible but with the environment. Of course, be sure, that the user can reach ALL hosts via SSH.
    – TRW
    Jan 16 at 16:32
  • I doubt, running by the same user, ssh oytal@ can connect and ansible ... -u oytal can't. This would suggest some kind of SSH keys misconfiguration i.e. Ansible and SSH utilities are configured to use different keys (and not all public keys are stored in authorized_keys at remote hosts). Jan 16 at 21:45

Permission denied (publickey)

when running ssh oytal@ (and other hosts) means sshd at (and other hosts) is not able to get the public key of the user running the ssh command from /home/oytal/.ssh/authorized_keys at (and other hosts), by default.

There may be many reasons for this. Make sure ssh command works

shell> ssh oytal@

Next, make sure Ansible uses the correct private key (see `ansible-doc -t connection ssh). Try and set it manually. For example

shell> ANSIBLE_PRIVATE_KEY_FILE=$HOME/.ssh/id_rsa ansible -i inventory.yml -u oytal -m ping

Review inventory and Ansible configuration

shell> ansible-config dump

If this doesn't help see other Permission denied (publickey) answers explaining possible reasons for the error.


I finally found the solution to my issue. I feel kinda dumb but it was because my id_ed25519 was encrpyted.

I entered following commands to fix the issue:

ssh-agent bash

ssh-add /home/$USER/.ssh/id_ed25519


"Log in & log out the all remote servers first. Then, ping to the remote servers with ansible."

I also couldn't ping to multiple remote servers so I did the solution above. Then, I could ping to the all remote servers.

I don't know what's happening backside but I guess that we need to leave the fingerprints of the master server to the all remote server first before pinging to the remote servers with ansible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.