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) — Video Tutorial
Fix Ansible connection failed errors. Troubleshoot SSH timeouts, authentication failures, WinRM issues, and unreachable hosts with step-by-step solutions.
What You'll Learn
- Links
- Demo
- Error
- Fix
- Conclusion
- Common Connection Errors and Fixes
- Error 1: Operation timed out
- Error 2: Permission denied (publickey,password)
- Error 3: Host key verification failed
- Error 4: Connection refused
Full Tutorial Content
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.
Links
- [Network Debug and Troubleshooting Guide](https://docs.ansible.com/ansible/latest/network/user_guide/network_debug_troubleshooting.html)
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:
```bash
$ ssh username@hostname
Failed to connect to the host via ssh: ssh: connecto to host hostname port 22: Operation timed out
```
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.
```bash
$ ssh username@hostname
username@hostname:~$
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/troubleshooting)
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
```
fatal: [server]: UNREACHABLE! => {"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.1.100 port 22: 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:**
```bash
1. Can you reach the host at all?
ping 192.168.1.100
2. Is SSH port open?
nc -zv 192.168.1.100 22
3. Can you SSH manually?
ssh user@192.168.1.100
4. Check firewall on target (if you have console access)
sudo firewall-cmd --list-ports # RHEL/CentOS
sudo ufw status # Ubuntu/Debian
```
Error 2: Permission denied (publickey,password)
```
fatal: [server]: UNREACHABLE! => {"msg": "Failed to connect to the host via ssh: user@192.168.1.100: Permission denied (publickey,password)."}
```
**Causes:**
- Wrong username
- SSH key not copied to target
- SSH key passphrase not provided
- `PasswordAuthentication no` in sshd_config
**Fix:**
```bash
Copy your SSH key to the target
ssh-copy-id user@192.168.1.100
Or specify the key in inventory
inventory.yml
all:
hosts:
server:
ansible_host: 192.168.1.100
ansible_user: devops
ansible_ssh_private_key_file: ~/.ssh/id_rsa
```
Error 3: Host key verification failed
```
fatal: [server]: UNREACHABLE! => {"msg": "Failed to connect to the host via ssh: Host key verification failed."}
```
**Fix (choose one):**
```bash
Option 1: Accept the host key manually
ssh user@192.168.1.100 #
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 6 min
- Category: troubleshooting
Read the full written article: Ansible 'Connection failed' Error: Fix SSH & WinRM Issues (Guide)
Related Video Tutorials
- Ansible 'Failed to Connect via SSH localhost:22': Fix Guide — Fix Ansible 'failed to connect to the host via ssh localhost port 22' error. Resolve SSH config, connection type, host key, and authentication issues.
- Ansible troubleshooting - Destination does not exist rc 257 — Troubleshoot the "Destination does not exist" error (return code 257) in Ansible with Luca Berton on Ansible Pilot! Learn to fix file path issues effectively.
- Ansible Vault Error: 'Attempting to decrypt but no vault secrets found' Fix — Fix Ansible error 'Attempting to decrypt but no vault secrets found'. Provide vault password via --ask-vault-pass, password file, or environment variable.
- Ansible troubleshooting - Error markupsafe — Resolve the No module named markupsafe error in Ansible by reinstalling Ansible, ansible-lint, and python-markupsafe via Homebrew.
- Ansible troubleshooting - Module Failure on Windows-target — Discover how to troubleshoot Module Failure on Windows-target in Ansible, focusing on resolving execution errors effectively.
- Ansible Permission Denied (Errno 13): Fix File Access Errors — Fix Ansible Permission denied [Errno 13] errors. Resolve file permission, become/sudo, SELinux, and directory access issues with troubleshooting steps.