Interview Prep/Ansible/Connection and SSH Failures

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

All Ansible questions Roadmap: Ansible

More on Connection and SSH Failures