Make load_params into a function that custom modules can access (#15913)
This commit is contained in:
parent
b3676392a8
commit
92e4f25066
1 changed files with 66 additions and 47 deletions
|
@ -219,10 +219,10 @@ except ImportError:
|
||||||
|
|
||||||
_literal_eval = literal_eval
|
_literal_eval = literal_eval
|
||||||
|
|
||||||
# Internal global holding passed in params and constants. This is consulted
|
# Internal global holding passed in params. This is consulted in case
|
||||||
# in case multiple AnsibleModules are created. Otherwise each AnsibleModule
|
# multiple AnsibleModules are created. Otherwise each AnsibleModule would
|
||||||
# would attempt to read from stdin. Other code should not use this directly
|
# attempt to read from stdin. Other code should not use this directly as it
|
||||||
# as it is an internal implementation detail
|
# is an internal implementation detail
|
||||||
_ANSIBLE_ARGS = None
|
_ANSIBLE_ARGS = None
|
||||||
|
|
||||||
FILE_COMMON_ARGUMENTS=dict(
|
FILE_COMMON_ARGUMENTS=dict(
|
||||||
|
@ -520,9 +520,59 @@ def is_executable(path):
|
||||||
or stat.S_IXGRP & os.stat(path)[stat.ST_MODE]
|
or stat.S_IXGRP & os.stat(path)[stat.ST_MODE]
|
||||||
or stat.S_IXOTH & os.stat(path)[stat.ST_MODE])
|
or stat.S_IXOTH & os.stat(path)[stat.ST_MODE])
|
||||||
|
|
||||||
|
def _load_params():
|
||||||
|
''' read the modules parameters and store them globally.
|
||||||
|
|
||||||
class AnsibleFallbackNotFound(Exception):
|
This function may be needed for certain very dynamic custom modules which
|
||||||
pass
|
want to process the parameters that are being handed the module. Since
|
||||||
|
this is so closely tied to the implementation of modules we cannot
|
||||||
|
guarantee API stability for it (it may change between versions) however we
|
||||||
|
will try not to break it gratuitously. It is certainly more future-proof
|
||||||
|
to call this function and consume its outputs than to implement the logic
|
||||||
|
inside it as a copy in your own code.
|
||||||
|
'''
|
||||||
|
global _ANSIBLE_ARGS
|
||||||
|
if _ANSIBLE_ARGS is not None:
|
||||||
|
buffer = _ANSIBLE_ARGS
|
||||||
|
else:
|
||||||
|
# debug overrides to read args from file or cmdline
|
||||||
|
|
||||||
|
# Avoid tracebacks when locale is non-utf8
|
||||||
|
# We control the args and we pass them as utf8
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
if os.path.isfile(sys.argv[1]):
|
||||||
|
fd = open(sys.argv[1], 'rb')
|
||||||
|
buffer = fd.read()
|
||||||
|
fd.close()
|
||||||
|
else:
|
||||||
|
buffer = sys.argv[1]
|
||||||
|
if sys.version_info >= (3,):
|
||||||
|
buffer = buffer.encode('utf-8', errors='surrogateescape')
|
||||||
|
# default case, read from stdin
|
||||||
|
else:
|
||||||
|
if sys.version_info < (3,):
|
||||||
|
buffer = sys.stdin.read()
|
||||||
|
else:
|
||||||
|
buffer = sys.stdin.buffer.read()
|
||||||
|
_ANSIBLE_ARGS = buffer
|
||||||
|
|
||||||
|
try:
|
||||||
|
params = json.loads(buffer.decode('utf-8'))
|
||||||
|
except ValueError:
|
||||||
|
# This helper used too early for fail_json to work.
|
||||||
|
print('\n{"msg": "Error: Module unable to decode valid JSON on stdin. Unable to figure out what parameters were passed", "failed": true}')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if sys.version_info < (3,):
|
||||||
|
params = json_dict_unicode_to_bytes(params)
|
||||||
|
|
||||||
|
try:
|
||||||
|
return params['ANSIBLE_MODULE_ARGS']
|
||||||
|
except KeyError:
|
||||||
|
# This helper does not have access to fail_json so we have to print
|
||||||
|
# json output on our own.
|
||||||
|
print('\n{"msg": "Error: Module unable to locate ANSIBLE_MODULE_ARGS in json data from stdin. Unable to figure out what parameters were passed", "failed": true}')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
def env_fallback(*args, **kwargs):
|
def env_fallback(*args, **kwargs):
|
||||||
''' Load value from environment '''
|
''' Load value from environment '''
|
||||||
|
@ -533,6 +583,10 @@ def env_fallback(*args, **kwargs):
|
||||||
raise AnsibleFallbackNotFound
|
raise AnsibleFallbackNotFound
|
||||||
|
|
||||||
|
|
||||||
|
class AnsibleFallbackNotFound(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AnsibleModule(object):
|
class AnsibleModule(object):
|
||||||
def __init__(self, argument_spec, bypass_checks=False, no_log=False,
|
def __init__(self, argument_spec, bypass_checks=False, no_log=False,
|
||||||
check_invalid_arguments=True, mutually_exclusive=None, required_together=None,
|
check_invalid_arguments=True, mutually_exclusive=None, required_together=None,
|
||||||
|
@ -1477,48 +1531,13 @@ class AnsibleModule(object):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def _load_params(self):
|
def _load_params(self):
|
||||||
''' read the input and set the params attribute. Sets the constants as well.'''
|
''' read the input and set the params attribute.
|
||||||
|
|
||||||
|
This method is for backwards compatibility. The guts of the function
|
||||||
|
were moved out in 2.1 so that custom modules could read the parameters.
|
||||||
|
'''
|
||||||
# debug overrides to read args from file or cmdline
|
# debug overrides to read args from file or cmdline
|
||||||
|
self.params = _load_params()
|
||||||
global _ANSIBLE_ARGS
|
|
||||||
if _ANSIBLE_ARGS is not None:
|
|
||||||
buffer = _ANSIBLE_ARGS
|
|
||||||
else:
|
|
||||||
# Avoid tracebacks when locale is non-utf8
|
|
||||||
# We control the args and we pass them as utf8
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
if os.path.isfile(sys.argv[1]):
|
|
||||||
fd = open(sys.argv[1], 'rb')
|
|
||||||
buffer = fd.read()
|
|
||||||
fd.close()
|
|
||||||
else:
|
|
||||||
buffer = sys.argv[1]
|
|
||||||
if sys.version_info >= (3,):
|
|
||||||
buffer = buffer.encode('utf-8', errors='surrogateescape')
|
|
||||||
# default case, read from stdin
|
|
||||||
else:
|
|
||||||
if sys.version_info < (3,):
|
|
||||||
buffer = sys.stdin.read()
|
|
||||||
else:
|
|
||||||
buffer = sys.stdin.buffer.read()
|
|
||||||
_ANSIBLE_ARGS = buffer
|
|
||||||
|
|
||||||
try:
|
|
||||||
params = json.loads(buffer.decode('utf-8'))
|
|
||||||
except ValueError:
|
|
||||||
# This helper used too early for fail_json to work.
|
|
||||||
print('\n{"msg": "Error: Module unable to decode valid JSON on stdin. Unable to figure out what parameters were passed", "failed": true}')
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if sys.version_info < (3,):
|
|
||||||
params = json_dict_unicode_to_bytes(params)
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.params = params['ANSIBLE_MODULE_ARGS']
|
|
||||||
except KeyError:
|
|
||||||
# This helper used too early for fail_json to work.
|
|
||||||
print('\n{"msg": "Error: Module unable to locate ANSIBLE_MODULE_ARGS and ANSIBLE_MODULE_CONSTANTS in json data from stdin. Unable to figure out what parameters were passed", "failed": true}')
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
def _log_to_syslog(self, msg):
|
def _log_to_syslog(self, msg):
|
||||||
if HAS_SYSLOG:
|
if HAS_SYSLOG:
|
||||||
|
|
Loading…
Reference in a new issue