避免 Ansible 无法存取第一次登录的 Server
问题
最近使用 ansible
批量管理服务器,这批机器都没在我本机登录过,在首次运行 ansible-playbook
操作时出现报错:
1 | fatal: [cache_01]: 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."} |
原因
ssh
在第一次连接的时候一般会提示输入 yes
进行确认并把 ssh
信息加入到 ~/.ssh/known_hosts
文件中。因为报错的机器从未在本机登录过,所以并没有对应的 ssh
信息才导致报错。
解决方法
方法1
在 ansible.cfg
文件中更改下面的参数:
1 | #host_key_checking = False 将#号去掉即可 |
方法2 (亲测有效)
添加环境变量,跳过 fingerprint
检查, 例如我本机使用的是 zsh
则编辑 ~/.zshrc
文件,添加以下信息:
1 | export ANSIBLE_HOST_KEY_CHECKING=False |
引用
- Ansible provisioning ERROR! Using a SSH password instead of a key is not possible
- ansible不配置ssh免密钥,使用密码登录
以上。