我有以下资源:
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?
假设每次脚本运行时,它都会生成与给定主机文件完全相同的文件,您可以稍作调整。
# 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'], }
从长远来看,尽pipe尝试设法将脚本转换为模板可能会更好。 或者找一些更适合傀儡做事的方式。
你的exec定义的问题是它会改变/更新文件,而不pipe内容是否被改变了。 看看typedef规范中的onlyif
如果我是你,我会在临时文件中生成sv-hosts.cfg文件,并将其与当前文件进行比较。 如果有一些差异,只需将tmp文件复制到新位置,然后重新启动nagios。
也看看types的参考文档 ,因为有这么多的nagios特定types,也许其中一个将为你做的伎俩:)