Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to connect to the host via ssh: Host key verification failed.\r\n

Getting error when i try to connect to hosts in ansible

I am able to ping ssh_connection in local host (ansible), but not in jenkins pipeline build.

I have tried almost everything.

----------
[FPipeline] Running shell script
+ ansible all -m ping -vvv
ansible 2.4.0.0
config file = /var/lib/jenkins/workspace/FPipeline/ansible.cfg
configured module search path = 
[u'/var/lib/jenkins/.ansible/plugins/modules', 
u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 
(Red Hat 4.8.5-16)]
Using /var/lib/jenkins/workspace/FPipeline/ansible.cfg as config file
Parsed /etc/ansible/hosts inventory source with ini plugin
META: ran handlers
Using module file /usr/lib/python2.7/site-
packages/ansible/modules/system/ping.py
<localhost> ESTABLISH SSH CONNECTION FOR USER: None
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 
KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-
mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 
ConnectTimeout=10 -o ControlPath=/var/lib/jenkins/.ansible/cp/8a5a4c6a60 
localhost '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
Using module file /usr/lib/python2.7/site-
packages/ansible/modules/system/ping.py
<192.168.219.131> ESTABLISH SSH CONNECTION FOR USER: None
<192.168.219.131> SSH: EXEC ssh -C -o ControlMaster=auto -o 
ControlPersist=60s -o KbdInteractiveAuthentication=no -o 
PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o 
PasswordAuthentication=no -o ConnectTimeout=10 -o 
ControlPath=/var/lib/jenkins/.ansible/cp/81147ff3f7 192.168.219.131 '/bin/sh 
-c '"'"'echo ~ && sleep 0'"'"''
<192.168.219.131> (255, '', 'Host key verification failed.\r\n')
server1 | UNREACHABLE! => {
"changed": false, 
"msg": "Failed to connect to the host via ssh: Host key verification  
failed.\r\n", 
"unreachable": true
}
<localhost> (255, '', 'Host key verification failed.\r\n')
localhost | UNREACHABLE! => {
"changed": false, 
"msg": "Failed to connect to the host via ssh: Host key verification 
failed.\r\n", 
"unreachable": true
}

----------

Can you please help me regarding this...

like image 605
Kumar Avatar asked Oct 25 '17 10:10

Kumar


4 Answers

In your ansible config file /var/lib/jenkins/workspace/FPipeline/ansible.cfg add below line and test it again.

[defaults]
host_key_checking = False
like image 188
sfgroups Avatar answered Nov 04 '22 02:11

sfgroups


Another way to disable host key checking in ansible is by setting the ANSIBLE_HOST_KEY_CHECKING environment variable to False

export ANSIBLE_HOST_KEY_CHECKING=False

Ansible Docs

like image 31
kjpc-tech Avatar answered Nov 04 '22 04:11

kjpc-tech


Instead of disabling the host key check completely I would recommend to add all hosts once (you should verify the SSH fingerprints). You need to run these commands with the user who initiates the SSH connection (in your case probably the Jenkins user):

# remove any old fingerprints for the host
ssh-keygen -R server.example.com
# add SSH fingerprints for the host
ssh-keyscan -t ecdsa,ed25519 -H server.example.com >> ~/.ssh/known_hosts 2>&1
like image 2
jansohn Avatar answered Nov 04 '22 04:11

jansohn


Was the machine rebuilt since it first connected to it?

If so you need to remove the host from ~/.ssh/known_hosts or add the following option to SSH: StrictHostKeyChecking=no.

like image 1
Paul Burrows Avatar answered Nov 04 '22 03:11

Paul Burrows