Skip to content

Instantly share code, notes, and snippets.

@acidprime
Last active January 21, 2016 00:02
Show Gist options
  • Save acidprime/f43dce513298ba6380e5 to your computer and use it in GitHub Desktop.
Save acidprime/f43dce513298ba6380e5 to your computer and use it in GitHub Desktop.
Create known hosts for r10k ( when not using rugged )
#!/usr/bin/ruby
require 'rubygems'
require 'puppet'
require 'yaml'
# Have puppet parse its config so we can call its settings
Puppet.initialize_settings
# Use puppet to manage host keys
def add_known_host(user, homedir, hostname)
ssh_dir = "#{homedir}/.ssh/"
known_hosts = "#{ssh_dir}/known_hosts"
keyscan = `ssh-keyscan -p 2222 #{hostname}`.split
status = $?.exitstatus
if status == 0
FileUtils.mkdir_p(ssh_dir)
FileUtils.touch(known_hosts)
if user != "root"
FileUtils.chown(user, nil, [ssh_dir, known_hosts])
end
raise "keyscan returned empty result for (#{hostname})" if keyscan.empty?
sshkey = Puppet::Resource.new('sshkey', keyscan[0], :parameters => {
:ensure =>'present',
:target => known_hosts,
:type => keyscan[1],
:key => keyscan[2],
})
result, report = Puppet::Resource.indirection.save(sshkey)
else
puts "ssh-keyscan failed for host #{hostname}, check your network conection"
exit 1
end
end
sources = YAML.load_file('/etc/r10k.yaml')['sources']
sources.each do |prefix,source|
hostname = /ssh:\/\/gitosis@(.*):[1-9]+\/puppet.*/.match(source['remote'])[1]
puts "Processing source #{hostname}"
add_known_host('root','/root',hostname)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment