Created
February 21, 2018 21:36
-
-
Save cardil/63f063ac1cbe082f87284e3e2064476b to your computer and use it in GitHub Desktop.
Example custom fact for Facter 3+ with 2 implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Facter.add(:modules) do | |
setcode { simple } | |
def simple | |
codedir = Facter::Core::Execution.exec('puppet config print codedir') | |
list = Facter::Core::Execution.exec("ls #{codedir}/modules").split("\n") | |
{ | |
count: list.size, | |
list: list.freeze | |
} | |
end | |
def power | |
require 'yaml' | |
require 'puppet' | |
yaml = Facter::Core::Execution.exec('puppet module list --render-as yaml') | |
data = YAML.load(yaml) | |
modules = [] | |
data[:'modules_by_path'].each do |k, list| | |
list.each do |m| | |
splited = m.forge_name.split('/') | |
modules.push({ | |
name: m.forge_name, | |
company: splited[0], | |
short: splited[1], | |
version: m.version | |
}) | |
end | |
end | |
{ | |
list: modules, | |
names: { | |
full: modules.map { |m| m[:name] }, | |
short: modules.map { |m| m[:short] } | |
}, | |
count: modules.size | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment