SSH ok but Ansible returns "unreachable"

11,712

Solution 1

Ansible returns "unreachable" for the SFTP connection, not SSH.

Either enable SFTP on the target node (or a firewall in-between), or configure Ansible to use SCP in ansible.cfg:

scp_if_ssh = True

Solution 2

I had a similar "unreachable" error, but in my case it was because my playbook file specified the host this way:

[webservers]
[email protected]

This worked for us in the past, so presumably this works with some Ansible versions, but not with my version (2.0.0.2). Instead I changed this to what the documentation recommends:

[webservers]
123.456.789.111 ansible_user=ubuntu

and now the SFTP connection does not fail.

Solution 3

After many years of try and error, now I always have these setting on my ansible.cfg:

[defaults]
host_key_checking = false

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null -o ServerAliveInterval=20
scp_if_ssh = True

[connection]
pipelining = true
  • The pipelining is my personal preference when dealing with multiple hosts.
  • The ssh_args deals with hangs and timeouts, useful when your target remote has unstable connection.
Share:
11,712
Paul
Author by

Paul

Updated on June 09, 2022

Comments

  • Paul
    Paul almost 2 years

    My SSH using keys is set up properly.

    ssh [email protected]
    admin@DiskStation:~$
    

    But Ansible returns an error:

    TASK [setup] *******************************************************************
    <192.168.1.111> ESTABLISH SSH CONNECTION FOR USER: admin
    <192.168.1.111> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=admin -o ConnectTimeout=10 -o ControlPath=/Users/Shared/Jenkins/.ansible/cp/ansible-ssh-%h-%p-%r 192.168.1.111 '/bin/sh -c '"'"'( umask 22 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1479205446.3-33100049148171 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1479205446.3-33100049148171 `" )'"'"''
    <192.168.1.111> PUT /var/folders/pd/8q63k3z93nx_78dggb9ltm4c00007x/T/tmpNJvc43 TO /var/services/homes/admin/.ansible/tmp/ansible-tmp-1479205446.3-33100049148171/setup
    <192.168.1.111> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=admin -o ConnectTimeout=10 -o ControlPath=/Users/Shared/Jenkins/.ansible/cp/ansible-ssh-%h-%p-%r '[192.168.1.111]'
    fatal: [192.168.1.111]: 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": true}
    

    Can someone help me?