Access data from structured fact in Custom Fact plugin

381 Views Asked by At
# file: module/lib/facter/custom_fact.rb

Facter.add("test_fact") do
  setcode do
      Facter.value(:my_structured_fact::key::key). # This doesn't work
  end
end

How can I reference nested levels of structured fact?

2

There are 2 best solutions below

0
Brian Luong On

Get the structured fact, then use Ruby to access the hash keys/values.

Facter.value(:my_structured_fact)[key][key]
0
azbarcea On

You can avoid undef errors with, if you know that my_structured_fact is always a Hash (also known in other programming languages as map or dict)

Facter.value('my_structured_fact').dig('key', 'key')

dig(key, ...) → objectclick to toggle source Retrieves the value object corresponding to the each key objects repeatedly. more about dig

I have also heard synonyms for a structured fact:

  • complex fact (vs a simple fact)
  • a fact with Hash value