|
|
@ -103,7 +103,7 @@ import os
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
from ansible.module_utils import six
|
|
|
|
from ansible.module_utils import six
|
|
|
|
from ansible.module_utils.urls import Request, urllib_error, ConnectionError, socket, httplib
|
|
|
|
from ansible.module_utils.urls import Request, urllib_error, ConnectionError, socket, httplib
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
from ansible.module_utils._text import to_text, to_native
|
|
|
|
from ansible.errors import AnsibleParserError, AnsibleOptionsError
|
|
|
|
from ansible.errors import AnsibleParserError, AnsibleOptionsError
|
|
|
|
from ansible.plugins.inventory import BaseInventoryPlugin
|
|
|
|
from ansible.plugins.inventory import BaseInventoryPlugin
|
|
|
|
from ansible.config.manager import ensure_type
|
|
|
|
from ansible.config.manager import ensure_type
|
|
|
@ -127,18 +127,18 @@ class InventoryModule(BaseInventoryPlugin):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
response = request_handler.get(tower_url)
|
|
|
|
response = request_handler.get(tower_url)
|
|
|
|
except (ConnectionError, urllib_error.URLError, socket.error, httplib.HTTPException) as e:
|
|
|
|
except (ConnectionError, urllib_error.URLError, socket.error, httplib.HTTPException) as e:
|
|
|
|
error_msg = 'Connection to remote host failed: {err}'.format(err=e)
|
|
|
|
n_error_msg = 'Connection to remote host failed: {err}'.format(err=to_native(e))
|
|
|
|
# If Tower gives a readable error message, display that message to the user.
|
|
|
|
# If Tower gives a readable error message, display that message to the user.
|
|
|
|
if callable(getattr(e, 'read', None)):
|
|
|
|
if callable(getattr(e, 'read', None)):
|
|
|
|
error_msg += ' with message: {err_msg}'.format(err_msg=e.read())
|
|
|
|
n_error_msg += ' with message: {err_msg}'.format(err_msg=to_native(e.read()))
|
|
|
|
raise AnsibleParserError(to_native(error_msg))
|
|
|
|
raise AnsibleParserError(n_error_msg)
|
|
|
|
|
|
|
|
|
|
|
|
# Attempt to parse JSON.
|
|
|
|
# Attempt to parse JSON.
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return json.loads(response.read())
|
|
|
|
return json.loads(response.read())
|
|
|
|
except (ValueError, TypeError) as e:
|
|
|
|
except (ValueError, TypeError) as e:
|
|
|
|
# If the JSON parse fails, print the ValueError
|
|
|
|
# If the JSON parse fails, print the ValueError
|
|
|
|
raise AnsibleParserError(to_native('Failed to parse json from host: {err}'.format(err=e)))
|
|
|
|
raise AnsibleParserError('Failed to parse json from host: {err}'.format(err=to_native(e)))
|
|
|
|
|
|
|
|
|
|
|
|
def verify_file(self, path):
|
|
|
|
def verify_file(self, path):
|
|
|
|
if path.endswith('@tower_inventory'):
|
|
|
|
if path.endswith('@tower_inventory'):
|
|
|
@ -167,14 +167,15 @@ class InventoryModule(BaseInventoryPlugin):
|
|
|
|
# validate type of inventory_id because we allow two types as special case
|
|
|
|
# validate type of inventory_id because we allow two types as special case
|
|
|
|
inventory_id = self.get_option('inventory_id')
|
|
|
|
inventory_id = self.get_option('inventory_id')
|
|
|
|
if isinstance(inventory_id, int):
|
|
|
|
if isinstance(inventory_id, int):
|
|
|
|
inventory_id = to_native(self.get_option('inventory_id'))
|
|
|
|
inventory_id = to_text(inventory_id, nonstring='simplerepr')
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
inventory_id = ensure_type(inventory_id, 'str')
|
|
|
|
inventory_id = ensure_type(inventory_id, 'str')
|
|
|
|
except ValueError as e:
|
|
|
|
except ValueError as e:
|
|
|
|
raise AnsibleOptionsError(
|
|
|
|
raise AnsibleOptionsError(
|
|
|
|
'Invalid type for configuration option inventory_id, '
|
|
|
|
'Invalid type for configuration option inventory_id, '
|
|
|
|
'not integer, and cannot convert to string: %s' % to_native(e))
|
|
|
|
'not integer, and cannot convert to string: {err}'.format(err=to_native(e))
|
|
|
|
|
|
|
|
)
|
|
|
|
inventory_id = inventory_id.replace('/', '')
|
|
|
|
inventory_id = inventory_id.replace('/', '')
|
|
|
|
inventory_url = '/api/v2/inventories/{inv_id}/script/?hostvars=1&towervars=1&all=1'.format(inv_id=inventory_id)
|
|
|
|
inventory_url = '/api/v2/inventories/{inv_id}/script/?hostvars=1&towervars=1&all=1'.format(inv_id=inventory_id)
|
|
|
|
inventory_url = urljoin(tower_host, inventory_url)
|
|
|
|
inventory_url = urljoin(tower_host, inventory_url)
|
|
|
|