Ansible部署時跑出SSH encountered an unknown error during the connection

今天用Ansible-playbook部署環境時遇到SSH encountered an unknown error during the connection這樣一個錯誤
但是這個playbook使用已久,今天用的時候突然跑出這錯誤感到納悶,後來狗了一下找到了原因,這邊做個記錄

狀況如下
今天我有三台機器
- example1.com
- example2.com
- example3.com

在埋好ssh key之後運行playbook時出現了如下的錯誤

TASK [setup] *******************************************************************
The authenticity of host 'example1.com' can't be established.
RSA key fingerprint is 91:dd:be:80:dd:bd:6d:9f:77:1f:7f:9c:06:7e:16:fa.
Are you sure you want to continue connecting (yes/no)? The authenticity of host 'example2.com' can't be established.
RSA key fingerprint is 91:dd:be:80:dd:bd:6d:9f:77:1f:7f:9c:06:7e:16:fa.
Are you sure you want to continue connecting (yes/no)? yes ok: [example1.com]

Please type 'yes' or 'no': yes
Please type 'yes' or 'no':
Please type 'yes' or 'no':
fatal: [Cexample2.com]: UNREACHABLE! => {"changed": false, "msg": "SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue", "unreachable": true}

fatal: [example3.com]: UNREACHABLE! => {"changed": false, "msg": "SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue", "unreachable": true}

只有example1.com成功,其他兩台都失敗了.但是這在以前沒有發生過.腳本也都是同一個
後來細查了一下SSH encountered an unknown error during the connection這個錯誤
才發現是被ssh host key check的機制弄失敗了
追究了一下原因是ansible部署的機器換過,以前的部署機器有用StrictHostKeyChecking這個設定去避開
新的部署機器不知怎麼的StrictHostKeyChecking的設定沒加上去

這個錯誤解法有三個

設定ANSIBLE_HOST_KEY_CHECKING

在ansible環境變數上設定跳過

$ export ANSIBLE_HOST_KEY_CHECKING=False

修改~/.ansible.cfg

在ansible的設定檔上設定不要檢查

~/.ansible.cfg
[defaults]
host_key_checking = False

設定StrictHostKeyChecking

比較常用的做法是在部署機器的~/.ssh/config上加上StrictHostKeyChecking設定

~/.ssh/config
Host *
    StrictHostKeyChecking no

後來選擇了方案3,StrictHostKeyChecking設定加上去之後就解決了

comments powered by Disqus