AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible 'Connection failed' Error: Fix SSH & WinRM Issues (Guide)

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

Fix Ansible connection failed errors. Troubleshoot SSH timeouts, authentication failures, WinRM issues, and unreachable hosts with step-by-step solutions.

Today we're going to talk about Ansible troubleshooting and specifically about connection failed errors. I'm Luca Berton and welcome to today's episode of the Ansible Pilot. The root cause of these types of problems rely directly on the networking. So jump on the ssh command line and troubleshoot like an ssh connection error. In the following example the Operation timed out was caused by the virtual machine that doesn’t have network card enabled.

LinksNetwork Debug and Troubleshooting Guide

Demo The best way of talking about Ansible troubleshooting is to jump in a live Playbook to show you practically the connection failed error and how to solve it!

Error

Ansible relies on an SSH connection to the target machine. Let's try manually:

You need to verify the network connection between the source and the target machine.

Fix

Once the network connection is fixed you could successfully connect to the target machine.

code with ❤️ in GitHub

Conclusion Now you know better how to troubleshoot the most common Ansible error about connection failure.

Common Connection Errors and Fixes

Error 1: Operation timed out

Causes: • Target machine is powered off or unreachable • Firewall blocking port 22 • Wrong IP address in inventory • Network routing issue

Fix checklist:

Error 2: Permission denied (publickey,password)

Causes: • Wrong username • SSH key not copied to target • SSH key passphrase not provided • PasswordAuthentication no in sshd_config

Fix:

Error 3: Host key verification failed

Fix (choose one):

⚠️ Warning: Disabling host key checking in production is a security risk. Use ssh-keyscan to populate known_hosts instead.

Error 4: Connection refused

Causes: • SSH daemon (sshd) not running on target • SSH listening on a different port

Fix:

Debugging Connection Issues

Increase verbosity

Test with ansible ping module

Check Ansible configuration

ansible.cfg Connection Settings

FAQ

How do I connect to Windows hosts?

Windows doesn't use SSH by default. Use WinRM:

How do I use a jump host / bastion?

How do I increase the connection timeout?

Quick Diagnosis

Common Causes & Fixes

SSH Authentication Failed

Host Key Verification Failed

Connection Timeout

Python Not Found

Wrong Port

WinRM Connection Issues

Network/Firewall Issues

SSH Connection Reuse

Debugging Checklist

| Check | Command | |-------|---------| | Host reachable | ping 192.168.1.10 | | SSH port open | nc -zv host 22 | | SSH works manually | ssh -vvv user@host | | Key permissions | ls -la ~/.ssh/ | | Python on remote | ssh user@host python3 --version | | Ansible verbose | ansible host -m ping -vvvv | | DNS resolution | nslookup hostname |

FAQ

"Permission denied (publickey)" but key is correct?

Check: key permissions (600), SSH agent running, correct user, key added to authorized_keys on remote.

"Connection timed out" intermittently?

Could be network issues, firewall rules, or SSH MaxStartups limit on the server. Increase ansible_ssh_timeout and check server SSH config.

How do I connect through a bastion/jump host?

Related ArticlesAnsible Roles Guide

Category: troubleshooting

Watch the video: Ansible 'Connection failed' Error: Fix SSH & WinRM Issues (Guide) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home