From efe15ec0789311100dd8dd015639062451f450ab Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sun, 15 Nov 2026 18:35:20 +1100 Subject: [PATCH] Add networkctl fact (#573) Hosts running systemd-networkd expose link, address, route and DNS state only through `networkctl`, so none of it is visible in PuppetDB for querying or reporting. - add `networkctl` fact from `networkctl --json=short`, keyed by interface name - confine to Linux hosts with `networkctl` and a running networkd (`/run/systemd/netif`) - return nil on missing output or parse failure so puppet runs never break Reviewed-on: https://git.unkin.net/unkin/puppet-prod/pulls/573 Co-authored-by: unkin-agent Co-committed-by: unkin-agent --- modules/libs/lib/facter/networkctl.rb | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 modules/libs/lib/facter/networkctl.rb 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