diff --git a/modules/libs/lib/facter/networkctl.rb b/modules/libs/lib/facter/networkctl.rb new file mode 100644 index 0000000..40623b0 --- /dev/null +++ b/modules/libs/lib/facter/networkctl.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'facter' +require 'json' + +# Exposes systemd-networkd link state from `networkctl --json=short`, keyed by interface name. +module NetworkctlFact + module_function + + def parse(output) + JSON.parse(output).fetch('Interfaces').to_h { |iface| [iface['Name'], iface] } + end + + def resolve + output = Facter::Core::Execution.execute('networkctl --json=short 2>/dev/null', on_fail: nil) + return nil if output.to_s.empty? + + parse(output) + rescue StandardError + nil + end +end + +Facter.add(:networkctl) do + confine kernel: 'Linux' + confine { Facter::Core::Execution.which('networkctl') } + confine { File.directory?('/run/systemd/netif') } + setcode { NetworkctlFact.resolve } +end