Slim networkctl fact (#576)

The networkctl fact dumps every raw `networkctl --json=short` field, route and interface (including CNI veths), with addresses as byte arrays, pushing facter past its 4096 fact value limit.

- skip links networkd does not manage
- keep only state, type, MTU and config file fields per link
- render MAC and IP addresses as strings, drop routes and empty dropins
- return nil when no managed links exist

Reviewed-on: #576
Co-authored-by: unkin-agent <unkin-agent@unkin.net>
Co-committed-by: unkin-agent <unkin-agent@unkin.net>
This commit was merged in pull request #576.
This commit is contained in:
2026-11-15 18:55:12 +11:00
committed by BenVincent
parent 9a4dbd82ac
commit bd126a0d1e
+25 -2
View File
@@ -1,14 +1,37 @@
# frozen_string_literal: true
require 'facter'
require 'ipaddr'
require 'json'
# Exposes systemd-networkd link state from `networkctl --json=short`, keyed by interface name.
# Exposes systemd-networkd managed link state from `networkctl --json=short`, keyed by interface name.
module NetworkctlFact
module_function
KEYS = %w[Type Kind AdministrativeState OperationalState CarrierState OnlineState MTU
NetworkFile NetworkFileDropins NetDevFile NetDevFileDropins LinkFile].freeze
def address(addr)
return unless [2, 10].include?(addr['Family'])
"#{IPAddr.new_ntoh(addr['Address'].pack('C*'))}/#{addr['PrefixLength']}"
end
def mac(bytes)
bytes&.map { |byte| format('%02x', byte) }&.join(':')
end
def slim(iface)
out = iface.slice(*KEYS).merge('HardwareAddress' => mac(iface['HardwareAddress'])).compact
out.reject! { |_key, val| val == [] }
addrs = Array(iface['Addresses']).filter_map { |addr| address(addr) }
out['Addresses'] = addrs unless addrs.empty?
out
end
def parse(output)
JSON.parse(output).fetch('Interfaces').to_h { |iface| [iface['Name'], iface] }
managed = JSON.parse(output).fetch('Interfaces').reject { |iface| iface['AdministrativeState'] == 'unmanaged' }
managed.to_h { |iface| [iface['Name'], slim(iface)] } unless managed.empty?
end
def resolve