dev/provisioning/modules/stdlib/README_DEVELOPER.markdown
changeset 28 b0b56e0f8c7f
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 Puppet Specific Facts
       
     2 =====================
       
     3 
       
     4 Facter is meant to stand alone and apart from Puppet.  However, Facter often
       
     5 runs inside Puppet and all custom facts included in the stdlib module will
       
     6 almost always be evaluated in the context of Puppet and Facter working
       
     7 together.
       
     8 
       
     9 Still, we don't want to write custom facts that blow up in the users face if
       
    10 Puppet is not loaded in memory.  This is often the case if the user runs
       
    11 `facter` without also supplying the `--puppet` flag.
       
    12 
       
    13 Ah! But Jeff, the custom fact won't be in the `$LOAD_PATH` unless the user
       
    14 supplies `--facter`! You might say...
       
    15 
       
    16 Not (always) true I say!  If the user happens to have a CWD of
       
    17 `<modulepath>/stdlib/lib` then the facts will automatically be evaluated and
       
    18 blow up.
       
    19 
       
    20 In any event, it's pretty easy to write a fact that has no value if Puppet is
       
    21 not loaded.  Simply do it like this:
       
    22 
       
    23     Facter.add(:node_vardir) do
       
    24       setcode do
       
    25         # This will be nil if Puppet is not available.
       
    26         Facter::Util::PuppetSettings.with_puppet do
       
    27           Puppet[:vardir]
       
    28         end
       
    29       end
       
    30     end
       
    31 
       
    32 The `Facter::Util::PuppetSettings.with_puppet` method accepts a block and
       
    33 yields to it only if the Puppet library is loaded.  If the Puppet library is
       
    34 not loaded, then the method silently returns `nil` which Facter interprets as
       
    35 an undefined fact value.  The net effect is that the fact won't be set.