[英]Puppet - how to use different file source
我在Puppet中為Nagios代理(NRPE)創建了一個配置。 現在,我試圖根據Dirs的存在設置不同的文件源。 首先,我檢查特定目錄是否存在,然后設置特定文件內容。 我當前的配置文件如下所示:
class nagios_client::file_nagios-check-Linux-stats {
include nagios_client::check_location_lib-nagios
file { '/usr/lib/nagios/plugins/check_linux_stats.pl':
ensure => file,
owner => root,
group => root,
mode => 755,
content => template("nagios_client/check_linux_stats.pl.erb"),
require => Exec["check_usr-lib_exists"],
}
file { '/usr/lib64/nagios/plugins/check_linux_stats.pl':
ensure => file,
owner => root,
group => root,
mode => 755,
content => template("nagios_client/check_linux_stats.pl.erb"),
require => Exec["check_usr-lib64_exists"],
}
file { '/usr/lib32/nagios/plugins/check_linux_stats.pl':
ensure => file,
owner => root,
group => root,
mode => 755,
content => template("nagios_client/check_linux_stats.pl.erb"),
require => Exec["check_usr-lib32_exists"],
}
}
這工作正常,但我對此有疑問:
class nagios_client::file_nrpe-cfg {
# include nagios_client::check_location_lib-nagios
file { '/etc/nagios/nrpe.cfg.def':
path => '/etc/nagios/nrpe.cfg',
ensure => file,
owner => root,
group => root,
mode => 644,
content => template("nagios_client/nrpe-cfg.erb"),
require => Exec["check_usr-lib_exists"],
}
file { '/etc/nagios/nrpe.cfg.32':
path => '/etc/nagios/nrpe.cfg',
ensure => file,
owner => root,
group => root,
mode => 644,
content => template("nagios_client/nrpe-cfg-32.erb"),
require => Exec["check_usr-lib32_exists"],
}
file { '/etc/nagios/nrpe.cfg.64':
path => '/etc/nagios/nrpe.cfg',
ensure => file,
owner => root,
group => root,
mode => 644,
content => template("nagios_client/nrpe-cfg-64.erb"),
require => Exec["check_usr-lib64_exists"],
}
}
看起來require => Exec[...]
被忽略了。
我的支票定義:
class nagios_client::check_location_lib-nagios {
exec { 'check_usr-lib_exists':
command => '/bin/true',
onlyif => '/usr/bin/test -d /usr/lib/nagios/plugins',
}
exec { 'check_usr-lib32_exists':
command => '/bin/true',
onlyif => '/usr/bin/test -d /usr/lib32/nagios/plugins',
}
exec { 'check_usr-lib64_exists':
command => '/bin/true',
onlyif => '/usr/bin/test -d /usr/lib64/nagios/plugins',
}
}
我正在使用Puppet 3.8.7。 如何以正確的方式做到這一點?
您所擁有的問題是,您正在使用require
,這只能確保指定的資源(在這種情況下為每個exec
)在file
資源之前執行。 您想要的行為與notify
關系(創建刷新事件)的關系更加緊密,但是, file
資源並不關心刷新事件。 您可以在此處閱讀有關刷新關系的更多信息: https : //puppet.com/docs/puppet/latest/lang_relationships.html#refreshing-and-notification 。
我有2種可能的方法可以解決此問題。 第一個是使用exec
語句來管理文件,而不是file
資源。 這絕對不是最佳選擇,因為您會丟失file
資源中的所有參數(我絕對不建議您使用這種方法,但是您可以這樣做)。
另一種方法是創建自定義紅寶石事實,以檢查文件是否存在。 事實看起來像這樣:
Facter.add('nagios_directories') do
confine kernel: 'Linux'
setcode do
paths_to_check = [
'/usr/lib/nagios/plugins',
'/usr/lib32/nagios/plugins',
'/usr/lib64/nagios/plugins',
]
paths_to_check.select { |d| File.directory?(d) }
end
end
這個事實將檢查paths_to_check
數組中列出的所有目錄,並返回包含確實存在的目錄的數組。 如果沒有目錄,它將返回一個空數組。
設置完事實后,您可以像下面這樣重寫代碼:
class nagios_client::file_nrpe-cfg {
if (member($fact['nagios_directories'], '/usr/lib/nagios/plugins')) {
file { '/etc/nagios/nrpe.cfg.def':
path => '/etc/nagios/nrpe.cfg',
ensure => file,
owner => root,
group => root,
mode => 644,
content => template("nagios_client/nrpe-cfg.erb"),
}
}
if (member($fact['nagios_directories'], '/usr/lib32/nagios/plugins')) {
file { '/etc/nagios/nrpe.cfg.32':
path => '/etc/nagios/nrpe.cfg',
ensure => file,
owner => root,
group => root,
mode => 644,
content => template("nagios_client/nrpe-cfg-32.erb"),
}
}
if (member($fact['nagios_directories'], '/usr/lib64/nagios/plugins')) {
file { '/etc/nagios/nrpe.cfg.64':
path => '/etc/nagios/nrpe.cfg',
ensure => file,
owner => root,
group => root,
mode => 644,
content => template("nagios_client/nrpe-cfg-64.erb"),
}
}
}
這是一些有關自定義事實的其他文檔: https : //puppet.com/docs/facter/3.9/fact_overview.html 。
最后,如果您正在使用Puppet 6(當前是最新版本),則可以編寫自定義Ruby函數並使用新的deferred
類型。 此類型使您可以在目錄運行時在代理上執行功能(在此發行版之前,所有Puppet函數均在編譯時在Puppet Master上執行),這意味着您可以使用函數來檢查文件是否存在。 我沒有機會嘗試此功能,但是您可以在這里查看文檔: https : //puppet.com/docs/puppet/6.0/integrating_secrets_and_retrieving_agent-side_data.html 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.