4

I'm considering collect server data and in those servers Python 2.6 are pre-installed. Now I wonder if there are Python library correspond to "facter" of Ruby, not the Python "binding" for facter.

I googled about it but couldn't find any. Does anyone have any idea about this?

John
  • 6,701
  • 3
  • 34
  • 56
Yoshi
  • 405
  • 4
  • 12

4 Answers4

6

I can't see any reason why you wouldn't just use facter itself. The output format is easily consumable from within a python script.

import subprocess
import pprint

def facter2dict( lines ):
        res = {}
        for line in lines:
                k, v = line.split(' => ')
                res[k] = v
        return res

def facter():
        p = subprocess.Popen( ['facter'], stdout=subprocess.PIPE )
        p.wait()
        lines = p.stdout.readlines()
        return facter2dict( lines )

if __name__ == "__main__":
        pprint.pprint( facter() )
Andrew Walker
  • 40,984
  • 8
  • 62
  • 84
  • 3
    For those finding your way here from Google: note that Facter can also produce output in JSON and YAML formats (using the `--json` and `--yaml` flags, respectively). Python has modules available for parsing either of these formats. – larsks Oct 26 '12 at 00:40
5

Salt implements a Facter replacement called Grains.

http://docs.saltstack.org/en/latest/ref/modules/index.html#grains-data

There is also an attempt to do this called Phacter

http://code.google.com/p/speed/wiki/Phacter

I haven't tried it, however I agree with the concept. One may not want/be able to install Ruby on their system but want the similar functionality.

gregswift
  • 4,568
  • 1
  • 20
  • 8
  • Unfortunately, Phacter looks dead. Last commit was 25 April 2009 (as of this comment.) Saltstack is very very active. – Green Feb 07 '13 at 21:37
  • Joy of open source... fork on github is active as of last year. Still not very current... but leaves someone that wants to move it forward the option. https://github.com/radez/phacter – gregswift Mar 04 '13 at 04:52
  • fwiw... new version of facter itself has moved to C and supports plugable facts in whatever language. – gregswift Oct 18 '17 at 03:40
1

Somewhat new http://github.com/hihellobolke/sillyfacter/

Install using

  # Needs pip v1.5
  pip install --upgrade --allow-all-external --allow-unverified netifaces sillyfacter

You may beed to upgrade pip too

  # To upgrade pip
  pip install --ugrade pip
testytest
  • 23
  • 3
0

Here is a more compressed version of @AndrewWalker's suggestion. It also ensures ' => ' is present before splitting and removes the trailing \n :

import subprocess

p = subprocess.Popen( ['facter'], stdout=subprocess.PIPE )
p.wait()
facts = p.stdout.readlines()
# strip removes the trailing \n
facts = dict(k.split(' => ') for k in [s.strip() for s in facts if ' => ' in s])
print facts["architecture"]

I think I am going for facterpy, though. pip install facterpy, then:

import facter

facts = facter.Facter()
print facts["architecture"]
bryn
  • 3,155
  • 1
  • 16
  • 15