Fix python3 and nonascii handling in inventory plugins

Fixes #30663
This commit is contained in:
Toshio Kuratomi 2017-09-20 17:01:07 -07:00
parent f12c6e0946
commit 72bdf7aa7e
6 changed files with 41 additions and 41 deletions

View file

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