在Puppet中,我制作了自己的模块,该模块将管理员帐户添加到我们的管理服务器中。
class admins::add_admin($username, $userkey) {
$username.each |String $username| {
file { "/home/${username}":
ensure => directory,
mode => '0750',
owner => $username,
}
user { $username:
ensure => present,
shell => '/bin/bash',
}
ssh_authorized_key { $username:
ensure => present,
user => $username,
type => 'ssh-rsa',
key => $userkey,
}
}
}
$ username是所需用户名的数组,$ userkeys是ssh键的数组。
运行每个循环时,将相应地创建用户,但是每个用户的键都相同(这是合乎逻辑的,因为我还没有迭代用户键的功能)。
我想要的是这个Puppet模块在两个数组上进行迭代,但是我不知道该怎么做。
答案 0 :(得分:2)
您可以这样操作:
class admins::add_admin (
Array[Hash[String, String]] $users_data = [], # declare data type and defaults
) {
$users_data.each |Hash $user| {
$username = $user['username']
$userkey = $user['userkey']
file { "/home/${username}":
ensure => directory,
mode => '0750',
owner => $username,
}
user { $username:
ensure => present,
shell => '/bin/bash',
}
ssh_authorized_key { $username:
ensure => present,
user => $username,
type => 'ssh-rsa',
key => $userkey,
}
}
}
然后您将在其中传递数据,如下所示:
class { 'admins::add_admin':
users_data => [
{
'username' => 'bill',
'userkey' => 'keydata1',
},
{
'username' => 'ted',
'userkey' => 'keydata2',
},
]
}
我认为在这里重组输入数据比尝试处理两个数组要好得多。