我可以使用 ssh 成功连接到我的服务器。然而,当我尝试通过 Ansible cli 执行命令时,它失败了:
ansible en2 -a "/sbin/reboot" -vvv
Using /etc/ansible/ansible.cfg as config file
<en2> ESTABLISH SSH CONNECTION FOR USER: None
<en2> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/buzut/.ansible/cp/ansible-ssh-%h-%p-%r en2 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1470744750.87-182962282383475 `" && echo ansible-tmp-1470744750.87-182962282383475="` echo $HOME/.ansible/tmp/ansible-tmp-1470744750.87-182962282383475 `" ) && sleep 0'"'"''
<en2> PUT /tmp/tmpampMBF TO /root/.ansible/tmp/ansible-tmp-1470744750.87-182962282383475/command
<en2> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/buzut/.ansible/cp/ansible-ssh-%h-%p-%r '[en2]'
<en2> ESTABLISH SSH CONNECTION FOR USER: None
<en2> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/buzut/.ansible/cp/ansible-ssh-%h-%p-%r -tt en2 '/bin/sh -c '"'"'LANG=fr_FR.UTF-8 LC_ALL=fr_FR.UTF-8 LC_MESSAGES=fr_FR.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1470744750.87-182962282383475/command; rm -rf "/root/.ansible/tmp/ansible-tmp-1470744750.87-182962282383475/" > /dev/null 2>&1 && sleep 0'"'"''
en2 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh.",
"unreachable": true
}
答案1
您的输出中的所有内容看起来都是正确的。
Ansible 建立 SSH 连接,运行其脚本(包含您的命令)并等待其退出代码。
但是你命令它发出一个/sbin/reboot
重启服务器的命令。因此在退出代码返回给 Ansible 并且 Ansible 报告错误之前,SSH 连接就中断了。
该错误信息实际上有点误导,但它意味着连接已断开。