class MCollective::Facts::Yaml_facts

A factsource that reads a hash of facts from a YAML file

Multiple files can be specified seperated with a : in the config file, they will be merged with later files overriding earlier ones in the list.

Public Class Methods

new() click to toggle source
Calls superclass method MCollective::Facts::Base::new
   # File lib/mcollective/facts/yaml_facts.rb
11 def initialize
12   @yaml_file_mtimes = {}
13 
14   super
15 end

Public Instance Methods

force_reload?() click to toggle source

force fact reloads when the mtime on the yaml file change

   # File lib/mcollective/facts/yaml_facts.rb
43 def force_reload?
44   config = Config.instance
45 
46   fact_files = config.pluginconf["yaml"].split(File::PATH_SEPARATOR)
47 
48   fact_files.each do |file|
49     @yaml_file_mtimes[file] ||= File.stat(file).mtime
50     mtime = File.stat(file).mtime
51 
52     if mtime > @yaml_file_mtimes[file]
53       @yaml_file_mtimes[file] = mtime
54 
55       Log.debug("Forcing fact reload due to age of #{file}")
56 
57       return true
58     end
59   end
60 
61   false
62 end
load_facts_from_source() click to toggle source
   # File lib/mcollective/facts/yaml_facts.rb
17 def load_facts_from_source
18   config = Config.instance
19 
20   fact_files = config.pluginconf["yaml"].split(File::PATH_SEPARATOR)
21   facts = {}
22 
23   fact_files.each do |file|
24     begin
25       if File.exist?(file)
26         if YAML.respond_to? :safe_load
27           facts.merge!(YAML.safe_load(File.read(file), [Symbol], [], true))
28         else
29           facts.merge!(YAML.load(File.read(file)))  # rubocop:disable Security/YAMLLoad
30         end
31       else
32         raise("Can't find YAML file to load: #{file}")
33       end
34     rescue Exception => e
35       Log.error("Failed to load yaml facts from #{file}: #{e.class}: #{e}")
36     end
37   end
38 
39   facts
40 end