Python版"facter"?

ymo*_*poo 4 python facter

我正在考虑收集服务器数据,并在那些服务器中预安装Python 2.6.现在我想知道是否有Python库对应Ruby的"facter",而不是Python的"绑定"因为facter.

我用Google搜索但却找不到任何东西.有没有人对此有任何想法?

And*_*ker 5

我看不出为什么你不会只使用facter本身的任何理由.输出格式很容易从python脚本中获取.

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() )
Run Code Online (Sandbox Code Playgroud)

  • 对于那些从谷歌找到你的方式:请注意,Facter还可以生成JSON和YAML格式的输出(分别使用`--json`和`--yaml`标志).Python有可用于解析这些格式之一的模块. (2认同)

gre*_*ift 5

Salt实现了称为谷物的Facter替代品。

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

还有一种尝试做到这一点的方法叫做Phacter

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

我没有尝试过,但是我同意这个概念。一个人可能不想/不能在他们的系统上安装Ruby,但是想要类似的功能。