简体   繁体   中英

How to schedule puppet custom facts to execute every X hours?

I have a custom puppet fact (written in ruby) which executes a simple bash script. The script checks all files in the file system for world-readable permissions and returns results accordingly. The puppet fact checks the output of the script and returns "fail" if the script output is not null.

The problem is that the script is resource intensive and I don't want it to be executed every 30 minutes (client' puppet.conf: runinternval=1800).

I've tried using the "schedule" resource but because of the way puppet works, it only affects the class I'm using to evaluate the fact's output. Puppet facts are evaluated on each puppet run, regardless of anything else. (thus making them available for resources)

I've also tried to move the code that executes the bash script out from the fact and into the puppet class but it appears you cannot evaluate the output of a bash script (using "exec" type) and store it in a variable (again, because of the way puppet works).

I am now reduced to try and apply some kind of scheduling mechanism using Ruby language, and implement it in the fact. I've read a bit on PStore and right now this seems like a good direction.

Does anyone know how I can make a ruby script execute only during night? I cannot use crontab because the code will run by puppet.

Code snippet from the puppet class:

if $::myfact == 'fail' {
  notify { "warning blah blah...":
    loglevel => err,
    schedule => 'night',
   }
} 

Code snippet from the puppet fact:

 require 'facter'
 Facter.add(:myfact) do
   setcode do
     if File.exists? '/usr/local/puppet/scripts/myscript.sh'
       result = Facter::Util::Resolution.exec("/usr/local/puppet/scripts/myscript.sh")
       if result != ''
         puts "Result of myfact is: \n #{result}"
       'fail'
     end
   end
 end

end

Your custom fact can memorialize its result in a file somewhere on the system. When it is evaluated, it can compare the timestamp on that file to the current system time, and either read back the value or compute it freshly, as appropriate. That also allows you to defer updates (for instance, by touch ing the file) or to manually request re-evaluation (by deleting the file).

Consider, however, whether it makes sense to decouple the costly evaluation from fact gathering. Even if Facter only occasionally has to re-evaluate the fact from scratch, Facter -- and therefore Puppet -- will take a long time to perform those runs. You could consider instead using a scheduled job to perform the evaluation at whatever intervals you like, and have the custom fact always rely on cached results from the latest of those evaluations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM