Skip to content

Instantly share code, notes, and snippets.

@supercow
Last active December 26, 2015 06:09
Show Gist options
  • Save supercow/7105496 to your computer and use it in GitHub Desktop.
Save supercow/7105496 to your computer and use it in GitHub Desktop.
supercow@stout:~$ puppet apply /tmp/test.pp
Notice: Compiled catalog for stout.4c in environment production in 0.05 seconds
Notice: /Stage[main]//Ssh_authorized_key[supercow@doppelbock]/ensure: current_value absent, should be present (noop)
Notice: /Stage[main]//Ssh_authorized_key[supercow@Kriek]/ensure: current_value absent, should be present (noop)
Notice: /Stage[main]//Ssh_authorized_key[supercow@stout]/ensure: current_value absent, should be present (noop)
Notice: Class[Main]: Would have triggered 'refresh' from 3 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.03 seconds
# Returns a hash of an ssh_authorized_key resource from a file
Puppet::Parser::Functions::newfunction(
:sshauth_to_hash, :type => :rvalue,
:doc => "Returns an array of hashes suitable for create_resources to create ssh_authorized_key resources"
) do |vals|
ret = nil
file = vals[0]
unless Puppet::Util.absolute_path?(file)
raise Puppet::ParseError, "Files must be fully qualified"
end
if FileTest.exists?(file)
keyfile = File.read(file)
ret = Hash.new
keyfile.each do |str_key_line|
key_line = str_key_line.split(/\s/)
key = Hash.new
key['ensure'] = 'present'
key['type'] = key_line[0]
key['key'] = key_line[1]
ret[key_line[2]] = key
end
end
if ret
ret
else
raise Puppet::ParseError, "Could not find any files from #{vals.join(", ")}"
end
end
$keys = sshauth_to_hash('/home/supercow/.ssh/authorized_keys')
create_resources('ssh_authorized_key',$keys,{ 'noop' => true, 'user' => 'root' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment