|
|
@ -49,10 +49,11 @@ simple_config_file:
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from collections import MutableMapping
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.errors import AnsibleParserError
|
|
|
|
from ansible.errors import AnsibleParserError
|
|
|
|
from ansible.module_utils._text import to_bytes, to_text
|
|
|
|
from ansible.module_utils._text import to_bytes, to_native, to_text
|
|
|
|
from ansible.plugins.inventory import BaseInventoryPlugin
|
|
|
|
from ansible.plugins.inventory import BaseInventoryPlugin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -60,14 +61,14 @@ class InventoryModule(BaseInventoryPlugin):
|
|
|
|
''' Host inventory parser for ansible using local virtualbox. '''
|
|
|
|
''' Host inventory parser for ansible using local virtualbox. '''
|
|
|
|
|
|
|
|
|
|
|
|
NAME = 'virtualbox'
|
|
|
|
NAME = 'virtualbox'
|
|
|
|
VBOX = "VBoxManage"
|
|
|
|
VBOX = b"VBoxManage"
|
|
|
|
|
|
|
|
|
|
|
|
def _query_vbox_data(self, host, property_path):
|
|
|
|
def _query_vbox_data(self, host, property_path):
|
|
|
|
ret = None
|
|
|
|
ret = None
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
cmd = [self.VBOX, 'guestproperty', 'get', host, property_path]
|
|
|
|
cmd = [self.VBOX, b'guestproperty', b'get', to_bytes(host, errors='surrogate_or_strict'), to_bytes(property_path, errors='surrogate_or_strict')]
|
|
|
|
x = Popen(cmd, stdout=PIPE)
|
|
|
|
x = Popen(cmd, stdout=PIPE)
|
|
|
|
ipinfo = x.stdout.read()
|
|
|
|
ipinfo = to_text(x.stdout.read(), errors='surrogate_or_strict')
|
|
|
|
if 'Value' in ipinfo:
|
|
|
|
if 'Value' in ipinfo:
|
|
|
|
a, ip = ipinfo.split(':', 1)
|
|
|
|
a, ip = ipinfo.split(':', 1)
|
|
|
|
ret = ip.strip()
|
|
|
|
ret = ip.strip()
|
|
|
@ -81,7 +82,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|
|
|
for host in hostvars:
|
|
|
|
for host in hostvars:
|
|
|
|
|
|
|
|
|
|
|
|
# create vars from vbox properties
|
|
|
|
# create vars from vbox properties
|
|
|
|
if data.get('query') and isinstance(data['query'], dict):
|
|
|
|
if data.get('query') and isinstance(data['query'], MutableMapping):
|
|
|
|
for varname in data['query']:
|
|
|
|
for varname in data['query']:
|
|
|
|
hostvars[host][varname] = self._query_vbox_data(host, data['query'][varname])
|
|
|
|
hostvars[host][varname] = self._query_vbox_data(host, data['query'][varname])
|
|
|
|
|
|
|
|
|
|
|
@ -168,7 +169,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
config_data = self.loader.load_from_file(path)
|
|
|
|
config_data = self.loader.load_from_file(path)
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
raise AnsibleParserError(e)
|
|
|
|
raise AnsibleParserError(to_native(e))
|
|
|
|
|
|
|
|
|
|
|
|
if not config_data or config_data.get('plugin') != self.NAME:
|
|
|
|
if not config_data or config_data.get('plugin') != self.NAME:
|
|
|
|
# this is not my config file
|
|
|
|
# this is not my config file
|
|
|
@ -182,26 +183,26 @@ class InventoryModule(BaseInventoryPlugin):
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if not source_data:
|
|
|
|
if not source_data:
|
|
|
|
pwfile = to_bytes(config_data.get('settings_password_file'))
|
|
|
|
b_pwfile = to_bytes(config_data.get('settings_password_file'), errors='surrogate_or_strict')
|
|
|
|
running = config_data.get('running_only', False)
|
|
|
|
running = config_data.get('running_only', False)
|
|
|
|
|
|
|
|
|
|
|
|
# start getting data
|
|
|
|
# start getting data
|
|
|
|
cmd = [self.VBOX, 'list', '-l']
|
|
|
|
cmd = [self.VBOX, b'list', b'-l']
|
|
|
|
if running:
|
|
|
|
if running:
|
|
|
|
cmd.append('runningvms')
|
|
|
|
cmd.append(b'runningvms')
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
cmd.append('vms')
|
|
|
|
cmd.append(b'vms')
|
|
|
|
|
|
|
|
|
|
|
|
if pwfile and os.path.exists(pwfile):
|
|
|
|
if b_pwfile and os.path.exists(b_pwfile):
|
|
|
|
cmd.append('--settingspwfile')
|
|
|
|
cmd.append(b'--settingspwfile')
|
|
|
|
cmd.append(pwfile)
|
|
|
|
cmd.append(b_pwfile)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
p = Popen(cmd, stdout=PIPE)
|
|
|
|
p = Popen(cmd, stdout=PIPE)
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
AnsibleParserError(e)
|
|
|
|
AnsibleParserError(to_native(e))
|
|
|
|
|
|
|
|
|
|
|
|
source_data = p.stdout.readlines()
|
|
|
|
source_data = p.stdout.read()
|
|
|
|
inventory.cache[cache_key] = to_text(source_data)
|
|
|
|
inventory.cache[cache_key] = to_text(source_data, errors='surrogate_or_strict')
|
|
|
|
|
|
|
|
|
|
|
|
self._populate_from_source(source_data, config_data)
|
|
|
|
self._populate_from_source(source_data.splitlines(), config_data)
|
|
|
|