Creating custom facts
While Facter's built-in facts are useful, it's actually quite easy to add your own facts. For example, if you have machines in different data centers or hosting providers, you could add a custom fact for this so that Puppet can determine if any local settings need to be applied (for example, local DNS servers or network routes).
How to do it…
Here's an example of a simple custom fact:
Run the following command:
ubuntu@cookbook:~/puppet$ mkdir -p modules/facts/lib/facter
Create the file
modules/facts/lib/facter/hello.rb
with the following contents:Facter.add(:hello) do setcode do "Hello, world" end end
Modify your
manifests/nodes.pp
file as follows:node 'cookbook' { notify { $::hello: } }
Run Puppet:
ubuntu@cookbook:~/puppet$ papply Notice: Hello, world Notice: /Stage[main]//Node[cookbook]/Notify[Hello, world]/message: defined 'message' as 'Hello, world' Notice: Finished catalog run in 0.24 seconds
How it works...
The built-in facts in Facter are defined in the same...