我正在尝试使用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
如果我在 Ansible 主机上发出命令,我可以毫无问题地连接到目标服务器。我也可以在那里不输入密码。ssh [email protected]
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
什么原因导致了这个问题?
答案1
事实证明,tty
使用时我的目标系统是必需的sudo
。
我能够通过注释掉以下行来建立联系ansible.cfg
:
pipelining = True
其他选项是禁用requiretty
sudoer 文件中的设置。
答案2
更详细地阐述@Michael Hampton 的评论。Ansible 正在屏蔽 ssh 错误。
在我的例子中,真正的错误以详细级别显示-vvvv是:Host key verification failed.
我在这里找到了解决方案:
基本上,提供 ssh 选项:-o StrictHostKeyChecking=no
答案3
在 ansible.cfg 中添加这几行对我来说解决了这个问题。[source_link][1]
[ssh_connection]
scp_if_ssh = smart
transfer_method = smart