From 368285b3516ba0befcd4b9e452aa3cbc6ada18e9 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 30 Jan 2015 10:53:21 -0500 Subject: [PATCH 1/2] initial draft virtualbox inventory script --- plugins/inventory/vbox.py | 113 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100755 plugins/inventory/vbox.py diff --git a/plugins/inventory/vbox.py b/plugins/inventory/vbox.py new file mode 100755 index 0000000000..0850a9119c --- /dev/null +++ b/plugins/inventory/vbox.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python + +# This file is part of Ansible, +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +import sys +from subprocess import check_output + +try: + import json +except ImportError: + import simplejson as json + + +VBOX="VBoxManage" + + +def get_hosts(host=None): + + returned = {} + try: + if host: + results = check_output([VBOX, 'showvminfo', host]) + else: + returned = { 'all': set(), '_metadata': {} } + results = check_output([VBOX, 'list', '-l', 'vms']) + except: + sys.exit(1) + + hostvars = {} + prevkey = pref_k = '' + + for line in results.splitlines(): + + try: + k,v = line.split(':',1) + except: + continue + + if k == '': + continue + + v = v.strip() + if k.startswith('Name'): + if v not in hostvars: + curname = v + hostvars[curname] = {} + try: # try to get network info + ip_info = check_output([VBOX, 'guestproperty', 'get', curname,"/VirtualBox/GuestInfo/Net/0/V4/IP"]) + if 'Value' in ip_info: + a,ip = ip_info.split(':',1) + hostvars[curname]['ansible_ssh_host'] = ip.strip() + except: + pass + + continue + + if not host: + if k == 'Groups': + for group in v.split('/'): + if group: + if group not in returned: + returned[group] = set() + returned[group].add(curname) + returned['all'].add(curname) + continue + + pref_k = 'vbox_' + k.strip().replace(' ','_') + if k.startswith(' '): + if prevkey not in hostvars[curname]: + hostvars[curname][prevkey] = {} + hostvars[curname][prevkey][pref_k]= v + else: + if v != '': + hostvars[curname][pref_k] = v + + prevkey = pref_k + + if not host: + returned['_metadata']['hostvars'] = hostvars + else: + returned = hostvars[host] + return returned + + +if __name__ == '__main__': + + inventory = {} + hostname = None + + if len(sys.argv) > 1: + if sys.argv[1] == "--host": + hostname = sys.argv[2] + + if hostname: + inventory = get_hosts(hostname) + else: + inventory = get_hosts() + + import pprint + print pprint.pprint(inventory) From d4a89c8ead35bc2020ae41542027a1dba44cae88 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 30 Jan 2015 15:09:09 -0500 Subject: [PATCH 2/2] moved from callback_output to popen to keep 2.6 compat --- plugins/inventory/vbox.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/plugins/inventory/vbox.py b/plugins/inventory/vbox.py index 0850a9119c..ff31785d7e 100755 --- a/plugins/inventory/vbox.py +++ b/plugins/inventory/vbox.py @@ -16,7 +16,7 @@ # along with Ansible. If not, see . import sys -from subprocess import check_output +from subprocess import Popen,PIPE try: import json @@ -32,17 +32,17 @@ def get_hosts(host=None): returned = {} try: if host: - results = check_output([VBOX, 'showvminfo', host]) + p = Popen([VBOX, 'showvminfo', host], stdout=PIPE) else: returned = { 'all': set(), '_metadata': {} } - results = check_output([VBOX, 'list', '-l', 'vms']) + p = Popen([VBOX, 'list', '-l', 'vms'], stdout=PIPE) except: sys.exit(1) hostvars = {} prevkey = pref_k = '' - for line in results.splitlines(): + for line in p.stdout.readlines(): try: k,v = line.split(':',1) @@ -58,9 +58,10 @@ def get_hosts(host=None): curname = v hostvars[curname] = {} try: # try to get network info - ip_info = check_output([VBOX, 'guestproperty', 'get', curname,"/VirtualBox/GuestInfo/Net/0/V4/IP"]) - if 'Value' in ip_info: - a,ip = ip_info.split(':',1) + x = Popen([VBOX, 'guestproperty', 'get', curname,"/VirtualBox/GuestInfo/Net/0/V4/IP"],stdout=PIPE) + ipinfo = x.stdout.read() + if 'Value' in ipinfo: + a,ip = ipinfo.split(':',1) hostvars[curname]['ansible_ssh_host'] = ip.strip() except: pass