我正在尝试通过Puppet(使用Hiera(创建一个文件和一个符号链接。 我在 Ubuntu 16.04 上运行,并且正在使用所有最新的模块。该文件称为"a",链接称为"b"。 这些文件没有直接关系。
我有一个节点的 init.pp 文件:
define hiera::files (
$ensure,
$content= "",
$target="/./.",
$mode = "",
$owner = "root",
$force = "false" ) {
file { $title:
ensure => $ensure,
owner => $owner,
force => $force,
content => $content,
mode => $mode,
target => $target,
}
}
create_resources('hiera::files', hiera_hash('files', {}))
node "default" {
hiera_include('classes')
}
还有一个包含相应数据的node.yaml文件:
files:
/etc/a.txt:
ensure: "present"
mode: "2770"
owner: "simon"
content: "[d] n type = sss n resource = samba_1"
/etc/b:
ensure: "link"
target: "/usr/share/b"
mode: "777"
我尝试了其他变体,但我总是收到一个错误,我无法指定content
、source
、target
中的多个。有可能两者兼而有之吗? 不是针对同一个文件,而是像我尝试做的那样针对单独的文件?
hiera::files
默认值是这里的问题,因为即使您没有在 Hiera 哈希中指定content
和target
,它们也会被设置:
define hiera::files ( $ensure, $content= "", $target="/./.", $mode = "", $owner = "root", $force = "false" ){
将$content
和$target
默认值更改为undef
,以便在要使用它们时必须指定它们。
define hiera::files ( $ensure, $content = undef, $target = undef, $mode = "", $owner = "root", $force = "false" ){
如果您确实需要这些默认值,那么您需要一种方法来避免在包含内容的文件上设置target
,而在带有target
的文件/符号链接上设置content
,例如
define hiera::files ( $ensure, $content = undef, $target = undef, $mode = "", $owner = "root", $force = "false" ){
# Provide a default empty content value when ensure => present, and no content is given
$real_content = $ensure ? {
"present" => $content ? {
undef => "",
default => $content,
},
default => undef,
}
# Provide a default /./. target value when ensure => link, and no target is given
$real_target = $ensure ? {
"link" => $target ? {
undef => "/./.",
default => $target,
},
default => undef,
}
file { $title:
ensure => $ensure,
owner => $owner,
force => $force,
content => $real_content,
mode => $mode,
target => $real_target,
}
}
或者,也许最好使用单独的定义来明确行为。