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

pra*_*een 3 ssh centos ssh-keys jenkins ansible

我在执行 Jenkins 的 ansible-playbook 时遇到问题,

喜欢 :

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   
Run Code Online (Sandbox Code Playgroud)

但我能够得到乒乓响应,每次都要求

Matching host key in /var/jenkins_home/.ssh/known_hosts:5  :
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
centos-slave-02 | SUCCESS => {

    "ansible_facts": {

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

    },

    "changed": false,

    "ping": "pong"

}

Run Code Online (Sandbox Code Playgroud)

任何人都可以解决这个问题!提前致谢。

fra*_*ijo 5

您在主机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
Run Code Online (Sandbox Code Playgroud)

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

ssh-keygen -R centos-slave-02
Run Code Online (Sandbox Code Playgroud)

ansible.cfgansible 的解决方法是在下面的部分添加这一行[defaults]

[defaults]
host_key_checking = False
Run Code Online (Sandbox Code Playgroud)

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

  • 请记住,禁用 HostKeyChecking 确实是不好的做法。最正确的做法是设置 SSH 用户/主机证书。 (2认同)