Skip to content

Commit

Permalink
Give a module the possibility to known its own name (ansible#16087)
Browse files Browse the repository at this point in the history
* 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-<stdin>: Invoked with ...

now becomes:

    Jun  1 17:32:03 moria ansible-copy: Invoked with ...

This fixes ansible#15830

* Rename the internal name from module.ansible_module_name to module._name
  • Loading branch information
dagwieers authored and bcoca committed Jun 10, 2016
1 parent a529a60 commit 04ce71b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/ansible/module_utils/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def __init__(self, argument_spec, bypass_checks=False, no_log=False,
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():
Expand Down Expand Up @@ -1229,8 +1229,6 @@ def _check_arguments(self, check_invalid_arguments):
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':
Expand All @@ -1254,13 +1252,19 @@ def _check_arguments(self, check_invalid_arguments):
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)

#clean up internal params:
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:
Expand Down Expand Up @@ -1537,7 +1541,7 @@ def _load_params(self):

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)
Expand All @@ -1553,7 +1557,7 @@ def log(self, msg, log_args=None):
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')

Expand Down
3 changes: 3 additions & 0 deletions lib/ansible/plugins/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ def _execute_module(self, module_name=None, module_args=None, tmp=None, task_var
# 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)

Expand Down

0 comments on commit 04ce71b

Please sign in to comment.