summaryrefslogtreecommitdiff
path: root/fact.rb
diff options
context:
space:
mode:
authorKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>2011-04-23 01:19:43 +0100
committerKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>2011-04-23 01:28:11 +0100
commit399dd8cca8d527e40558cbb44eab1c1b5111d6c3 (patch)
tree9af5569ee668e2d51565256d4fc6b61c9a7fe97c /fact.rb
parent55c94178ebec564cb27a1394a2fa0a970bf03621 (diff)
First version. A function for Puppet that allows to retrieve a fact from Facter
easily instead of resorting to awful "inline_template" use-and-abuse practices. Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
Diffstat (limited to 'fact.rb')
-rw-r--r--fact.rb63
1 files changed, 63 insertions, 0 deletions
diff --git a/fact.rb b/fact.rb
new file mode 100644
index 0000000..7d170c8
--- /dev/null
+++ b/fact.rb
@@ -0,0 +1,63 @@
+#
+# fact.rb
+#
+
+module Puppet::Parser::Functions
+ newfunction(:fact, :type => :rvalue, :doc => <<-EOS
+This function will retrieve fact from Facter based on the fact name
+and expose it for further use within Puppet manifest file ...
+
+For example:
+
+Given the following sample manifest:
+
+ define partitions {
+ $result = split(fact("partitions_${name}"), ',')
+
+ notice $result
+
+ partition { $result: }
+ }
+
+ define partition {
+ notice $name
+ }
+
+ $available_disks = split($disks, ',')
+
+ partitions { $available_disks: }
+
+This will produce the following:
+
+ notice: Scope(Partitions[hda]): hda1 hda2
+ notice: Scope(Partition[hda1]): hda1
+ notice: Scope(Partition[hda2]): hda2
+
+
+Which allows you to avoid resorting to the following:
+
+ $fact = "partitions_${name}"
+ $result = split(inline_template("<%= scope.lookupvar(fact) %>"), ',')
+
+Taking out the need for using "inline_template" in the "partitions" define above.
+ EOS
+ ) do |arguments|
+
+ raise(Puppet::ParseError, "Wrong number of arguments " +
+ "given (#{arguments.size} for 1)") if arguments.size < 1
+
+ fact = arguments[0]
+
+ raise(Puppet::ParseError, 'You must provide fact name') if fact.empty?
+
+ result = lookupvar(fact) # Get the value of interest from Facter ...
+
+ if not result or result.empty?
+ raise(Puppet::ParseError, "Unable to retrieve fact `#{fact}'")
+ end
+
+ return result
+ end
+end
+
+# vim: set ts=2 sw=2 et :