Python version of "facter"?

I am considering the possibility of collecting server data, and Python 2.6 is pre-installed on these servers. Now I am wondering if there is a Python library matching the “facter” Ruby, and not a Python “binding” for the facter.

I was looking for her, but could not find. Does anyone know about this?

+3
source share
4 answers

I see no reason why you would not just use the fax machine itself. The output format is easily consumed from within the 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() )
+5
source

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 with the name Phacter

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

, . , / Ruby , .

+4

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

Install with

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

You can also upgrade the pip

  # To upgrade pip
  pip install --ugrade pip
+1
source

Here is a more concise version of @AndrewWalker's suggestion. It also ensures the presence of "=>" before splitting and removing the final \ 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'm going to facterpy . pip install facterpythen:

import facter

facts = facter.Facter()
print facts["architecture"]
0
source

Source: https://habr.com/ru/post/1795387/


All Articles