Ansible - BadHostKeyException

When executing an ansible playbook, you may get the following error:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: paramiko.BadHostKeyException: Host key for server switch-name does not match!

This probably happened after you changed the name of your network device, and generated new RSA keys. Or, if you are using DNS device names in ansible, resolve the name of your network device from your ansible server and verify the DNS resolution is correct. To fix the issue, edit your "known_hosts" file and remove the key associated with the network device throwing the error:

  1. Open your known_hosts file
vi ~/.ssh/known_hosts
  1. Search for the line with the network device RSA key
/network_device_name
  1. Delete the RSA key for that network device ("dd" deletes the entire line)
dd
  1. Save the known_hosts file
#Press the escape key first
:wq

If you don't want ansible to perform this check, edit your ansible.cfg file with:

host_key_checking = False