有没有办法仅在已设置的情况下将参数传递给 puppet 资源?例如,假设我有一个创建新用户的 list ,我可能想也可能不想手动设置 uid 和 gid。我能想到的最好方法是这样的:
class example (
$uid = undef,
$gid = undef,
$username = 'default'
) {
user { $username:
ensure => present,
# etc
}
if $uid != undef {
user { "uid-${username}":
name => $username,
uid => $uid,
require => User[$username]
}
}
if $gid != undef {
user { "gid-${username}":
name => $username,
gid => $gid,
require => User[$username]
}
}
}
但这有很多代码只是为了确定是否发送 uid 和/或 gid。有更好的办法吗?
最佳答案
我认为您的示例由于多个声明而无法编译,因为 name
关于user
资源是正在检查唯一性的内容。话虽这么说,在我看来,有几种方法可以让它稍微好一点。
第一种方式涉及属性修改:
...
user { $username:
ensure => present,
# etc
}
if $uid != undef {
User[$username] { uid => $uid }
}
if $gid != undef {
User[$username] { gid => $gid }
}
第二个涉及从哈希设置属性:
class example (
Hash $id = {},
String $username = 'default'
) {
user { $username:
ensure => present,
* => $id,
# etc
}
}
然后通过声明传递附加参数:
class example { 'foo':
username => 'bar',
id => { uid => 1000, gid => 1000 }
}
或使用类似于以下内容的自动 Hiera 查找:
example::id:
uid: 1000
gid: 1000
请注意,未指定 id
在任何一种情况下(声明或 Hiera)仍然会导致 user
的正确且预期的行为。资源无uid
或gid
已更改。
您可以在 https://docs.puppet.com/puppet/latest/reference/lang_resources_advanced.html 中找到记录的两种方法。 ,您甚至可以从该文档中找出另一种或两种方法。
关于puppet - 可选择将参数传递给 puppet 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40530656/