Collect SSH Keys with Ansible
Here’s a common scenario I’m encountering on Ansible-related forums:
Q: I cannot connect to network devices with my Ansible network modules. I keep getting these weird error messages…
Me: Are you sure you have the device SSH keys in known_hosts file?
Q: How did you know?
Me: Been there, done that…
I’m describing the challenge and the potential solutions in more details in the Ansible Networking Modules part of Ansible for Networking Engineers webinar and online course.
In my case, the situation got really annoying because I’m using Cisco IOS devices running in VIRL to test my Ansible scripts, and there’s absolutely no way to retain the router SSH keys across device or VIRL reloads.
Finally, I found an Ansible playbook that used ssh-keyscan to collect SSH keys. It was easy to adapt it to collect keys from managed devices and store them in known_hosts file on your Ansible host.
In case you’re wondering how that playbook works, I described it in details in the Sample Ansible Playbooks section of Ansible for Networking Engineers online course.
This is an slight outdated but still valid example on Ubuntu:
https://www.digitalocean.com/community/tutorials/how-to-create-an-ssh-ca-to-validate-hosts-and-clients-with-ubuntu
Getting your host keys signed and put in the right place is only a matter of a few steps in Ansible :-)