0
# 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?

azbarcea
  • 3,323
  • 1
  • 20
  • 25
Brian Luong
  • 538
  • 3
  • 15

2 Answers2

1

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

Facter.value(:my_structured_fact)[key][key]
Brian Luong
  • 538
  • 3
  • 15
0

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
azbarcea
  • 3,323
  • 1
  • 20
  • 25