Ansible - SSH Known Host Keys

less than 1 minute read

I wanted to throw this together mainly for my own reference but maybe it will help someone else as well. I had a need to add every host’s ssh keys to every host so that every host knew what every other hosts ssh keys were. After a bit of attempting many different things below is what I came up with. And it works.

First create a simple playbook:

Next create this simple template:

Then run

ansible-playbook -i yourinventoryfile ssh-keys.yml

and it will run through each host and capture their respective ssh key and then create /etc/ssh/ssh_known_hosts on each host including all other hosts ssh keys as well. Pretty simple after quite a bit of trial and error but it does work.


Leave a Comment