问题背景

当设置好ansible的hosts后,尝试用ansible去连接host里设置的主机。

我使用的是162主机去连接163主机:

如果你之前从来没有连接过163主机,执行后会报错

[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts web-servers -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to
see details
192.168.1.163 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."

问题原因

这是因为你控制端和被控制端第一次通信时,需要添加指纹

解决方案

有两种解决方案:

方法1 . 可以直接修改配置文件
vim /etc/ansible/ansible.cfg
中的 host_key_checking = False 的#去掉即可

ansible:Using a SSH password instead of a key is not possible because Host Key checking_运维

然后在执行命令

[root@itlaoxin162 ~]# ansible -i /etc/ansible/hosts web-servers -m ping
[WARNING]: Invalid characters were found in group names but not replaced, us
see details
192.168.1.163 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

可以看到已经执行成功了。

方法2:直接登陆SSH,留下指纹即可

使用ssh 登陆远程主机,只要登陆过一次,下次就不会报错。