Ubuntu

Ansible - SSH錯誤:數據無法發送到遠端主機

  • April 11, 2020

我正在嘗試使用Ansible 2.7.7來管理Ubuntu 16.04 cloud image,但是當我嘗試建立連接時出現錯誤:

SSH Error: data could not be sent to remote host "192.168.111.11". Make sure this host can be reached over ssh

如果我ssh ubuntu@192.168.111.11在 Ansible 主機上發出命令,我可以毫無問題地連接到目標伺服器。我也可以sudo在那裡沒有密碼提示。

如果查看目標機器的日誌,我會看到 Ansible 成功登錄到機器:

Feb 20 11:44:51 ubuntu sshd[2870]: Accepted publickey for ubuntu from 192.168.111.111 port 56236 ssh2: RSA SHA256:yI...
Feb 20 11:44:51 ubuntu sshd[2870]: pam_unix(sshd:session): session opened for user ubuntu by (uid=0)
Feb 20 11:44:51 ubuntu systemd-logind[1090]: New session 13 of user ubuntu.
Feb 20 11:44:51 ubuntu sudo:   ubuntu : TTY=unknown ; PWD=/home/ubuntu ; USER=root ; COMMAND=/bin/sh -c echo BECOME-SUCCESS-kldfjkldjhjljkl; /usr/bin/python
Feb 20 11:44:51 ubuntu sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Feb 20 11:44:51 ubuntu sudo: pam_unix(sudo:session): session closed for user root

詳細輸出:

ansible-playbook -i inventory default.yaml -vvv
ansible-playbook 2.7.7
 config file = /home/admin/ansible/ansible.cfg
 configured module search path = [u'/home/admin/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
 ansible python module location = /home/admin/ansible/ansible-env/local/lib/python2.7/site-packages/ansible
 executable location = /home/admin/ansible/ansible-env/bin/ansible-playbook
 python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]
Using /home/admin/ansible/ansible.cfg as config file
/home/admin/ansible/inventory did not meet host_list requirements, check plugin documentation if this is unexpected
/home/admin/ansible/inventory did not meet script requirements, check plugin documentation if this is unexpected
Parsed /home/admin/ansible/inventory inventory source with ini plugin

PLAYBOOK: default.yaml *******************************************************************************************************************************************************************************************************************
1 plays in default.yaml

PLAY [default] ***************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
task path: /home/admin/ansible/default.yaml:3
Using module file /home/admin/ansible/ansible-env/local/lib/python2.7/site-packages/ansible/modules/system/setup.py
<192.168.111.11> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<192.168.111.11> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -o ControlPath=/home/admin/.ansible/cp/a9e60c69b0 192.168.111.11 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-tenlfcstwmojszikegdyatixrzpwqwah; /usr/bin/python'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded
fatal: [192.168.111.11]: UNREACHABLE! => {
   "changed": false,
   "unreachable": true
}

MSG:

SSH Error: data could not be sent to remote host "192.168.111.11". Make sure this host can be reached over ssh

什麼可能導致此問題?

原來我的目標系統tty在使用時需要sudo

我能夠將我從以下位置註釋掉的這一行聯繫起來ansible.cfg

pipelining = True

requiretty其他選項是從 sudoer 文件中禁用設置。

詳細說明@Michael Hampton 評論和。Ansible 正在掩蓋 ssh 錯誤。

在我的情況下,真正的錯誤,顯示為**-vvvv**的詳細級別是:Host key verification failed.我在這裡找到了解決方案:

https://unix.stackexchange.com/questions/416166/cant-establish-ssh-connection-debug1-read-passphrase-cant-open-dev-tty-n#answer-416186

基本上,提供 ssh 選項:-o StrictHostKeyChecking=no

引用自:https://serverfault.com/questions/954877