【问题标题】:Failed to connect to the host via ssh: Host key verification failed.\r\n无法通过 ssh 连接到主机: 主机密钥验证失败。\r\n
【发布时间】:2018-04-06 09:04:50
【问题描述】:

当我尝试连接到 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
}

----------

你能帮我解决这个问题吗...

【问题讨论】:

    标签: linux jenkins ssh ansible


    【解决方案1】:

    机器是否在首次连接后重建?

    如果是这样,您需要从 ~/.ssh/known_hosts 中删除主机或将以下选项添加到 SSH:StrictHostKeyChecking=no

    【讨论】:

      【解决方案2】:

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

      [defaults]
      host_key_checking = False
      

      【讨论】:

        【解决方案3】:

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

        export ANSIBLE_HOST_KEY_CHECKING=False

        Ansible Docs

        【讨论】:

          【解决方案4】:

          您可以使用 Jenkins ansible 插件来做到这一点。他们的文档对主机密钥检查有错误,但here is a full working example

          这是重要的 sn-p 分辨率:

          ansiColor('xterm') {
              ansiblePlaybook(
                  playbook: './ansible/playbook.yml',
                  inventory: '/tmp/inventory.ini',
                  credentialsId: 'your-jenkins-pk-credential',
                  disableHostKeyChecking: true,
                  colorized: true)
              }
          }
          

          【讨论】:

            【解决方案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
            

            【讨论】:

              【解决方案6】:

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

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

              【讨论】:

                猜你喜欢
                • 2020-07-15
                • 2021-07-03
                • 2012-01-23
                • 2019-10-21
                • 1970-01-01
                • 1970-01-01
                • 2014-01-31
                • 2019-12-08
                • 2017-09-10
                相关资源
                最近更新 更多