2016-05-24 3 views
3

これは私の最初の投稿です。何か不明な点がある場合や何か不審な点がある場合は、お気軽にお問い合わせください。Pythonで動的なホストファイルを作成しようとしています

私は最初のホストファイルを管理することなく、複数の放浪のマシンを構築することができるように、動的ホストファイルを使用しようとしています。これは私がオンラインに見つけたものです:私はこれを実行すると

#!/usr/bin/env python 
# Adapted from Mark Mandel's implementation 
# https://github.com/ansible/ansible/blob/devel/plugins/inventory/vagrant.py 
import argparse 
import json 
import paramiko 
import subprocess 
import sys 


def parse_args(): 
    parser = argparse.ArgumentParser(description="Vagrant inventory script") 
    group = parser.add_mutually_exclusive_group(required=True) 
    group.add_argument('--list', action='store_true') 
    group.add_argument('--host') 
    return parser.parse_args() 


def list_running_hosts(): 
    cmd = "vagrant status --machine-readable" 
    status = subprocess.check_output(cmd.split()).rstrip() 
    hosts = [] 
    for line in status.split('\n'): 
     (_, host, key, value) = line.split(',') 
     if key == 'state' and value == 'running': 
      hosts.append(host) 
    return hosts 


def get_host_details(host): 
    cmd = "vagrant ssh-config {}".format(host) 
    p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) 
    config = paramiko.SSHConfig() 
    config.parse(p.stdout) 
    c = config.lookup(host) 
    return {'ansible_ssh_host': c['hostname'], 
      'ansible_ssh_port': c['port'], 
      'ansible_ssh_user': c['user'], 
      'ansible_ssh_private_key_file': c['identityfile'][0]} 


def main(): 
    args = parse_args() 
    if args.list: 
     hosts = list_running_hosts() 
     json.dump({'vagrant': hosts}, sys.stdout) 
    else: 
     details = get_host_details(args.host) 
     json.dump(details, sys.stdout) 

if __name__ == '__main__': 
    main() 

はしかし、私は次のエラーを取得する:

ERROR! The file inventory/vagrant.py is marked as executable, but failed to execute correctly. If this is not supposed to be an executable script, correct this with `chmod -x inventory/vagrant.py`. 
ERROR! Inventory script (inventory/vagrant.py) had an execution error: Traceback (most recent call last): 
    File "/home/sebas/Desktop/playbooks/inventory/vagrant.py", line 52, in <module> 
    main() 
    File "/home/sebas/Desktop/playbooks/inventory/vagrant.py", line 45, in main 
    hosts = list_running_hosts() 
    File "/home/sebas/Desktop/playbooks/inventory/vagrant.py", line 24, in list_running_hosts 
    (_, host, key, value) = line.split(',') 
ValueError: too many values to unpack 

ERROR! inventory/vagrant.py:4: Expected key=value host variable assignment, got: argparse 

誰も私が間違って何をしたか知っているのですか?事前に皆さんありがとう!

+0

これをチェックしましたか?http://stackoverflow.com/questions/5466618/too-many-values-to-unpack-iterating-over-a-dict-key-string-value-list –

答えて

1

は、私はこの問題はvagrant statusコマンドはVagrantfileでディレクトリ内のみで動作します、またはターゲットマシンのIDが指定されている場合ということですね。

システム上のすべてのアクティブベイグラント環境の状態を取得するには、vagrant global-statusを代わりに使用してください。しかし、グローバルステータスには欠点があります。キャッシュを使用し、マシンの状態を積極的に検証しません。

状態を確実に判断するには、まず、すべてのVMのIDをvagrant global-statusにして、これらのIDをvagrant status IDで確認する必要があります。

+0

うまくいかない: \今すぐ取得しました ファイル "/home/sebas/Desktop/playbooks/inventory/vagrant.py"、リスト21のリスト21で、リスト21は、 status = subprocess.check_output(cmd.split( '、'))。rstrip() ファイル"/usr/lib/python2.7/subprocess.py"、行567、check_outputで プロセス= popenの(STDOUT = PIPE、* popenargs、** kwargsから) ファイル「/usr/lib/python2.7/subprocess。 py "、line 711、in __init__ errread、errwrite) ファイル" /usr/lib/python2.7/subprocess.py "、行1340、_execute_child raise child_exception OSError:[Errno 2]そのようなファイルやディレクトリがありません –

+0

私の初期の仮定は完全に間違っていました。答えを編集しました。 – wombatonfire

関連する問題