Ubuntu – Ansible host “unreachable”: true

ansibleansible-playbooksshUbuntu

I just started learning the basics of Ansible. And facing some problem related to server connectivity via SSH. I created a folder named publich_html and put my code into that folder and changed the permission. When I try to execute my playbook it gives me the following error

UNREACHABLE! => {"changed": false, "msg": "SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh", "unreachable"

Whereas when I try to ping servers from user home or root user without going into a directory it pings perfectly. Here is the verbose output :

SSH: EXEC sshpass -d15 sftp -b – -C -vvv -o ForwardAgent=yes -o StrictHostKeyChecking=no -o User=root -o ConnectTimeout=10

config file is:

[default]
host_key_checking = False

I don't know where I am missing something. Anyone have any idea about it?

Best Answer

I recently resolved a similar issue, where Ansible would encounter "UNREACHABLE!" errors only for tasks requiring file transfers (e.g. copy, template) while connecting just fine for anything else. I resolved it by adding the following directive to ansible.cfg, under the [ssh_connection] section:

scp_if_ssh = True

As you might guess from that fix, the issue was related to the target servers' SFTP configuration. Since Ansible uses SFTP by default to transfer files, the misconfigured servers encountered errors for any Ansible tasks that involved file transfers. So, if this change makes the errors go away, I recommend checking your target servers' sshd_config files for errors in the SFTP configuration.