|?Tailor made facts walkthrough
Lengthen Facter by producing your personal tailor made facts to produce specifics to Puppet.
Adding personalized facts to Facter
At times you will need to be able to write down conditional expressions depending on site-specific information that just isnвЂ™t in existence through Facter, or perhaps youвЂ™d like to include it inside a template.
Since you canвЂ™t include arbitrary Ruby code inside of your manifests, the most advantageous remedy is to incorporate a new fact to Facter. These added facts can then be distributed to Puppet clients and are accessible for use in manifests and templates, just like any other fact would be.
Note: Facter 3.0 removed the Ruby implementations of some attributes and replaced them which has a tailor made facts API. Any personalized fact that requires a single on the Ruby documents previously stored in lib/facter/util will fail with the error. For even more content, see the Facter 3.0 release notes .
YouвЂ™re able to insert new facts by producing snippets of Ruby code about the Puppet master. Puppet then works by using Plugins in Modules to distribute the facts to the client.
Loading custom made facts
Facter has a handful of methods of loading facts:
$LOAD\_PATH. or the Ruby library load path
The --custom-dir command line option.
The environment variable вЂ?FACTERLIBвЂ™
YouвЂ™re able to use these methods of loading facts to do things like check data files locally before distributing them, or else you can arrange to have a distinct list of facts accessible on certain machines.
By making use of the Ruby load path
Facter searches all directories with the Ruby $LOAD_PATH variable for subdirectories named вЂ?facterвЂ™, and loads all Ruby data files in those directories. For those who had some directory on your $LOAD_PATH like
/lib/ruby. build like this:
Facter would try to load вЂ?facter/system_load.rbвЂ™, вЂ?facter/users.rbвЂ™, and вЂ?facter/rackspace.rbвЂ™.
Working with the --custom-dir command line option
Facter can take different --custom-dir alternate options about the command line that specifies one directory to search for customized facts. Facter attempts to load all Ruby documents inside of the specified directories. This permits you to definitely do something like this:
By means of the FACTERLIB environment variable
Facter also checks the environment variable FACTERLIB for a delimited (semicolon for Windows and colon for all other platforms) list of directories, and tries to load all Ruby documents in those directories. This lets you to definitely do something like this:
Note: Changes in built-in pluginsync help in Facter 3
Facter two.four deprecated FacterвЂ™s aid for loading facts through PuppetвЂ™s pluginsync (the -p option), and Facter 3.0.0 removed the -p option. However, we reversed this decision in Facter 3.0.two and re-enabled the -p option. For details about recent and potential assist for this option, see the Facter 3.0.two release notes .
Two parts of every fact
Setting aside external facts for now, most facts have at least two factors:
A call to Facter.increase('fact_name'). which determines the name on the fact
A setcode statement for very easy resolutions, which is evaluated to determine the factвЂ™s value.
Facts can get a lot significantly more complicated than that, but those two together are the foremost wide-spread implementation of the personalized fact.
Executing shell commands in facts
Puppet gets particulars about a plan from Facter, together with the most widespread way for Facter to get that answers is by executing shell commands. You possibly can then parse and manipulate the output from those commands by using standard Ruby code. The Facter API gives you two or three ways to execute shell commands:
If all you intend to do is run the command and utilize the output, verbatim, as your factвЂ™s value, you possibly can pass the command into setcode directly. For example: setcode 'uname --hardware-platform'
If your fact is much more complicated than that, one can call Facter::Core::Execution.exec('uname --hardware-platform') from inside of the setcode do вЂ¦ conclusion block. As always, whatever the setcode statement returns is made use of since the factвЂ™s value.
In any case, remember that your shell command is likewise a Ruby string, so youвЂ™ll require to escape special characters if you happen to desire to pass them through.
It is important to note that not everything that is effective inside the terminal will operate inside a fact . It is possible to utilize the pipe ( | ) and similar operators as you normally would, but Bash-specific syntax like if statements will not give good results. The greatest way to handle this limitation is to put in writing your conditional logic in Ruby.
LetвЂ™s say you will need to get the output of uname --hardware-platform to one out a exact type of workstation. To do this, you would formulate a new personalized fact. Start off by giving the fact a name, on this case, hardware_platform. and build your new fact in the file, hardware_platform.rb. in the Puppet master server:
You can easlily then make use of the instructions during the Plugins in Modules webpage to copy the new fact to your module and distribute it. During your next Puppet run, the value for the new fact will be on hand to utilise within your manifests and templates.
Choosing other facts
It is easy to generate a fact that works by using other facts by accessing Facter.value(:somefact). If the fact fails to resolve or just isn't existing, Facter returns nil .
Facts have one or two properties that you simply can use to customize how facts are evaluated.
A particular of your even more commonly put into use properties is the confine statement, which restricts the fact to only run on programs that matches another given fact.
An example of your confine statement would be something like the following:
This fact utilizes sysfs on linux to get a list for the power states that are accessible for the given technique. Since this is only attainable on Linux units, we utilize the confine statement to ensure that this fact isnвЂ™t needlessly run on units that do not guidance this type of enumeration.
Only one fact can have several resolutions . just about every of which is a really different way of ascertaining what the value of your fact should be. It is very popular to have different resolutions for different operating units, for example. It is simple and easy to confuse facts and resolutions when you consider that they are superficially identical - to include a new resolution to some fact, you simply insert the fact again, only having a different setcode statement.
When a fact has over just one resolution, the earliest resolution that returns a value other than nil will established the factвЂ™s value. The way that Facter decides the issue of resolution precedence is the weight property. Once Facter rules out any resolutions that are excluded due to the fact that of confine statements, the resolution with the highest weight is evaluated to start with. If that resolution returns nil. Facter moves on to the next resolution (by descending weight) until it gets a value for that fact.
By default, the weight of the fact is the quantity of confines for that resolution, so that a great deal more particular resolutions takes priority over less targeted resolutions.
Facter two.x supported a :timeout option to Facter#add. Facter no longer supports this option, and produces a warning if it is applied.
Although this version of Facter does not assistance overall timeouts on resolutions, you'll be able to pass a timeout to Facter::Core::Execution#execute :
Though the norm is for a fact to return one string, Facter two.0 introduced structured facts . which take the variety of either a hash or an array. All you want to do to make a structured fact is return a hash or an array from the setcode statement. You are able to see some relevant examples inside of the composing structured facts section of your Fact Overview .
If your fact brings together the output of various commands, it may make perception to work with aggregate resolutions . An aggregate resolution is split into вЂњchunksвЂќ, each and every one particular responsible for resolving one particular piece for the fact. After all from the chunks have been resolved separately, theyвЂ™re combined into an individual flat or structured fact and returned.
Aggregate resolutions have several key differences compared to uncomplicated resolutions, beginning with the fact declaration. To introduce an aggregate resolution, youвЂ™ll must have to incorporate the :type => :aggregate parameter:
Each individual step on the resolution then gets its have named chunk statement:
Within a quick resolution, the code always comes with a setcode statement that determines the factвЂ™s value. Aggregate resolutions never have a setcode statement. Instead, they have an optional aggregate block that brings together the chunks. Whatever value the aggregate block returns will be the factвЂ™s value. HereвЂ™s an example that just brings together the strings from the two chunks over:
If the chunk blocks either all return arrays or all return hashes, you may omit the aggregate block. When you do, Facter instantly merges all of your knowledge into 1 array or hash and use that because the factвЂ™s value.
For far more examples of aggregate resolutions, see the aggregate resolutions section with the Fact Overview web page.
Viewing fact values
If your Puppet master(s) are configured make use of PuppetDB. you may check out and search all within the facts for any node, as well as customized facts. See the PuppetDB docs for added info.
What are external facts?
External facts give you a way to utilize arbitrary executables or scripts as facts, or established facts statically with structured details. If youвЂ™ve ever wanted to put in writing a custom made fact in Perl, C, or a one-line textual content file, this is how.
The most reliable way to distribute external facts is with pluginsync, which extra assist for them in Puppet 3.four /Facter two.0.1. To include external facts to your Puppet modules, just location them in <MODULEPATH>/<MODULE>/facts.d/ .
If youвЂ™re not utilizing pluginsync, then external facts must go in the standard directory. The location of this directory varies determined by your operating technique, whether your deployment employs Puppet Company or open source releases, and whether you happen to be working as root/Administrator. When calling facter from the command line, you are able to specify the external facts directory with the --external-dir option.
Note: These directories never necessarily exist by default; you may will need to make them. If you happen to build the directory, make sure to restrict accessibility so that only Administrators can be able to write to the directory.
Inside of a module (recommended):
On Unix/Linux/OS X, there are three directories:
On Windows 2003:
When operating as a non-root / non-Administrator person:
Executable facts - Unix
Executable facts on Unix perform by dropping an executable file into the standard external fact path previously mentioned. A shebang is always required for executable facts on Unix. If the shebang is missing, the execution with the fact will fail.
An example external fact written in Python:
You must ensure that the script has its execute bit established:
For Facter to parse the output, the script must return key/value pairs on STDOUT during the format:
Applying this format, just one script can return a number of facts.
Executable facts - Windows
Executable facts on Windows get the job done by dropping an executable file into the external fact path for ones version of Windows. Unlike with Unix, the external facts interface expects Windows scripts to conclusion having a known extension. Line endings may be either LF or CRLF. In the moment the following extensions are supported:
.com and .exe. binary executables
.bat and .cmd. batch scripts
.ps1. PowerShell scripts
As with Unix facts, each individual script must return key/value pairs on STDOUT with the format:
Choosing this format, just one script can return an array of facts in a single return.
The file encoding for .bat/.cmd information must be ANSI or UTF8 without BOM (Byte Order Mark), otherwise you may get strange output.
Below is often a sample batch script which outputs facts by making use of the required format:
The encoding that should be put into use with .ps1 documents is pretty open. PowerShell determines the encoding on the file at run time.
Right here can be a sample PowerShell script which outputs facts choosing the required format:
You should be able to save and execute this PowerShell script to the command line.
Structured information facts
Facter can parse structured information data files stored from the external facts directory and established facts based upon their contents.
Structured information information must use 1 belonging to the supported information styles and must have the correct file extension. With the moment, Facter supports the following extensions and info sorts:
yaml. YAML knowledge, inside following format:
json. JSON facts, around the following format:
txt. Key value pairs, on the following format:
As with executable facts, structured facts information can established a number of facts at once.
Structured knowledge facts on Windows
All belonging to the over sorts are supported on Windows with the following caveats:
The line endings could very well be either LF or CRLF .
The file encoding must be either ANSI or UTF8 without BOM (Byte Order Mark).
If your external fact is not really appearing in FacterвЂ™s output, functioning Facter in debug mode should give you a meaningful reason and tell you which file is causing the problem:
1 example of when this may possibly happen is in cases where a fact returns invalid characters. Let say you put to use a hyphen instead of an equals sign with your script take a look at.sh :
Operating puppet facts --debug should yield a useful message:
External facts and stdlib
If you happen to track down that an external fact does not match what you have configured inside your facts.d directory, make sure you haven't defined the same fact working with the external facts abilities found inside of the stdlib module.
When external facts produce a mostly-equal way to produce variables for Puppet, they have a number of disadvantages:
An external fact cannot internally reference another fact. However, due to parse order, it is possible to reference an external fact from the Ruby fact.
External executable facts are forked instead of executed inside the same procedure.
Distributing executable facts through pluginsync requires Puppet 3.four.0 or greater.
Facter 3.four <a href=http://geacc.webs.fcm.unc.edu.ar/presentaciones-a-congresos/>payforessay</a>