I found myself recently writing yet another fact to read the contents of a knob file, so I generalized one to read all the files found in /etc/knobs and return the contents as facts named after the files found.
Caveats: read_knobs.rb returns only the last line it finds in the file. This is on purpose. It also skips all lines beginning with # so you can use shell-style comments. Files that are empty create facts with the value true, and files containing just a t or f are normalized to true or false respectively.
Here's the source, and it's also available on github.
Caveats: read_knobs.rb returns only the last line it finds in the file. This is on purpose. It also skips all lines beginning with # so you can use shell-style comments. Files that are empty create facts with the value true, and files containing just a t or f are normalized to true or false respectively.
Here's the source, and it's also available on github.
#!/usr/bin/ruby## fact returns knob values based on contents of /etc/knobs.## Author: jpb@ooyala.com## Copyright 2009 Ooyala, Inc.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#require'facter'deflogger(message)system("/usr/bin/logger -t read_knobs #{message}")end# facts can only have one value. We ignore lines with shell style comments,# and return the last valid line.defread_knob(filename)knob_name=filename.split('/')[-1]knob_file=File.open(filename)# an empty knob file must have been created for a reason, so set default# value to truevalue=trueknob_file.each{|line|ifline[0,1]!="#"if(line.downcase.chomp=="true")or(line.downcase.chomp=="t")value=trueelsif(line.downcase.chomp=="false")or(line.downcase.chomp=="f")value=falseelsevalue=line.chompendend}knob_file.closevalueenddefload_knobs(knob_d)logger"Processing #{knob_d}..."if!File.directory?(knob_d)logger("Can't read #{knob_d}!")returnnilendDir["#{knob_d}/*"].eachdo|knob|ifFile.readable?(knob)knob_name=knob.split('/')[-1]Facter.add("#{knob_name}")dosetcodedodata=read_knob(knob)dataendendelselogger("Can't read #{knob}!")endendendload_knobs('/etc/knobs')