AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 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 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", 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 'Failed to Connect via SSH localhost:22': Fix Guide — Video Tutorial
Fix Ansible 'failed to connect to the host via ssh localhost port 22' error. Resolve SSH config, connection type, host key, and authentication issues.
What You'll Learn
- Introduction
- Understanding the Error
- Live Demo
- Error Code: `ping.yml`
- Error Execution
- Fix Code: `ping.yml`
- Fix Execution
- Links
- Conclusion
- The Fix: Use Local Connection
Full Tutorial Content
Introduction
In today's episode of Ansible Pilot, I'm Luca Berton, and we'll delve into Ansible troubleshooting, focusing on the common error "Failed to connect to the host via SSH: localhost port 22." This error often occurs when testing your code on your local machine using the `ansible_connection local` parameter.
Understanding the Error
The exact error message you might encounter in the terminal is:
```bash
fatal: [localhost]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via SSH: ssh: connect to host localhost port 22: Connection refused", "unreachable": true}
```
This error is a clear indication that Ansible failed to establish an SSH connection to the localhost on port 22.
Live Demo
Let's jump into a live Playbook to reproduce the Ansible connection failed problem and fix it in the inventory file.
Error Code: `ping.yml`
```yaml
---
- name: ping module Playbook
hosts: all
tasks:
- name: test connection
ansible.builtin.ping:
```
Error Execution
Executing the playbook with the error:
```bash
$ ansible-playbook -i inventory ping.yml
PLAY [ping module Playbook] *****************************************************************
TASK [Gathering Facts] ******************************************************************
fatal: [localhost]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via SSH: ssh: connect to host localhost port 22: Connection refused", "unreachable": true}
PLAY RECAP ******************************************************************************
localhost : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
```
Fix Code: `ping.yml`
```yaml
---
- name: ping module Playbook
hosts: all
tasks:
- name: test connection
ansible.builtin.ping:
```
Fix Execution
Executing the fixed playbook:
```bash
$ ansible-playbook -i inventory ping.yml
PLAY [ping module Playbook] *****************************************************************
TASK [Gathering Facts] ******************************************************************
[WARNING]: Platform darwin on host localhost is using the discovered Python interpreter
at /opt/homebrew/bin/python3.10, but future installation of another Python interpreter
could change the meaning of that path. See https://docs.ansible.com/ansible-
core/2.13/reference_appendices/interpreter_discovery.html for more information.
ok: [localhost]
TASK [test connection] ******************************************************************
ok: [localhost]
PLAY RECAP ******************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
Links
- [Local playbooks](https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html)
Conclusion
In conclusion, you now know how to troubleshoot the common Ansible error "Failed to connect to the host via
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 7 min
- Category: installation
Read the full written article: Ansible 'Failed to Connect via SSH localhost:22': Fix Guide