我在定义的类型remote_file
中定义了一些变量。
当我在class cdnlog::base
中实例化此类型时,如果我用require_dir
注释了该行,我会在puppet客户端中收到一些错误消息:
错误:无法从远程服务器检索目录:SERVER上的错误400:无效关系:文件[cdnlog-release] {require => ,},因为,似乎没有正确的格式。资源引用应格式化为:Classname [' title']或Modulename :: Classname [' title'](请注意大小写)。
警告:未在失败的目录
上使用缓存错误:无法检索目录;跳过运行
我的清单:
define remote_file($remote_location=undef, $path=undef, $mode='0644', $require_dir=undef){
exec{"retrieve_${title}":
command => "/usr/bin/wget -q ${remote_location} -O ${path}",
creates => $path,
}
$require_exec="Exec[retrieve_${title}]"
if $require_dir {
$require_file="File[$require_dir]"
}
elsif $require_dir == undef {
$require_file=","
}
file{$title:
path => $path,
mode => $mode,
require => [$require_exec, $require_file],
}
}
class cdnlog::base{
tag 'cdnlog'
file {'/etc':
ensure => directory,
recurse => true,
}
file {'/etc/rc.d/init.d':
ensure => directory,
recurse => true,
}
file {'/etc/cdnlog.d':
ensure => directory,
recurse => true,
}
###########################################
###########################################
###########################################
remote_file {'cdnlog-release':
path => '/etc/cdnlog.d/cdnlog-release',
remote_location => "${collectd_server}/tarball/cdnlog-release",
require_dir => '/etc/cdnlog.d',
}
remote_file {'cdnlog.conf':
path => '/etc/cdnlog.conf',
remote_location => "${collectd_server}/tarball/cdnlog.conf",
require_dir => '/etc',
}
}
感谢您的帮助
答案 0 :(得分:3)
变量替换后的有效资源是:
file{'cdnlog-release':
path => '/etc/cdnlog.d/cdnlog-release',
mode => '0644',
require => [Exec['retrieve_cdnlog-release], ","],
}
这是无效的。 你应该尝试使用
$require_exec=Exec["retrieve_${title}"]
if $require_dir {
$requirearray=[$require_exec ,File["$require_dir"]]
}
elsif $require_dir == undef {
$requirearray=$require_exec
}
file{$title:
path => $path,
mode => $mode,
require => $requirearray,
}
引号也应放在括号内
答案 1 :(得分:0)
为后人添加一个重构Raul的答案。
file { $title:
path => $path,
mode => $mode,
}
if $require_dir {
File[$title] { require => [ $require_exec , File["$require_dir"] ]
}
elsif $require_dir == undef {
File[$title] { require => $require_exec }
}
它可以帮助您避免处理太多不必要的变量。