Ansible troubleshooting - Failed to connect to the host via ssh host localhost port 22
How to solve the error connection failed fatal error when connecting via ssh host localhost port 22 when testing your code on your local machine using ansible_connection local parameter.
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:
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 demo to reproduce the Ansible connection failed problem and fix it in the inventory file.
Error Code: ping.yml
---
- name: ping module demo
hosts: all
tasks:
- name: test connection
ansible.builtin.ping:
Error Execution
Executing the playbook with the error:
$ ansible-playbook -i inventory ping.yml
PLAY [ping module demo] *****************************************************************
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
---
- name: ping module demo
hosts: all
tasks:
- name: test connection
ansible.builtin.ping:
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Fix Execution
Executing the fixed playbook:
$ ansible-playbook -i inventory ping.yml
PLAY [ping module demo] *****************************************************************
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
Recap
In conclusion, you now know how to troubleshoot the common Ansible error “Failed to connect to the host via SSH localhost port 22.” By modifying the inventory file and setting ansible_connection=local
, you can resolve this issue when testing Ansible code on your local machine.
If you found this troubleshooting guide helpful, be sure to subscribe for more Ansible insights.
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate