2

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:

192.168.1.90 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}


192.168.1.21 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey).",
    "unreachable": true
}


192.168.1.20 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey).",
    "unreachable": true
}


192.168.1.100 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: [email protected]: 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:

---
all:
    hosts:
        192.168.1.90:
        192.168.1.21:
        192.168.1.20:
        192.168.1.100:
3
  • 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, 2021 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, 2021 at 16:32
  • I doubt, running by the same user, ssh [email protected] 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, 2021 at 21:45

3 Answers 3

1

Permission denied (publickey)

when running ssh [email protected] (and other hosts) means sshd at 192.168.1.21 (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 192.168.1.21 (and other hosts), by default.

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

shell> ssh [email protected]

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

0

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
0

Solution:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .