参考https://docs.puppetlabs.com/references/latest/function.html#createresources
# A hash of user resources:
$myusers = {
'nick' => { uid => '1330',
gid => allstaff,
groups => ['developers', 'operations', 'release'], },
'dan' => { uid => '1308',
gid => allstaff,
groups => ['developers', 'prosvc', 'release'], },
}
create_resources(user, $myusers)
阅读此功能create_resources
的说明,但不确定create_resources(user, $myusers)
后的结果
是否会使用指定的uid,gid和群组创建两个用户nick
和dan
?
来自网络的一些解释。
Listing 12-79. Hiera lookup of sysadmin hash
root@puppet-master-hiera-ubuntu:/etc/puppet/data# hiera sysadmins
{"spencer"=>{"uid"=>1861, "groups"=>"root", "gid"=>300}, "william"=>{"uid"=>11254,
"groups"=>"root", "gid"=>300}}
Now we can use a function called create_resources() to generate Puppet resources from this hash, as shown in
Listing 12-80. create_resources example
$sysadmins = hiera('sysadmins')
create_resources(user, $sysadmins)
Listing 12-81 shows the output.
Listing 12-81. Applying the create_resources example from Listing 12-80
root@puppet-master-hiera-ubuntu:/etc/puppet# puppet apply create_resources.pp
Notice: Compiled catalog for puppet-master-hiera-ubuntu.green.gah in environment production in
0.11 seconds
Notice: /User[spencer]/ensure: created
Notice: /User[william]/ensure: created
Notice: Finished catalog run in 0.32 seconds
我无法在我的环境中正确设置和验证它,但上面的示例给出了答案,它在create_resources
函数中创建了带哈希的用户帐户。
答案 0 :(得分:1)
create_resources
只会映射
# A hash of user resources:
$myusers = {
'nick' => { uid => '1330',
gid => allstaff,
groups => ['developers', 'operations', 'release'], },
'dan' => { uid => '1308',
gid => allstaff,
groups => ['developers', 'prosvc', 'release'], },
}
create_resources(user, $myusers)
到
user{'nick':
uid => '1330',
gid => allstaff,
groups => ['developers', 'operations', 'release'], },
}
user{'dan':
uid => '1308',
gid => allstaff,
groups => ['developers', 'prosvc', 'release'], },
}
因此,这些用户将被传递给用户提供商。您可以使用您的配方轻松地创建users.pp文件并使用puppet apply --noop
进行测试来测试它:
# puppet apply --noop users.pp
Notice: Compiled catalog for yourfqdn in environment production in 0.15 seconds
Notice: /Stage[main]/Main/User[nick]/ensure: current_value absent, should be present (noop)
Notice: /Stage[main]/Main/User[dan]/ensure: current_value absent, should be present (noop)
请注意,如果用户已经存在,则puppet apply将不执行任何操作