Batch manage and version host keys with ansible
$ ansible-playbook tool_known_hosts.yml
---
- hosts: localhost
become: no
tasks:
- with_fileglob: files/host_keys/*.com
lineinfile:
dest: ~/.ssh/known_hosts
regexp: '{{ item | basename }}'
line: '{{ lookup("file", item) }}'
#!/usr/bin/env php
<?php
$domain = 'example.com';
$hosts = [
'web',
'db',
'redis',
'test',
];
foreach ($hosts as $host) {
$fqdn = "$host.$domain";
$ip = gethostbyname($fqdn);
$key = exec('ssh-keyscan '.escapeshellarg("$fqdn,$ip").' 2>/dev/null');
file_put_contents(__DIR__.'/'.$fqdn, $key);
}