November 17, 2011, 8:51 am
Simple puppet file based "fact" definition and facter plugin
I ran into a situation with puppet where we needed very simple file based facts. I googled around and oddly didn't find anything. Here is what I came up with that met my needs.
A file resource ensures /etc/facter/facts exists and purges recursively. A definition drops a file into that dir so that the name of the fact is the name of the file, and the contents of the fact are the contents of the file. The puppet masters are configured to deploy plugins - EG: http://docs.puppetlabs.com/guides/plugins_in_modules.html
See below for code.
The facts ruby facter plugin (modules/facts/lib/facter/facts.rb):
The definition which provides "fact" (fact.pp):
And finally the file resource that sets up the /etc/facter/facts area:
You can use this easily like this:
If you want to see these facts when you manually run facter from the cli, you should set your environment variable for facter lib (syntax for bash):
A file resource ensures /etc/facter/facts exists and purges recursively. A definition drops a file into that dir so that the name of the fact is the name of the file, and the contents of the fact are the contents of the file. The puppet masters are configured to deploy plugins - EG: http://docs.puppetlabs.com/guides/plugins_in_modules.html
See below for code.
The facts ruby facter plugin (modules/facts/lib/facter/facts.rb):
path="/etc/facter/facts"
if File.directory? "#{path}"
Dir.foreach(path) do |entry|
entry.chomp
if File.file? "#{path}/#{entry}"
Facter.add(entry) do
setcode do
contents = File.read("#{path}/#{entry}")
contents.chomp
end
end
end
end
end
The definition which provides "fact" (fact.pp):
# Provide arbitrary named facts - cannot match name of facter plugin (from
# module path)
#
#
# Example
#
# fact { "sysadmin1": value => "My Name" }
# fact { "sysadmin2": value => "Your Name" }
# fact { "support": value => "Group Name" }
# fact { "restricted": value => "true" }
#
define fact ($ensure=present,
$value='NOSRC') {
$factsdir = "/etc/facter/facts"
case $ensure {
absent: {
file { "$factsdir/$name": ensure => absent }
}
present: {
case $value {
'NOSRC': {
fail "value required for fact define"
}
default: {
file { "$factsdir/$name":
content => "$value\n",
require => File[$factsdir],
}
}
}
}
default: { crit "Invalid ensure value: $ensure." }
}
}
And finally the file resource that sets up the /etc/facter/facts area:
file {
"/etc/facter":
ensure => directory;
"/etc/facter/facts":
ensure => directory,
require => File["/etc/facter"],
recurse => true,
force => true,
purge => true;
}
You can use this easily like this:
fact { "support": value => "SHARED" }If you want to see these facts when you manually run facter from the cli, you should set your environment variable for facter lib (syntax for bash):
export FACTERLIB=/var/lib/puppet/lib/facter