Add networkctl fact #573

Merged
benvin merged 1 commits from benvin/networkctl-fact into develop 2026-11-15 18:35:21 +11:00
+29
View File
@@ -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