How to generate file using script using puppet

puppet

I have the following resource:

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'],
}

Ideally, I'd want to restart Nagios only if the file changed. It seems what I really want is a file resource, but file resources have a template or the actual content, not a script. How can I generate the file (or change my method) to restart Nagios only when /etc/nagios3/conf.d/sv-hosts.cfg changes?

Best Answer

Assuming that each time your script runs it will generate the file exactly the same way for a given hosts file you could make a minor adjustment.

# 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'],
}

In the long term though it might be better to try and find ways to convert your script into a template. Or find some other way that fits better into the puppet way of doing things.