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

时间:2020-04-03 02:14:57

标签: jenkins ssh ansible centos ssh-keys

我在执行詹金斯(Ansible-playbook)形式的ansible-playbook时遇到问题,

like:

PLAY [centos-slave-02] *********************************************************



TASK [Gathering Facts] *********************************************************

fatal: [centos-slave-02]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Host key verification failed.", "unreachable": true}

PLAY RECAP *********************************************************************

centos-slave-02            : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   

但是我能够得到乒乓球的回应以及每次它的要求

Matching host key in /var/jenkins_home/.ssh/known_hosts:5  :
jenkins@c11582cb5024:~/jenkins-ansible$ ansible -i hosts -m ping centos-slave-02

Warning: the ECDSA host key for 'centos-slave-02' differs from the key for the IP address '172.19.0.3'

Offending key for IP in /var/jenkins_home/.ssh/known_hosts:2

Matching host key in /var/jenkins_home/.ssh/known_hosts:5

Are you sure you want to continue connecting (yes/no)? yes

centos-slave-02 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": false,

    "ping": "pong"

}

任何人都可以解决此问题!谢谢!

2 个答案:

答案 0 :(得分:2)

您在known_hosts主机中的jenkins-ansible文件中已经有主机centos-slave-02的条目。现在centos-slave-02主机的身份已更改,需要添加一个新条目。但是文件中的现有条目将引发此警告。

Warning: the ECDSA host key for 'centos-slave-02' differs from the key for the IP address '172.19.0.3'

Offending key for IP in /var/jenkins_home/.ssh/known_hosts:2

Matching host key in /var/jenkins_home/.ssh/known_hosts:5

您可以手动编辑/var/jenkins_home/.ssh/known_hosts文件以删除此centos-slave-02主机的密钥,也可以运行以下命令,

ssh-keygen -R centos-slave-02

ansible的解决方法是在ansible.cfg下的[defaults]中添加此行,

[defaults]
host_key_checking = False

这将在建立SSH连接时禁用HostKeyChecking

答案 1 :(得分:0)

确保在jenkin build Exec命令中不使用sudo。