AutoDeploy
← All guides

Ansible

MEDIUMOtherBEGINNER

Ansible SSH Connection Failed

Ansible cannot target hosts via SSH.

Est. Time

20 minutes

Version

v1.0.0

Updated

7/28/2026

Author

AutoDeploy Team

Tags

#ansible#ssh#connection#authentication

Prerequisites

  • Ansible installed
  • SSH access

Setup Guide

Download guide

Ansible SSH Connection Failed

Problem

Ansible playbooks fail with SSH connection errors when trying to manage remote hosts.

Symptoms

  • "SSH connection failed" error
  • "UNREACHABLE" error
  • Playbook execution stops
  • Cannot connect to inventory hosts

Root Cause

SSH misconfiguration, authentication issues, or network connectivity problems.

Solution

1. Test SSH Connection

ssh user@target-host
ssh -vvv user@target-host

2. Check Ansible Inventory

[webservers]
host1 ansible_host=192.168.1.10 ansible_user=ubuntu
host2 ansible_host=192.168.1.11 ansible_user=ubuntu

3. Configure SSH Key Authentication

# Generate SSH key
ssh-keygen -t rsa

# Copy to target
ssh-copy-id user@target-host

4. Update ansible.cfg

[defaults]
host_key_checking = False
private_key_file = ~/.ssh/id_rsa
remote_user = ubuntu

5. Use SSH Agent

ssh-add ~/.ssh/id_rsa

6. Check SSH Config

cat ~/.ssh/config

7. Add SSH Config Entry

Host target-host
  HostName 192.168.1.10
  User ubuntu
  IdentityFile ~/.ssh/id_rsa
  StrictHostKeyChecking no

8. Test with Ansible Ping

ansible all -m ping -i inventory.ini
ansible all -m ping -u ubuntu -i inventory.ini

9. Use Password Authentication

ansible-playbook playbook.yml -u ubuntu -k

10. Check Firewall

sudo ufw allow 22/tcp
sudo iptables -L -n

11. Increase SSH Timeout

[defaults]
timeout = 30

Prevention

  • Use SSH key authentication
  • Implement proper inventory management
  • Monitor SSH connectivity
  • Use SSH config files
  • Regular connection testing

References

Last updated on 7/28/2026 · Part of the AutoDeploy DevOps Documentation library