require 'net/http'
module Puppet::Parser::Functions
newfunction(:sshkeyfromdb, :type => :rvalue) do |args|
ssh_keys_file = "/tmp/sshkeys"
host = "puppet"
url = "/setting/ssh_keys"
ttl = (60 * 60 * 24)
if not File.exist?(ssh_keys_file) or File.mtime(ssh_keys_file) < (Time.now - ttl)
keys = Net::HTTP.get host,url
File.new(ssh_keys_file,"w", 0600).write(keys)
keys = Marshal.load keys
else
keys = Marshal.load File.open(ssh_keys_file).read
end
case args[0].downcase
when nil
warn "Must supply key type - RSA or DSA"
exit 1
when "hosts"
hosts = Array.new
keys.each_key do |fqdn|
ipaddress = keys[fqdn]["ipaddress"]
hosts << [fqdn, ipaddress, 'ssh-rsa', keys[fqdn]["rsa"]] if keys[fqdn].has_key?('rsa')
hosts << [fqdn, ipaddress, 'ssh-dss', keys[fqdn]["dsa"]] if keys[fqdn].has_key?('dsa')
end
return hosts
end
end
end
USAGE:
class ssh::knownhosts {
$ssh_hosts = sshkeyfromdb(hosts)
file{"/etc/ssh/ssh_known_hosts":
owner => root,
group => root,
mode => 644,
content => template("ssh/known_hosts.erb"),
schedule => "maint",
require => Schedule["maint"]
}
}
template:
<%# generate ssh keys from sshkeyfromdb function, not using native sshkey type do to performance difference %>
<% ssh_hosts.each do |host|
fqdn, ipaddress, key_type, key = host[0], host[1], host[2], host[3]
hostname = fqdn.split('.')[0] -%>
<%="#{hostname},
<%end -%>