Ask Your Question

Facter wildcard

asked 2018-03-26 10:56:55 -0600


for a non-Puppet related issue I need to compile a list of all bound IP-addresses. I've tried ip -o a, but I've realized Puppet's installed anyway, so utilizing facter providing data in a more structured fashion is very welcome. I'm writing a _shell_ script, since I think it's gonna be less than 28 lines (and it has to remain maintainable for fellow devops).

However, facter networking.interfaces outputs still too much, so I attempted facter networking.interfaces.*.bindings but that doesn't output anything.

Doesn't facter support wildcards, does it? If it doesn't, any suggestions for a neat approach filtering out just the addresses? grep(1) is no solution, and ideally I don't have to install any additional tools.

edit retag flag offensive close merge delete


I think what you need is a piece of structured information. Why not try Python?

weiping gravatar imageweiping ( 2018-03-26 21:15:29 -0600 )edit

Yeah, sure, Python, it isn't one of my primary languages, but I'd give it a go, since it's just a few lines. How'd I extract those information, though? I just want to retrieve all `bindings`, not the /whole/ `network.interfaces` tree.

Kai Burghardt gravatar imageKai Burghardt ( 2018-03-27 13:29:07 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-04-02 10:46:44 -0600

reidmv gravatar image

updated 2018-04-02 15:18:23 -0600

Option A

Shell script using awk.

facter networking.interfaces --json | awk -F '"' '/"bindings":/ { while($0!~ /],/){ getline; if($0~ /address/){ print $4 }}}'

Option B

Ruby script (uses the Ruby interpreter that ships with Puppet to satisfy the "don't install new software" requirement).

facter networking.interfaces --json | /opt/puppetlabs/puppet/bin/ruby -rjson -e 'JSON.parse([1].each{|k1,v1| v = v1["bindings"]; v && v.each{|v2| puts v2["address"] }}'


Facter itself does not provide a wildcard-like query specification, so there's no way for Facter to print subkeys like this by itself out of the box. Post-processing is required.

A Facter-native idea that could be explored might be to create a new custom fact which returned exactly the information you want, e.g. a fact called bound_addresses. Once created, an invocation like facter bound_addresses might return just what you're looking for. Additional complexity might come in relating to distributing the fact. If you use Puppet to distribute the fact, you would need to pass additional flags to Facter to use it (-p which is deprecated, or more explicit flags like --custom-dir).

There are other ways to "cleanly" consume and process the JSON data besides the Ruby example shown above. jq is a popular one, but unfortunately doesn't satisfy the "no additional software" requirement.

edit flag offensive delete link more


In the question I wrote “grep(1) is no solution”. This includes awk(1) [or any other text manipulation tool], since it's being the same kind of an approach.

Kai Burghardt gravatar imageKai Burghardt ( 2018-04-02 12:36:57 -0600 )edit

Modified to include a JSON-native suggestion, and provide commentary on constraints and alternatives.

reidmv gravatar imagereidmv ( 2018-04-02 14:45:40 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-03-26 10:56:55 -0600

Seen: 307 times

Last updated: Apr 02