mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-04 15:29:10 -07:00
added warnings list to module and autoadd
added better way of adding warnings to return data backwards compatible if warnings key already exists added deprecations made iface more generic changed to enforce type per item added logging of warnings/deprecations also display deprecations by default
This commit is contained in:
parent
d6ea400efb
commit
7e6758873c
2 changed files with 62 additions and 14 deletions
|
@ -684,6 +684,9 @@ class AnsibleModule(object):
|
||||||
# May be used to set modifications to the environment for any
|
# May be used to set modifications to the environment for any
|
||||||
# run_command invocation
|
# run_command invocation
|
||||||
self.run_command_environ_update = {}
|
self.run_command_environ_update = {}
|
||||||
|
self._warnings = []
|
||||||
|
self._deprecations = []
|
||||||
|
self._passthrough = ['warnings', 'deprecations']
|
||||||
|
|
||||||
self.aliases = {}
|
self.aliases = {}
|
||||||
self._legal_inputs = ['_ansible_check_mode', '_ansible_no_log', '_ansible_debug', '_ansible_diff', '_ansible_verbosity', '_ansible_selinux_special_fs', '_ansible_module_name', '_ansible_version', '_ansible_syslog_facility']
|
self._legal_inputs = ['_ansible_check_mode', '_ansible_no_log', '_ansible_debug', '_ansible_diff', '_ansible_verbosity', '_ansible_selinux_special_fs', '_ansible_module_name', '_ansible_version', '_ansible_syslog_facility']
|
||||||
|
@ -757,6 +760,22 @@ class AnsibleModule(object):
|
||||||
# finally, make sure we're in a sane working dir
|
# finally, make sure we're in a sane working dir
|
||||||
self._set_cwd()
|
self._set_cwd()
|
||||||
|
|
||||||
|
def warn(self, warning):
|
||||||
|
|
||||||
|
if isinstance(warning, string_types):
|
||||||
|
self._warnings.append(warning)
|
||||||
|
self.log('[WARNING] %s' % warning)
|
||||||
|
else:
|
||||||
|
raise TypeError("warn requires a string not a %s" % type(warning))
|
||||||
|
|
||||||
|
def deprecate(self, deprecate):
|
||||||
|
|
||||||
|
if isinstance(deprecate, string_types):
|
||||||
|
self._deprecations.append(deprecate)
|
||||||
|
self.log('[DEPRECATION WARNING] %s' % deprecate)
|
||||||
|
else:
|
||||||
|
raise TypeError("deprecate requires a string not a %s" % type(deprecate))
|
||||||
|
|
||||||
def load_file_common_arguments(self, params):
|
def load_file_common_arguments(self, params):
|
||||||
'''
|
'''
|
||||||
many modules deal with files, this encapsulates common
|
many modules deal with files, this encapsulates common
|
||||||
|
@ -1717,7 +1736,7 @@ class AnsibleModule(object):
|
||||||
|
|
||||||
def debug(self, msg):
|
def debug(self, msg):
|
||||||
if self._debug:
|
if self._debug:
|
||||||
self.log(msg)
|
self.log('[debug] %s' % msg)
|
||||||
|
|
||||||
def log(self, msg, log_args=None):
|
def log(self, msg, log_args=None):
|
||||||
|
|
||||||
|
@ -1888,28 +1907,53 @@ class AnsibleModule(object):
|
||||||
for path in self.cleanup_files:
|
for path in self.cleanup_files:
|
||||||
self.cleanup(path)
|
self.cleanup(path)
|
||||||
|
|
||||||
def exit_json(self, **kwargs):
|
def _return_formatted(self, kwargs):
|
||||||
''' return from the module, without error '''
|
|
||||||
self.add_path_info(kwargs)
|
self.add_path_info(kwargs)
|
||||||
if not 'changed' in kwargs:
|
|
||||||
kwargs['changed'] = False
|
|
||||||
if 'invocation' not in kwargs:
|
if 'invocation' not in kwargs:
|
||||||
kwargs['invocation'] = {'module_args': self.params}
|
kwargs['invocation'] = {'module_args': self.params}
|
||||||
|
|
||||||
|
if 'warnings' in kwargs:
|
||||||
|
if isinstance(kwargs['warnings'], list):
|
||||||
|
for w in kwargs['warnings']:
|
||||||
|
self.warn(w)
|
||||||
|
else:
|
||||||
|
self.warn(kwargs['warnings'])
|
||||||
|
if self._warnings:
|
||||||
|
kwargs['warnings'] = self._warnings
|
||||||
|
|
||||||
|
if 'deprecations' in kwargs:
|
||||||
|
if isinstance(kwargs['deprecations'], list):
|
||||||
|
for d in kwargs['deprecations']:
|
||||||
|
self.warn(d)
|
||||||
|
else:
|
||||||
|
self.warn(kwargs['deprecations'])
|
||||||
|
|
||||||
|
if self._deprecations:
|
||||||
|
kwargs['deprecations'] = self._deprecations
|
||||||
|
|
||||||
kwargs = remove_values(kwargs, self.no_log_values)
|
kwargs = remove_values(kwargs, self.no_log_values)
|
||||||
self.do_cleanup_files()
|
|
||||||
print('\n%s' % self.jsonify(kwargs))
|
print('\n%s' % self.jsonify(kwargs))
|
||||||
|
|
||||||
|
def exit_json(self, **kwargs):
|
||||||
|
''' return from the module, without error '''
|
||||||
|
|
||||||
|
if not 'changed' in kwargs:
|
||||||
|
kwargs['changed'] = False
|
||||||
|
|
||||||
|
self.do_cleanup_files()
|
||||||
|
self._return_formatted(kwargs)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
def fail_json(self, **kwargs):
|
def fail_json(self, **kwargs):
|
||||||
''' return from the module, with an error message '''
|
''' return from the module, with an error message '''
|
||||||
self.add_path_info(kwargs)
|
|
||||||
assert 'msg' in kwargs, "implementation error -- msg to explain the error is required"
|
assert 'msg' in kwargs, "implementation error -- msg to explain the error is required"
|
||||||
kwargs['failed'] = True
|
kwargs['failed'] = True
|
||||||
if 'invocation' not in kwargs:
|
|
||||||
kwargs['invocation'] = {'module_args': self.params}
|
|
||||||
kwargs = remove_values(kwargs, self.no_log_values)
|
|
||||||
self.do_cleanup_files()
|
self.do_cleanup_files()
|
||||||
print('\n%s' % self.jsonify(kwargs))
|
self._return_formatted(kwargs)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def fail_on_missing_params(self, required_params=None):
|
def fail_on_missing_params(self, required_params=None):
|
||||||
|
|
|
@ -99,9 +99,13 @@ class CallbackBase:
|
||||||
|
|
||||||
def _handle_warnings(self, res):
|
def _handle_warnings(self, res):
|
||||||
''' display warnings, if enabled and any exist in the result '''
|
''' display warnings, if enabled and any exist in the result '''
|
||||||
if C.COMMAND_WARNINGS and 'warnings' in res and res['warnings']:
|
if C.COMMAND_WARNINGS:
|
||||||
for warning in res['warnings']:
|
if 'warnings' in res and res['warnings']:
|
||||||
self._display.warning(warning)
|
for warning in res['warnings']:
|
||||||
|
self._display.warning(warning)
|
||||||
|
if 'deprecations' in res and res['deprecations']:
|
||||||
|
for warning in res['deprecations']:
|
||||||
|
self._display.deprecated(warning)
|
||||||
|
|
||||||
def _get_diff(self, difflist):
|
def _get_diff(self, difflist):
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue