From 04ce71b4bd195c90658db7705ba3648f438495b6 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Fri, 10 Jun 2016 17:48:54 +0200 Subject: [PATCH] Give a module the possibility to known its own name (#16087) * Give a module the possibility to known its own name This is useful for logging and reporting and fixes the longstanding problem with syslog-messages: May 30 15:50:11 moria ansible-: Invoked with ... now becomes: Jun 1 17:32:03 moria ansible-copy: Invoked with ... This fixes #15830 * Rename the internal name from module.ansible_module_name to module._name --- lib/ansible/module_utils/basic.py | 14 +++++++++----- lib/ansible/plugins/action/__init__.py | 3 +++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 8929d7307e..362142ab7c 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -608,7 +608,7 @@ class AnsibleModule(object): self.run_command_environ_update = {} self.aliases = {} - self._legal_inputs = ['_ansible_check_mode', '_ansible_no_log', '_ansible_debug', '_ansible_diff', '_ansible_verbosity', '_ansible_selinux_special_fs', '_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'] if add_file_common_args: for k, v in FILE_COMMON_ARGUMENTS.items(): @@ -1229,8 +1229,6 @@ class AnsibleModule(object): for (k,v) in list(self.params.items()): if k == '_ansible_check_mode' and v: - if not self.supports_check_mode: - self.exit_json(skipped=True, msg="remote module does not support check mode") self.check_mode = True elif k == '_ansible_no_log': @@ -1254,6 +1252,9 @@ class AnsibleModule(object): elif k == '_ansible_version': self.ansible_version = v + elif k == '_ansible_module_name': + self._name = v + elif check_invalid_arguments and k not in self._legal_inputs: self.fail_json(msg="unsupported parameter for module: %s" % k) @@ -1261,6 +1262,9 @@ class AnsibleModule(object): if k.startswith('_ansible_'): del self.params[k] + if self.check_mode and not self.supports_check_mode: + self.exit_json(skipped=True, msg="remote module (%s) does not support check mode" % self._name) + def _count_terms(self, check): count = 0 for term in check: @@ -1537,7 +1541,7 @@ class AnsibleModule(object): def _log_to_syslog(self, msg): if HAS_SYSLOG: - module = 'ansible-%s' % os.path.basename(__file__) + module = 'ansible-%s' % self._name facility = getattr(syslog, self._syslog_facility, syslog.LOG_USER) syslog.openlog(str(module), 0, facility) syslog.syslog(syslog.LOG_INFO, msg) @@ -1553,7 +1557,7 @@ class AnsibleModule(object): if log_args is None: log_args = dict() - module = 'ansible-%s' % os.path.basename(__file__) + module = 'ansible-%s' % self._name if isinstance(module, bytes): module = module.decode('utf-8', 'replace') diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 17772e18b0..fc885909fe 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -574,6 +574,9 @@ class ActionBase(with_metaclass(ABCMeta, object)): # give the module information about the ansible version module_args['_ansible_version'] = __version__ + # give the module information about its name + module_args['_ansible_module_name'] = module_name + # set the syslog facility to be used in the module module_args['_ansible_syslog_facility'] = task_vars.get('ansible_syslog_facility', C.DEFAULT_SYSLOG_FACILITY)