Move module arg passing from the environment to stdin (from the wrapper to the module)

This commit is contained in:
Toshio Kuratomi 2016-04-10 12:33:16 -07:00
parent dcc5dfdf81
commit b571ecdfec
2 changed files with 111 additions and 88 deletions

View file

@ -223,23 +223,6 @@ from ansible import __version__
# Backwards compat. New code should just import and use __version__
ANSIBLE_VERSION = __version__
try:
# MODULE_COMPLEX_ARGS is an old name kept for backwards compat
MODULE_COMPLEX_ARGS = os.environ.pop('ANSIBLE_MODULE_ARGS')
except KeyError:
# This file might be used for its utility functions. So don't fail if
# running outside of a module environment (will fail in _load_params()
# instead)
MODULE_COMPLEX_ARGS = None
try:
# ARGS are for parameters given in the playbook. Constants are for things
# that ansible needs to configure controller side but are passed to all
# modules.
MODULE_CONSTANTS = os.environ.pop('ANSIBLE_MODULE_CONSTANTS')
except KeyError:
MODULE_CONSTANTS = None
FILE_COMMON_ARGUMENTS=dict(
src = dict(),
mode = dict(type='raw'),
@ -560,7 +543,6 @@ class AnsibleModule(object):
if k not in self.argument_spec:
self.argument_spec[k] = v
self._load_constants()
self._load_params()
self._set_fallbacks()
@ -1452,32 +1434,47 @@ class AnsibleModule(object):
continue
def _load_params(self):
''' read the input and set the params attribute'''
if MODULE_COMPLEX_ARGS is None:
''' read the input and set the params attribute. Sets the constants as well.'''
buffer = sys.stdin.read()
try:
params = json.loads(buffer)
except ValueError:
# This helper used too early for fail_json to work.
print('{"msg": "Error: ANSIBLE_MODULE_ARGS not found in environment. Unable to figure out what parameters were passed", "failed": true}')
print('{"msg": "Error: Module unable to decode valid JSON on stdin. Unable to figure out what parameters were passed", "failed": true}')
sys.exit(1)
params = json_dict_unicode_to_bytes(json.loads(MODULE_COMPLEX_ARGS))
if params is None:
params = dict()
self.params = params
def _load_constants(self):
''' read the input and set the constants attribute'''
if MODULE_CONSTANTS is None:
try:
self.params = params['ANSIBLE_MODULE_ARGS']
self.constants = params['ANSIBLE_MODULE_CONSTANTS']
except KeyError:
# This helper used too early for fail_json to work.
print('{"msg": "Error: ANSIBLE_MODULE_CONSTANTS not found in environment. Unable to figure out what constants were passed", "failed": true}')
print('{"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)
# Make constants into "native string"
if sys.version_info >= (3,):
constants = json_dict_bytes_to_unicode(json.loads(MODULE_CONSTANTS))
else:
constants = json_dict_unicode_to_bytes(json.loads(MODULE_CONSTANTS))
if constants is None:
constants = dict()
self.constants = constants
# import select
# buffer = ''
# while True:
# input_list = select.select([sys.stdin], [], [], 5.0)[0]
# if sys.stdin not in input_list:
# # This helper used too early for fail_json to work.
# print('{"msg": "Error: Module unable to read arguments from stdin. Unable to figure out what parameters were passed", "failed": true}')
# sys.exit(1)
# buffer += sys.stdin.read()
# if json.loads(buffer):
#
# for line in sys.stdin:
# if line is None:
# print('s')
# data = sys.stdin.read()
# if MODULE_COMPLEX_ARGS is None:
# # This helper used too early for fail_json to work.
# print('{"msg": "Error: ANSIBLE_MODULE_ARGS not found in environment. Unable to figure out what parameters were passed", "failed": true}')
# sys.exit(1)
#
# params = json_dict_unicode_to_bytes(json.loads(data))
# if params is None:
# params = dict()
# self.params = params
def _log_to_syslog(self, msg):
if HAS_SYSLOG: