无法通过ssh连接到主机: 主机密钥验证失败。

18

在使用ansible连接主机时,我遇到了错误。

我能够在本地主机(ansible)上ping通ssh_connection,但在jenkins流水线构建中却不能。

我已经尝试了几乎所有方法。

----------
[FPipeline] Running shell script
+ ansible all -m ping -vvv
ansible 2.4.0.0
config file = /var/lib/jenkins/workspace/FPipeline/ansible.cfg
configured module search path = 
[u'/var/lib/jenkins/.ansible/plugins/modules', 
u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 
(Red Hat 4.8.5-16)]
Using /var/lib/jenkins/workspace/FPipeline/ansible.cfg as config file
Parsed /etc/ansible/hosts inventory source with ini plugin
META: ran handlers
Using module file /usr/lib/python2.7/site-
packages/ansible/modules/system/ping.py
<localhost> ESTABLISH SSH CONNECTION FOR USER: None
<localhost> 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 
ConnectTimeout=10 -o ControlPath=/var/lib/jenkins/.ansible/cp/8a5a4c6a60 
localhost '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
Using module file /usr/lib/python2.7/site-
packages/ansible/modules/system/ping.py
<192.168.219.131> ESTABLISH SSH CONNECTION FOR USER: None
<192.168.219.131> 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 ConnectTimeout=10 -o 
ControlPath=/var/lib/jenkins/.ansible/cp/81147ff3f7 192.168.219.131 '/bin/sh 
-c '"'"'echo ~ && sleep 0'"'"''
<192.168.219.131> (255, '', 'Host key verification failed.\r\n')
server1 | UNREACHABLE! => {
"changed": false, 
"msg": "Failed to connect to the host via ssh: Host key verification  
failed.\r\n", 
"unreachable": true
}
<localhost> (255, '', 'Host key verification failed.\r\n')
localhost | UNREACHABLE! => {
"changed": false, 
"msg": "Failed to connect to the host via ssh: Host key verification 
failed.\r\n", 
"unreachable": true
}

----------

你能帮我处理一下这个问题吗?...

7个回答

20

在您的ansible配置文件/var/lib/jenkins/workspace/FPipeline/ansible.cfg中添加下面的行,并再次测试。

[defaults]
host_key_checking = False

10

另一种禁用 ansible 中主机密钥检查的方法是将 ANSIBLE_HOST_KEY_CHECKING 环境变量设置为 False

export ANSIBLE_HOST_KEY_CHECKING=False

Ansible 文档


5

不建议完全禁用主机密钥检查,建议先添加所有主机(需要验证SSH指纹)。您需要使用发起SSH连接的用户运行这些命令(在您的情况下可能是Jenkins用户):

# remove any old fingerprints for the host
ssh-keygen -R server.example.com
# add SSH fingerprints for the host
ssh-keyscan -t ecdsa,ed25519 -H server.example.com >> ~/.ssh/known_hosts 2>&1

1
机器自首次连接以来是否已经重新构建过?
如果是的话,您需要从~/.ssh/known_hosts中删除主机,或将以下选项添加到SSH中:StrictHostKeyChecking=no

1
你可以使用Jenkins的ansible插件来完成此操作。他们的文档在主机密钥检查方面有误,但这里有一个完整的工作示例
以下是解决方案的重要片段:
ansiColor('xterm') {
    ansiblePlaybook(
        playbook: './ansible/playbook.yml',
        inventory: '/tmp/inventory.ini',
        credentialsId: 'your-jenkins-pk-credential',
        disableHostKeyChecking: true,
        colorized: true)
    }
}

0

Jenkins提供了禁用SSH主机密钥检查的选项。

  1. 在构建步骤底部,点击[高级]。
  2. 勾选“禁用SSH主机密钥检查”前面的框。

-1

对于 Debian (Ubuntu)

检查 authorized_keys 文件的文件权限

使用这个命令来检查文件权限 sudo ls -ld /root/.ssh/authorized_keys

使用这个命令更改文件权限 sudo chmod u=rw,og=r /root/.ssh/authorized_keys

就是这样...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接