`UNREACHABLE! => {"msg": "Failed to connect to the host via ssh: Permission denied (publickey,password)."}`
BeginnerTroubleshootingConnection and SSH Failures
Answer
Likely causes → Wrong user, wrong or missing private key, key not in authorized_keys, or the server disallows the auth method.
Diagnose →
ansible web01 -m ping -vvv
ssh -i ~/.ssh/id_ed25519 -v ec2-user@web01
-vvv shows the exact ssh command Ansible built, including -o User= and IdentityFile.
Fix → Set ansible_user and ansible_ssh_private_key_file in inventory, or pass -u and --private-key. Add the public key to ~/.ssh/authorized_keys with correct permissions (700 on .ssh, 600 on the file).
Prevent → Standardise one automation user and key per environment, and test with ansible all -m ping in CI.
More on Connection and SSH Failures
`UNREACHABLE! => {"msg": "Failed to connect to the host via ssh: ssh: connect to host 10.0.1.15 port 22: Connection timed out"}`Beginner`UNREACHABLE! => {"msg": "Failed to connect to the host via ssh: ssh: connect to host web01 port 22: Connection refused"}`Beginner`Host key verification failed.` after rebuilding a serverBeginnerThe playbook hangs for ever at "Gathering Facts" on one hostIntermediate`Failed to connect to the host via ssh: Too many authentication failures`Intermediate`fatal: [web01]: UNREACHABLE! => {"msg": "Failed to connect to the host via ssh: Shared connection to web01 closed."}` mid-runIntermediate