我正在尝试通过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
不能指定多个变体。两者都有可能吗?不是为了同一个文件,而是为了我想要的单独文件吗?
答案 0 :(得分:2)
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
,并在content
的文件/符号链接上设置target
,例如< / p>
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,
}
}
或者最好使用单独的定义来明确行为。