module Facter

Functions as a hash of 'facts' about your system system, such as MAC address, IP address, architecture, etc.

@example Retrieve a fact

puts Facter['operatingsystem'].value

@example Retrieve all facts

Facter.to_hash
 => { "kernel"=>"Linux", "uptime_days"=>0, "ipaddress"=>"10.0.0.1" }

@api public

Constants

FACTERVERSION

Public Class Methods

[](name) click to toggle source

Returns a fact object by name. If you use this, you still have to call {Facter::Util::Fact#value `value`} on it to retrieve the actual value.

@param name [String] the name of the fact

@return [Facter::Util::Fact, nil] The fact object, or nil if no fact

is found.

@api public

# File lib/facter.rb, line 81
def self.[](name)
  collection.fact(name)
end
add(name, options = {}, &block) click to toggle source

Adds a {Facter::Util::Resolution resolution} mechanism for a named fact. This does not distinguish between adding a new fact and adding a new way to resolve a fact.

@overload add(name, options = {}, { || … }) @param name [String] the fact name @param options [Hash] optional parameters for the fact - attributes

of {Facter::Util::Fact} and {Facter::Util::Resolution} can be
supplied here

@option options [Integer] :timeout set the

{Facter::Util::Resolution#timeout timeout} for this resolution

@param block [Proc] a block defining a fact resolution

@return [Facter::Util::Fact] the fact object, which includes any previously

defined resolutions

@api public

# File lib/facter.rb, line 160
def self.add(name, options = {}, &block)
  collection.add(name, options, &block)
end
clear() click to toggle source

Clears all cached values and removes all facts from memory.

@return [void]

@api public

# File lib/facter.rb, line 186
def self.clear
  Facter.flush
  Facter.reset
end
collection() click to toggle source

Accessor for the collection object which holds all the facts @return [Facter::Util::Collection] the collection of facts

@api private

# File lib/facter.rb, line 50
def self.collection
  unless defined?(@collection) and @collection
    @collection = Facter::Util::Collection.new(
      Facter::Util::Loader.new,
      Facter::Util::Config.ext_fact_loader)
  end
  @collection
end
define_fact(name, options = {}, &block) click to toggle source

Define a new fact or extend an existing fact.

@param name [Symbol] The name of the fact to define @param options [Hash] A hash of options to set on the fact

@return [Facter::Util::Fact] The fact that was defined

@api public @see {Facter::Util::Collection#define_fact}

# File lib/facter.rb, line 139
def self.define_fact(name, options = {}, &block)
  collection.define_fact(name, options, &block)
end
each() { |*args| ... } click to toggle source

Iterates over fact names and values

@yieldparam [String] name the fact name @yieldparam [String] value the current value of the fact

@return [void]

@api public

# File lib/facter.rb, line 172
def self.each
  # Make sure all facts are loaded.
  collection.load_all

  collection.each do |*args|
    yield(*args)
  end
end
fact(name) click to toggle source

(see [])

# File lib/facter.rb, line 86
def self.fact(name)
  collection.fact(name)
end
flush() click to toggle source

Flushes cached values for all facts. This does not cause code to be reloaded; it only clears the cached results.

@return [void]

@api public

# File lib/facter.rb, line 96
def self.flush
  collection.flush
end
json?() click to toggle source

Returns whether the JSON “feature” is available.

@api private

# File lib/facter.rb, line 62
def self.json?
  begin
    require 'json'
    true
  rescue LoadError
    false
  end
end
list() click to toggle source

Lists all fact names

@return [Array<String>] array of fact names

@api public

# File lib/facter.rb, line 105
def self.list
  collection.list
end
loadfacts() click to toggle source

Loads all facts.

@return [void]

@api public

# File lib/facter.rb, line 207
def self.loadfacts
  collection.load_all
end
reset() click to toggle source

Removes all facts from memory. Use this when the fact code has changed on disk and needs to be reloaded.

@return [void]

@api public

# File lib/facter.rb, line 197
def self.reset
  @collection = nil
  reset_search_path!
end
reset_search_path!() click to toggle source

Reset the Facter search directories.

@api private @return [void]

# File lib/facter.rb, line 236
def self.reset_search_path!
  @search_path = []
end
search_external(dirs) click to toggle source

Registers directories to be searched for external facts.

@param dirs [Array<String>] directories to search

@return [void]

@api public

# File lib/facter.rb, line 249
def self.search_external(dirs)
  Facter::Util::Config.external_facts_dirs += dirs
end
search_external_path() click to toggle source

Returns the registered search directories.

@return [Array<String>] An array of the directories searched

@api public

# File lib/facter.rb, line 258
def self.search_external_path
  Facter::Util::Config.external_facts_dirs.dup
end
search_path() click to toggle source

Returns the registered search directories.

@return [Array<String>] An array of the directories searched

@api public

# File lib/facter.rb, line 228
def self.search_path
  @search_path.dup
end
to_hash() click to toggle source

Gets a hash mapping fact names to their values

@return [Hash{String => Object}] the hash of fact names and values

@api public

# File lib/facter.rb, line 125
def self.to_hash
  collection.load_all
  collection.to_hash
end
value(name) click to toggle source

Gets the value for a fact. Returns `nil` if no such fact exists.

@param name [String] the fact name @return [Object, nil] the value of the fact, or nil if no fact is

found

@api public

# File lib/facter.rb, line 116
def self.value(name)
  collection.value(name)
end
version() click to toggle source

Returns the running version of Facter.

@comment The intent is that software external to Facter be able to

determine the Facter version with no side-effects.  The expected
use is:

  require 'facter/version'
  version = Facter.version

This function has the following ordering precedence.  This precedence list
is designed to facilitate automated packaging tasks by simply writing to
the VERSION file in the same directory as this source file.

 1. If a version has been explicitly assigned using the Facter.version=
    method, return that version.
 2. If there is a VERSION file, read the contents, trim any
    trailing whitespace, and return that version string.
 3. Return the value of the Facter::FACTERVERSION constant hard-coded into
    the source code.

If there is no VERSION file, the method must return the version string of
the nearest parent version that is an officially released version.  That is
to say, if a branch named 3.1.x contains 25 patches on top of the most
recent official release of 3.1.1, then the version method must return the
string "3.1.1" if no "VERSION" file is present.

By design the version identifier is _not_ intended to vary during the life of
a process.  There is no guarantee provided that writing to the VERSION file
while a Puppet process is running will cause the version string to be
updated.  On the contrary, the contents of the VERSION are cached to reduce
filesystem accesses.

The VERSION file is intended to be used by package maintainers who may be
applying patches or otherwise changing the software version in a manner
that warrants a different software version identifier.  The VERSION file is
intended to be managed and owned by the release process and packaging
related tasks, and as such should not reside in version control.  The
FACTERVERSION constant is intended to be version controlled in history.

Ideally, this behavior will allow package maintainers to precisely specify
the version of the software they're packaging as in the following example:

    $ git describe > lib/facter/VERSION
    $ ruby -r facter -e 'puts Facter.version'
    1.6.14-6-g66f2c99

@api public

@return [String] containing the facter version, e.g. “1.6.14”

# File lib/facter/version.rb, line 55
def self.version
  version_file = File.join(File.dirname(__FILE__), 'VERSION')
  return @facter_version if @facter_version
  if version = read_version_file(version_file)
    @facter_version = version
  end
  @facter_version ||= FACTERVERSION
end
version=(version) click to toggle source

Sets the Facter version

@return [void]

@api private

# File lib/facter/version.rb, line 69
def self.version=(version)
  @facter_version = version
end