About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.
Ansible ends a task with fatal: [app01]: UNREACHABLE! => {... "msg": "Failed to connect to the host via ssh: ...", "unreachable": true} when the ssh command it ran exited with its connection-error code. Everything after the colon is what ssh itself wrote, so that text names the real fault. Read it with -vvvv, compare it with what the inventory tells Ansible to use, and reproduce the connection with ssh by hand using the same user, host, port and key.
What the message means
In ansible-core v2.18.1 the ssh connection plugin treats an ssh exit code of 255 as a connection failure, except where the output carries one of the signatures the plugin lists as not coming from ssh itself, such as Traceback (most recent call last): or PHP Parse error:. Its code builds the text Failed to connect to the host via ssh: followed by the stderr of ssh and raises AnsibleConnectionFailure; the task executor turns that exception into a result with unreachable=True and the same msg; the default callback prints fatal: [<host>]: UNREACHABLE! => .... Our own reading of that code: the sentence is Ansible's wrapper, and the part after the colon is ssh's own text.
The plugin's documentation gives the rule behind it: the ssh tool uses return code 255 for a connection error, which can clash with commands that also return 255 on the remote side. A remote command that exits 255 can therefore be reported as unreachable although the connection worked.
Three plugin defaults matter for a first look. The timeout option, "the default amount of time we will wait while establishing an SSH connection", defaults to 10 (seconds). reconnection_retries defaults to 0, and the page says "Ansible retries connections only if it gets an SSH error with a return code of 255." Host key checking is on by default: Ansible's connection page says "Ansible enables host key checking by default."
Likely causes
The ssh wording below is our own reading of what OpenSSH usually prints; the text on your screen decides.
| Cause | What you see after the colon | How to confirm |
|---|---|---|
| Wrong name or address | Could not resolve hostname |
ansible-inventory --host shows the ansible_host value |
| Wrong user | Permission denied (publickey) |
ansible_user differs from the account that has your key |
| Key not offered or not accepted | Permission denied (publickey) |
ansible_ssh_private_key_file or --private-key points elsewhere |
| Key file too open | Banner UNPROTECTED PRIVATE KEY FILE! |
See SSH "WARNING: UNPROTECTED PRIVATE KEY FILE!" "Permissions 0644 for 'id_ed25519' are too open": make the key private to you, on Linux or Windows |
Host not in known_hosts, or its key changed |
Host key verification failed |
A manual ssh shows the same text |
| Port, firewall or slow link | Connection refused or Connection timed out |
A manual ssh -p to the port; raise -T to test slowness |
| Remote command returned 255 | A message about the command | The same command run by hand returns 255 |
Prove the cause before changing anything
These steps do not edit a file you keep, except that steps 1, 3 and 4 connect to the host. Where the host is not yet in known_hosts, ssh asks whether to add its key, and a yes writes known_hosts; answer no unless you have checked the fingerprint. Step 3 opens a real login session; type exit to leave it.
1. Raise the verbosity. The ansible command page says "A reasonable level to start is -vvv, connection debugging might require -vvvv." Use the ping module as the test, as Ansible's getting-started page does with ansible myhosts -m ping -i inventory.ini:
ansible app01 -m ping -i inventory.ini -vvvv
Read the ssh command line it prints and the last lines of ssh's stderr. In the plugin source, a comment says that for other errors no exception is raised and the messages are only logged; the code for an ssh exit code from 1 to 254 logs the message at the -vvv level, and only 255 raises the unreachable result.
2. Print the connection variables Ansible will use. The ansible-inventory page says --host <HOST> outputs "specific host info":
ansible-inventory -i inventory.ini --host app01
Check ansible_host (Ansible's page: "the resolvable name or IP of the host to connect to"), ansible_user ("The username to use when connecting (logging in) to the host."), ansible_port ("The connection port number, if not the default (22 for ssh).") and ansible_ssh_private_key_file. Where ansible_user is not set, Ansible's connection page says it connects "with the username you are using on the control node."
3. Repeat the connection with ssh itself. Use exactly the values from step 2; ssh(1) says -v prints debugging messages and -i selects the identity file:
ssh -v -i ~/.ssh/deploy_key -p 22 ansible@192.0.2.20
If this fails the same way, the fault is outside Ansible. The server-side log and the key list are covered in SSH "Permission denied (publickey)": what the server log says and what to fix.
4. Test for slowness. The ansible page lists -T <TIMEOUT>, --timeout. A longer timeout on one run changes no file:
ansible app01 -m ping -i inventory.ini -T 30
If it passes with 30 and fails with the default of 10 seconds, the host or route is slow to answer.
Fix
Change one thing, then run the ping again.
Correct the inventory entry
Ansible's inventory page lists ansible_host, ansible_port, ansible_user and ansible_ssh_private_key_file as host variables, and shows entries such as aws_host ansible_ssh_private_key_file=/home/example/.ssh/aws.pem.
First save the current state:
cp -p inventory.ini inventory.ini.2026-12-03
Risk: the next edit changes how Ansible connects to app01 for every playbook that uses this inventory. A wrong value points later runs at another user or host; a wrong host value can aim a playbook at a different machine than intended.
Edit inventory.ini so the line for app01 carries the right values, for example:
app01 ansible_host=192.0.2.20 ansible_user=ansible ansible_ssh_private_key_file=/home/build/.ssh/deploy_key
Only if you need to back out, and knowing that any other edit made to the inventory since the save is lost, copy the saved file back and then print the host variables again.
Risk: the next command overwrites inventory.ini with the saved copy, so any other edit made to the inventory since the save is lost.
cp -p inventory.ini.2026-12-03 inventory.ini
Accept the host key after checking it
Ansible's connection page says that if a host is reinstalled with a different key in known_hosts it fails until corrected, and that a new host not yet in known_hosts may prompt for confirmation, an interactive step you may not want from cron. The page also shows host_key_checking = False. The page says "Checking host keys guards against server spoofing and man-in-the-middle attacks", so switching it off removes that guard; our own advice is to leave it on and accept the key by hand once, after you compare the fingerprint ssh shows with one the host's owner gives you.
First save the current state:
cp -p ~/.ssh/known_hosts ~/.ssh/known_hosts.2026-12-03
Risk: the next command opens a real login session to 192.0.2.20 as ansible, and adds the host's key to known_hosts for this user on the control host if you answer yes. A fingerprint that you did not check can add a key for the wrong machine.
Connect once and answer the prompt only if the fingerprint matches; once the login succeeds, type exit to leave the session:
ssh -i ~/.ssh/deploy_key ansible@192.0.2.20
Only if you need to back out, and knowing that every host key added since the save is lost, copy the saved file back.
Risk: the next command overwrites ~/.ssh/known_hosts with the saved copy, so every host key added by any connection made since the save is lost, and those hosts ask for confirmation again. To keep them, delete only the line for 192.0.2.20 by hand instead.
cp -p ~/.ssh/known_hosts.2026-12-03 ~/.ssh/known_hosts
A changed key on a host you did not reinstall is a reason to stop and ask the host's owner, not to edit known_hosts.
Example (illustrative): On ci01, ansible app01 -m ping -i inventory.ini prints UNREACHABLE! ... "msg": "Failed to connect to the host via ssh: deploy@192.0.2.20: Permission denied (publickey).". ansible-inventory --host app01 shows ansible_host 192.0.2.20 and ansible_user deploy. A manual ssh -v -i ~/.ssh/deploy_key deploy@192.0.2.20 fails the same way, while ssh ... ansible@192.0.2.20 works, because the key is installed for the ansible account. The administrator saves the inventory, changes ansible_user to ansible, and the ping returns pong.
Confirm the fix
ansible app01 -m ping -i inventory.inireturns"ping": "pong", the output shown on Ansible's getting-started page.- The playbook that failed runs past its first task on that host.
- A later run with
-vvvshows the ssh options you expect.
What to watch afterwards
- Set
ansible_userand the key in the inventory or group variables instead of relying on the control host's login name. - Keep the key file private to its owner; Ansible calls ssh, so the same key rules apply.
- Treat a remote command that returns 255 as a possible false unreachable.
- Keep
timeoutin mind on slow links; the default is 10.
Read next
Related articles you may want next.
- TroubleshootingSSH "WARNING: UNPROTECTED PRIVATE KEY FILE!" "Permissions 0644 for 'id_ed25519' are too open": make the key private to you, on Linux or WindowsSSH says UNPROTECTED PRIVATE KEY FILE and Permissions are too open? Read the file's mode or ACL, then set 0600 on Linux or trim the ACL on Windows.Read article
- TroubleshootingSSH "Permission denied (publickey)": what the server log says and what to fixSSH says Permission denied (publickey)? Read the sshd log line, then check authorized_keys modes, StrictModes, AllowUsers and accepted key algorithms.Read article
- TroubleshootingSSH "no matching host key type found. Their offer: ssh-rsa": why OpenSSH 8.8 and later refuse an old server, and what to doSSH says no matching host key type found, Their offer: ssh-rsa. Why OpenSSH 8.8 disabled it, and how to reach one old host without weakening the rest.Read article
Risks and limits
- These commands have not been run or lab-tested.
- The table of ssh wording after the colon is our own reading and varies by OpenSSH version.
- Source readings are for ansible-core v2.18.1; other releases may word the message differently.
- Changing the inventory affects every playbook that uses it.
host_key_checking = Falseremoves a security check and is not recommended here.
Sources
- ansible.builtin.ssh connection – connect via SSH client binary — Ansible Core Documentation, 8 October 2026, accessed 3 December 2026
- Connection methods and details — Ansible Core Documentation, 8 October 2026, accessed 3 December 2026
- How to build your inventory — Ansible Core Documentation, 8 October 2026, accessed 3 December 2026
- Building an inventory — Ansible Core Documentation, 8 October 2026, accessed 3 December 2026
- ansible — Ansible Core Documentation, 8 October 2026, accessed 3 December 2026
- ansible-inventory — Ansible Core Documentation, 8 October 2026, accessed 3 December 2026
- ssh.py — ansible source, tag v2.18.1, no date shown, accessed 3 December 2026
- task_executor.py — ansible source, tag v2.18.1, no date shown, accessed 3 December 2026
- default.py — ansible source, tag v2.18.1, no date shown, accessed 3 December 2026
- ssh(1) — OpenBSD manual pages, no date shown, accessed 3 December 2026
This article was originally published on .