2015-01-30 15:53:21 +00:00
|
|
|
#!/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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import sys
|
2017-05-09 21:38:08 +00:00
|
|
|
from subprocess import Popen, PIPE
|
2015-01-30 15:53:21 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2015-09-26 16:25:58 +00:00
|
|
|
class SetEncoder(json.JSONEncoder):
|
2017-01-30 23:01:47 +00:00
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, set):
|
|
|
|
return list(obj)
|
|
|
|
return json.JSONEncoder.default(self, obj)
|
2015-01-30 15:53:21 +00:00
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
VBOX = "VBoxManage"
|
2015-01-30 15:53:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_hosts(host=None):
|
|
|
|
|
|
|
|
returned = {}
|
|
|
|
try:
|
|
|
|
if host:
|
2015-01-30 20:09:09 +00:00
|
|
|
p = Popen([VBOX, 'showvminfo', host], stdout=PIPE)
|
2015-01-30 15:53:21 +00:00
|
|
|
else:
|
2017-05-09 21:38:08 +00:00
|
|
|
returned = {'all': set(), '_metadata': {}}
|
2015-01-30 20:09:09 +00:00
|
|
|
p = Popen([VBOX, 'list', '-l', 'vms'], stdout=PIPE)
|
2015-01-30 15:53:21 +00:00
|
|
|
except:
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
hostvars = {}
|
|
|
|
prevkey = pref_k = ''
|
|
|
|
|
2015-01-30 20:09:09 +00:00
|
|
|
for line in p.stdout.readlines():
|
2015-01-30 15:53:21 +00:00
|
|
|
|
|
|
|
try:
|
2017-05-09 21:38:08 +00:00
|
|
|
k, v = line.split(':', 1)
|
2015-01-30 15:53:21 +00:00
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if k == '':
|
|
|
|
continue
|
|
|
|
|
|
|
|
v = v.strip()
|
|
|
|
if k.startswith('Name'):
|
|
|
|
if v not in hostvars:
|
|
|
|
curname = v
|
|
|
|
hostvars[curname] = {}
|
2017-05-09 21:38:08 +00:00
|
|
|
try: # try to get network info
|
|
|
|
x = Popen([VBOX, 'guestproperty', 'get', curname, "/VirtualBox/GuestInfo/Net/0/V4/IP"], stdout=PIPE)
|
2015-01-30 20:09:09 +00:00
|
|
|
ipinfo = x.stdout.read()
|
|
|
|
if 'Value' in ipinfo:
|
2017-05-09 21:38:08 +00:00
|
|
|
a, ip = ipinfo.split(':', 1)
|
2015-01-30 15:53:21 +00:00
|
|
|
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
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
pref_k = 'vbox_' + k.strip().replace(' ', '_')
|
2015-01-30 15:53:21 +00:00
|
|
|
if k.startswith(' '):
|
|
|
|
if prevkey not in hostvars[curname]:
|
|
|
|
hostvars[curname][prevkey] = {}
|
2017-05-09 21:38:08 +00:00
|
|
|
hostvars[curname][prevkey][pref_k] = v
|
2015-01-30 15:53:21 +00:00
|
|
|
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()
|
|
|
|
|
2015-09-26 16:25:58 +00:00
|
|
|
sys.stdout.write(json.dumps(inventory, indent=2, cls=SetEncoder))
|