サーバーデータの収集を検討しており、それらのサーバーには Python 2.6 がプリインストールされています。さて、Rubyのfacterに対するPythonの「バインディング」ではなく、Rubyの「facter」に対応するPythonライブラリが存在するのだろうか。
私はそれについてグーグルで検索しましたが、何も見つかりませんでした。誰かこれについて何か考えがありますか?
Salt は、Grains と呼ばれる Facter 置換を実装します。
http://docs.saltstack.org/en/latest/ref/modules/index.html#grains-data
Phacterと呼ばれるこれを行う試みもあります
http://code.google.com/p/speed/wiki/Phacter
試したことはありませんが、コンセプトには同意します。システムにRubyをインストールしたくない/できなくても、同様の機能が必要な場合があります。
factorer 自体を使用しない理由がわかりません。出力形式は、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() )
やや新しいhttp://github.com/hihellobolke/sillyfacter/
を使用してインストール
# Needs pip v1.5
pip install --upgrade --allow-all-external --allow-unverified netifaces sillyfacter
pipもアップグレードする必要があるかもしれません
# To upgrade pip
pip install --ugrade pip
これは@AndrewWalkerの提案のより圧縮されたバージョンです。また、分割前に ' => ' が存在することを確認し、末尾の \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"]
しかし、私はfactoryに行くと思います。pip install facterpy
、 それから:
import facter
facts = facter.Facter()
print facts["architecture"]