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: #573 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 #573.
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user