2016-05-24 2 views
3

Dies ist meine erste Post hier, also wenn Sie Fragen haben oder wenn etwas unlcear ist, zögern Sie nicht zu fragen.Versuchen, eine dynamische Host-Datei für Ansible in Python zu machen

Ich versuche, eine dynamische Host-Datei zu verwenden, damit ich mehrere vagrante Maschinen erstellen kann, ohne zuerst die Host-Datei verwalten zu müssen. Dies ist, was ich online gefunden:

#!/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() 

Allerdings, wenn ich diese laufen bekomme ich die folgende Fehlermeldung:

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 

weiß jemand, was ich falsch gemacht habe? Danke Jungs im Voraus!

+0

haben Sie dies überprüft http://stackoverflow.com/questions/5466618/too-many-values-to-unpack-iterating-over-a-dict-key-string-value-list –

Antwort

1

Ich schätze das Problem ist, dass vagrant status Befehl nur in einem Verzeichnis mit einer Vagrantfile funktioniert, oder wenn die ID eines Zielcomputers angegeben ist.

Um den Status aller aktiven Vagrant-Umgebungen auf dem System zu erhalten, sollte stattdessen vagrant global-status verwendet werden. Der globale Status hat jedoch einen Nachteil: Er verwendet einen Cache und überprüft den Zustand der Maschinen nicht aktiv.

Um den Zustand zuverlässig zu ermitteln, müssen wir zuerst die IDs aller VMs mit vagrant global-status abrufen und dann diese IDs mit vagrant status ID überprüfen.

+0

Hat nicht funktioniert: \ jetzt bekomme ich File "/home/sebas/Desktop/playbooks/inventory/vagrant.py", Zeile 21, in list_running_hosts status = subprocess.check_output (cmd.split ('')). rstrip() Datei "/usr/lib/python2.7/subprocess.py", Zeile 567, in check_output Prozess = Popen (stdout = PIPE, * popenargs, ** kwargs) Datei "/usr/lib/python2.7/subprocess. py ", Zeile 711, in __init__ errread, errrrite) Datei" /usr/lib/python2.7/subprocess.py ", Zeile 1340, in _execute_child raise child_exception OSError: [Errno 2] Keine Datei oder Verzeichnis –

+0

Hoppla, meine ursprüngliche Annahme war völlig falsch. Bearbeitete die Antwort. – wombatonfire

Verwandte Themen