ssh_authorized_key
我决定不使用资源,而是定义一个authorized_keys
资源,该资源采用单个用户的所有SSH密钥的列表。定义看起来像这样:
define authorized_keys ($sshkeys, $ensure = "present", $home = '') {
# This line allows default homedir based on $title variable.
# If $home is empty, the default is used.
$homedir = $home ? {'' => "/home/${title}", default => $home}
file {
"${homedir}/.ssh":
ensure => "directory",
owner => $title,
group => $title,
mode => 700,
require => User[$title];
"${homedir}/.ssh/authorized_keys":
ensure => $ensure,
owner => $ensure ? {'present' => $title, default => undef },
group => $ensure ? {'present' => $title, default => undef },
mode => 600,
require => File["${homedir}/.ssh"],
content => template("authorized_keys.erb");
}
}
$ssh_keys
参数将所有必要的键作为列表。该authorized_keys.erb
模板看起来是这样的:
# NOTICE: This file is autogenerated by Puppet and should not be modified
<% sshkeys.each do |key| -%>
<%= key %>
<% end -%>
用法
user {'mikko':
...
}
authorized_keys {'mikko':
sshkeys => [
'ssh-rsa XXXXXXYYYYYYYYYZZZZZZZZZ mikko@domain.tld',
'ssh-rsa XXXXXXZZZZZZZZZHHHHHHHHH mikko@other-host.tld',
],
}
有条件地添加SSH密钥(例如在不同的类中)也很容易,这要归功于Puppet的+>
运算符:
Authorized_keys <| title == 'mikko' |> {
sshkeys +> 'ssh-rsa ASDFASDFASDFASDF mikko@somewhere-else.tld'
}
使用此方法,用户将永远不会拥有未在Puppet配置中明确指定的密钥。密钥字符串按原样在authorized_keys中使用,因此添加选项和限制很简单。
我很高兴听到别人是否成功使用了此方法!