Skip to content

Instantly share code, notes, and snippets.

@reidmv
Created December 26, 2013 18:59
Show Gist options
  • Save reidmv/8137423 to your computer and use it in GitHub Desktop.
Save reidmv/8137423 to your computer and use it in GitHub Desktop.
Custom Facts indirector to inject extra Hiera-based fact
require 'puppet/indirector/facts/puppetdb'
require 'puppet/node/facts'
require 'hiera'
class Puppet::Node::Facts::PuppetdbWithNetwork < Puppet::Node::Facts::Puppetdb
# Here we override the normal save, injecting a custom fact
def save(args)
name = args.instance.name
facts = args.instance.values
facts.define_singleton_method(:lookupvar) do |key|
self[key] || self[key.to_s]
end
begin
config = Hiera::Config.load(Puppet['hiera_config'])
config[:logger] = 'puppet'
hiera = Hiera.new(:config => config)
# For any Hiera lookup to inject as a fact, add it here
network = hiera.lookup('network', nil, facts, nil, :priority)
facts['network'] = network if network
args.instance = Puppet::Node::Facts.new(name, facts)
rescue
# ignore
ensure
super(args)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment