Uploaded image for project: 'Facter'
  1. Facter
  2. FACT-3067

Facter API does not resolve custom facts that partial match legacy facts

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved
    • Normal
    • Resolution: Fixed
    • None
    • FACT 4.2.5
    • Facter 4
    • Night's Watch
    • 3
    • NW - 2021-09-22
    • Needs Assessment
    • Bug Fix
    • Hide
      Before this fix, custom facts with names that partially matched core legacy facts were being resolved as expected in CLI but not using the API. Due to the nature of how legacy facts are defined in Facter 4, with the regular expression used in FACT_NAME, `Facter::FactManager` was not resolving custom facts if core or external facts had any results.

      This fix adds a new method to check if the user query matches the name of already resolved facts to decide whether or not custom facts should be also resolved.
      Show
      Before this fix, custom facts with names that partially matched core legacy facts were being resolved as expected in CLI but not using the API. Due to the nature of how legacy facts are defined in Facter 4, with the regular expression used in FACT_NAME, `Facter::FactManager` was not resolving custom facts if core or external facts had any results. This fix adds a new method to check if the user query matches the name of already resolved facts to decide whether or not custom facts should be also resolved.
    • Needs Assessment

    Description

      This works on Facter 3.

      Facter defines a couple of legacy facts using regexes, example networking: https://github.com/puppetlabs/facter/blob/main/lib/facter/facts/macosx/network_interfaces.rb#L6

      Adding the following custom_fact:

      Facter.add('network_nexthop_ip') do
        setcode do
          'custom_value'
        end
      end
      

      Will result in the fact being resolved on CLI but not when using Facter.value / Facter.fact

      ❯ bx facter network_nexthop_ip --custom-dir $custom_dir --debug
      [2021-08-26 09:41:40.066702 ] INFO Facter - executed with command line: network_nexthop_ip --custom-dir /Users/gheorghe.popescu/Workspace/custom_facts --debug
      [2021-08-26 09:41:40.066788 ] DEBUG Facter - Facter version: 4.2.4
      [2021-08-26 09:41:40.068471 ] DEBUG Facter::FactManager - Resolving facts sequentially
      [2021-08-26 09:41:40.069033 ] DEBUG Facter::FactLoader - Loading all internal facts
      [2021-08-26 09:41:40.069164 ] DEBUG Facter::FactLoader - Loading custom facts
      [2021-08-26 09:41:40.077111 ] DEBUG Facter::FactLoader - Loading external facts
      [2021-08-26 09:41:40.079167 ] DEBUG Facter::QueryParser - List of resolvable facts: [#<Facter::SearchedFact:0x00007fbfc42e4470 @name="network_.*", @fact_class=Facts::Macosx::NetworkInterfaces, @user_query="network_nexthop_ip", @type=:legacy, @file=nil>, #<Facter::SearchedFact:0x00007fbfc90eabb0 @name="network_nexthop_ip", @fact_class=nil, @user_query="network_nexthop_ip", @type=:custom, @file=nil>]
      [2021-08-26 09:41:40.079312 ] DEBUG Facter::Core::Execution::Posix - Executing command: ifconfig -a
      [2021-08-26 09:41:40.092197 ] DEBUG Facter::Core::Execution::Posix - Executing command: ipconfig getoption en0 server_identifier
      [2021-08-26 09:41:40.100080 ] DEBUG Facter::Core::Execution::Posix - Executing command: ipconfig getoption awdl0 server_identifier
      [2021-08-26 09:41:40.107701 ] DEBUG Facter::Core::Execution::Posix - Executing command: ipconfig getoption llw0 server_identifier
      [2021-08-26 09:41:40.118377 ] DEBUG Facter::Core::Execution::Posix - Executing command: route -n get default
      [2021-08-26 09:41:40.128626 ] DEBUG Facter::Util::Fact - custom fact network_nexthop_ip got resolved from: ["/Users/gheorghe.popescu/Workspace/custom_facts/ipaddress.rb", 25]
      [2021-08-26 09:41:40.129107 ] DEBUG Facter::FactLoader - Loading all internal facts
      [2021-08-26 09:41:40.130886 ] DEBUG Facter::QueryParser - List of resolvable facts: [#<Facter::SearchedFact:0x00007fbfc40f51c8 @name="network_.*", @fact_class=Facts::Macosx::NetworkInterfaces, @user_query="network_nexthop_ip", @type=:legacy, @file=nil>]
      [2021-08-26 09:41:40.131228 ] DEBUG Facter::FactManager - fact "network_lo0" has resolved to: 127.0.0.0
      [2021-08-26 09:41:40.131252 ] DEBUG Facter::FactManager - fact "network_en0" has resolved to: 192.168.100.0
      [2021-08-26 09:41:40.131264 ] DEBUG Facter::FactManager - fact "network_nexthop_ip" has resolved to: custom_value
      [2021-08-26 09:41:40.131390 ] DEBUG Facter::LegacyFactFormatter - Formatting for single user query
      custom_value
      

      Facter.value
       
      froce_dot: false
      4.2.4
       
       
      D, [2021-08-26T09:43:09.389815 #44494] DEBUG -- : Facter::FactManager - Resolving facts sequentially
      D, [2021-08-26T09:43:09.389872 #44494] DEBUG -- : Facter::FactManager - resolving fact with user_query: network_nexthop_ip
      D, [2021-08-26T09:43:09.390443 #44494] DEBUG -- : Facter::FactManager - Searching fact: network_nexthop_ip in file: network_nexthop_ip.rb
      D, [2021-08-26T09:43:09.390938 #44494] DEBUG -- : Facter::FactManager - Searching fact: network_nexthop_ip in core facts and external facts
      D, [2021-08-26T09:43:09.390965 #44494] DEBUG -- : Facter::FactLoader - Loading all internal facts
      D, [2021-08-26T09:43:09.392238 #44494] DEBUG -- : Facter::QueryParser - List of resolvable facts: [#<Facter::SearchedFact:0x00007f8a458d08c0 @name="network_.*", @fact_class=Facts::Macosx::NetworkInterfaces, @user_query="network_nexthop_ip", @type=:legacy, @file=nil>]
      D, [2021-08-26T09:43:09.392640 #44494] DEBUG -- : Facter::Core::Execution::Posix - Executing command: ifconfig -a
      D, [2021-08-26T09:43:09.403237 #44494] DEBUG -- : Facter::Core::Execution::Posix - Executing command: ipconfig getoption en0 server_identifier
      D, [2021-08-26T09:43:09.410192 #44494] DEBUG -- : Facter::Core::Execution::Posix - Executing command: ipconfig getoption awdl0 server_identifier
      D, [2021-08-26T09:43:09.416527 #44494] DEBUG -- : Facter::Core::Execution::Posix - Executing command: ipconfig getoption llw0 server_identifier
      D, [2021-08-26T09:43:09.424148 #44494] DEBUG -- : Facter::Core::Execution::Posix - Executing command: route -n get default
      D, [2021-08-26T09:43:09.432659 #44494] DEBUG -- : Facter::FactLoader - Loading external facts
      D, [2021-08-26T09:43:09.437615 #44494] DEBUG -- : Facter::FactManager - fact "network_lo0" has resolved to: 127.0.0.0
      D, [2021-08-26T09:43:09.437645 #44494] DEBUG -- : Facter::FactManager - fact "network_en0" has resolved to: 192.168.100.0
      network_nexthop_ip ->
      
      

      Attachments

        Activity

          People

            luchian.nemes Luchian Nemes
            gheorghe.popescu Gheorghe Popescu
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Zendesk Support