Created
August 22, 2012 08:41
-
-
Save raphink/3423800 to your computer and use it in GitHub Desktop.
Generic Augeas based facter plugin
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
# This is the configuration file | |
# for the augeas_generic.rb facter plugin | |
# | |
# Each section defines a new fact | |
# and has the following attributes: | |
# - type: Optional, the type of value to retrieve. | |
# Possible values: "single" and "multiple". | |
# Defaults to "single". | |
# - path: Mandatory, the path to the value. | |
# - method: Optional, how to retrieve the value. | |
# Possible values: "value", "label". | |
# Defaults to "value". | |
# When "value" is used, the node value is returned. | |
# When "label" is used, the node label is returned. | |
# - sep: Optional, the separator for multiple values. | |
# Defaults to ",". | |
[augeasversion] | |
type = single | |
path = /augeas/version | |
method = value | |
[root_shell] | |
type = single | |
path = /files/etc/passwd/root/shell | |
[root_device] | |
type = single | |
path = /files/etc/mtab/*[file="/"]/spec | |
[home_device] | |
# a home device | |
# type=single is default | |
path = /files/etc/mtab/*[file="/home"]/spec | |
[users] | |
type = multiple | |
sep = : | |
path = /files/etc/passwd/* | |
method = label |
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
require 'augeas' | |
aug = Augeas::open(nil, nil, Augeas::NO_LOAD) | |
DEFAULT_SEP = ',' | |
def path_label(path) | |
path.split("/")[-1].split("[")[0] | |
end | |
def get_value(aug, path, method) | |
if method == 'label' | |
path_label(path) | |
elsif method == 'value' | |
aug.get(path) | |
else | |
Facter.debug("Unknown method #{method} to retrieve path #{path}") | |
return nil | |
end | |
end | |
facts_file = '/home/rpinson/bas/facter/aug_facts.ini' | |
aug.transform( | |
:lens => 'Puppet.lns', | |
:name => 'Aug_Facts', | |
:incl => facts_file | |
) | |
aug.load! | |
aug.match("/files#{facts_file}/*[label()!='#comment']").each do |fact| | |
fact_name = path_label(fact) | |
Facter.debug("Adding fact #{fact_name}") | |
path = aug.get("#{fact}/path") | |
unless path | |
Facter.debug("No path specified for fact #{fact_name}. Ignoring.") | |
next | |
end | |
type = aug.get("#{fact}/type") || 'single' | |
method = aug.get("#{fact}/method") || 'value' | |
Facter.add(fact_name) do | |
setcode do | |
if type == 'single' | |
get_value(aug, path, method) || nil | |
elsif type == 'multiple' | |
sep = aug.get("#{fact}/sep") || DEFAULT_SEP | |
vals = Array.new() | |
aug.match(path).each do |p| | |
Facter.debug("Adding value from path #{p}") | |
vals.push(get_value(aug, p, method)) | |
end | |
vals.join(sep) | |
else | |
Facter.debug("Unknown type #{type} for fact #{fact_name}. Ignoring") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment