Structured Facts with Facter 2

Structured facts in facter had become the Puppet communities version of ‘Duke Nukem Forever’, something that’s always been just around the next corner. Now that the facter 2.0.1 release candidate is out you can finally get your hands on an early version and do some experimentation.

First we grab a version of facter 2 that supports structured facts from puppetlabs -

 # our play ground
 mkdir /tmp/facter && cd /tmp/facter

 # grab the code
 wget https://downloads.puppetlabs.com/facter/facter-2.0.1-rc1.tar.gz

 cd facter-2.0.1-rc1/

 # check facter runs from our expanded archive
 ruby -I lib bin/facter
  

This is the part where we can be underwhelmed, it’s all still flat. Don’t let the lack of nested facts dishearten you though. The Puppetlabs people have done all the hard work of implementing structured facts support, they’ve just not converted any showcase facts over yet. Instead of waiting for an official structured fact lets add our own and have a little play.

As we’re experimenting with a throw away environment we’ll drop the structured fact directly in to our expanded archive. In a real environment you’d never do this, you’d either use FACTERLIB or deploy your modules properly with puppet as Luke intended.

 # install the plugin
 wget https://raw.github.com/deanwilson/unixdaemon-puppet_facts/master/lib/facter/yumplugins.rb -O lib/facter/yumplugins.rb

 # and run it
 ruby -I lib bin/facter  yumplugins

 pluginblacklistlangpacksprestorefresh-packagekitwhiteoutdisabledblacklistwhiteoutenabledlangpacksprestorefresh-packagekit

Well, our first TODO will be to determine how to show structured facts as strings, but we’ll defer that for now as we really want to see some deep nesting. Assuming you’re on a RedHat osfamily host you can run facter with the yaml output, otherwise you’ll have to settle for the sample outputs below:

 $ ruby -I lib bin/facter yumplugins --yaml

--- 
yumplugins: 
  plugin: 
  - blacklist
  - langpacks
  - presto
  - refresh-packagekit
  - whiteout
  enabled: 
  - langpacks
  - presto
  - refresh-packagekit
  disabled: 
  - blacklist
  - whiteout

  # and now try it as json

 $ ruby -I lib ./bin/facter yumplugins -j
{
  "yumplugins": {
    "plugin": [
      "blacklist",
      "langpacks",
      "presto",
      "refresh-packagekit",
      "whiteout"
    ],
    "enabled": [
      "langpacks",
      "presto",
      "refresh-packagekit"
    ],
    "disabled": [
      "blacklist",
      "whiteout"
    ]
  }
}

Success! Structured fact output! From (nearly) Puppet! Of course, this is only a release candidate for Facter 2 so we’re not production ready yet but as a taster of what’s coming and a way to get ahead and start converting your own facts it’s a lovely, and amazingly overdue, gift.

As for writing structured facts, as you can see from my structured yumplugins fact example there’s no difference between a structured and an unstructured one apart from the value it returns.