我有以下资源:
exec{'regen-nagios-hosts':
command => '/usr/local/bin/generate-nagios-host-definitions-from-hostfile < /etc/hosts > /etc/nagios3/conf.d/sv-hosts.cfg',
user => 'root',
before => Class['nagios::server'],
notify => Service['nagios3'],
require => File['/etc/hosts'],
}
理想情况下,我希望仅在文件发生更改时重新启动 Nagios。看来我真正想要的是文件资源,但文件资源有模板或实际内容,而不是脚本。我如何生成文件(或更改我的方法)以仅在/etc/nagios3/conf.d/sv-hosts.cfg
发生更改时重新启动 Nagios?
答案1
假设每次脚本运行时,它都会以完全相同的方式为给定的主机文件生成文件,您可以进行微小的调整。
# generate the file into some arbitrary location, that is not actually referenced by nagios
exec{'regen-nagios-hosts':
command => '/usr/local/bin/generate-nagios-host-definitions-from-hostfile < /etc/hosts > /etc/nagios3/generatedtmpsv-hosts.cfg',
user => 'root',
before => Class['nagios::server'],
require => File['/etc/hosts'],
}
# have a file resource use the generated file to update the production file
# the update will only happen when the checksums have changed
# on update notify the service to restart.
file { '/etc/nagios3/conf.d/sv-hosts.cfg'':
ensure => present,
source => '/etc/nagios3/generatedtmp/sv-hosts.cfg'
require => Exec['regen-nagios-hosts'],
notify => Service['nagios3'],
}
但从长远来看,尝试找到将脚本转换为模板的方法可能会更好。或者找到更适合傀儡做事方式的其他方法。
答案2
您的 exec 定义的问题在于,无论内容是否实际更改,它都会更改/更新文件。请onlyif
查看typedef 规范
如果我是你,我会sv-hosts.cfg
在临时文件中生成文件并将其与当前文件进行比较。如果有差异,只需将 tmp 文件复制到新位置并重新启动 nagios。
还可以看看类型参考文档因为 nagios 有很多特定类型,也许其中一种类型可以满足您的需求 :)