diff options
| author | Toshio Kuratomi <toshio@fedoraproject.org> | 2015-10-02 11:11:48 -0700 |
|---|---|---|
| committer | Toshio Kuratomi <toshio@fedoraproject.org> | 2015-10-12 13:30:27 -0700 |
| commit | 15d7f5384640c379f73265ab1bdb72c2b267e4d9 (patch) | |
| tree | 79bf458b83d93936b07a41707935ccc82a1b6f92 /lib/ansible/module_utils/basic.py | |
| parent | b49e0995cb1049cf3c9c184601f00cdd4ba59760 (diff) | |
| download | ansible-py3-module_utils-log.tar.gz | |
AnsibleModules.log() fix for python3py3-module_utils-log
Also add unittests for AnsibleModules.log()
Fixes #12586
Diffstat (limited to 'lib/ansible/module_utils/basic.py')
| -rw-r--r-- | lib/ansible/module_utils/basic.py | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index cb61f3da7b..0da00053a0 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -1262,23 +1262,41 @@ class AnsibleModule(object): log_args = dict() module = 'ansible-%s' % os.path.basename(__file__) + if isinstance(module, bytes): + module = module.decode('utf-8', 'replace') # 6655 - allow for accented characters - if isinstance(msg, unicode): - # We should never get here as msg should be type str, not unicode - msg = msg.encode('utf-8') + if not isinstance(msg, (bytes, unicode)): + raise TypeError("msg should be a string (got %s)" % type(msg)) + + # We want journal to always take text type + # syslog takes bytes on py2, text type on py3 + if sys.version_info >= (3,): + if isinstance(msg, bytes): + journal_msg = msg.decode('utf-8', 'replace') + syslog_msg = journal_msg + else: + # TODO: surrogateescape is a danger here + journal_msg = syslog_msg = msg + else: + if isinstance(msg, bytes): + journal_msg = msg.decode('utf-8', 'replace') + syslog_msg = msg + else: + journal_msg = msg + syslog_msg = msg.encode('utf-8', 'replace') - if (has_journal): + if has_journal: journal_args = [("MODULE", os.path.basename(__file__))] for arg in log_args: journal_args.append((arg.upper(), str(log_args[arg]))) try: - journal.send("%s %s" % (module, msg), **dict(journal_args)) + journal.send(u"%s %s" % (module, journal_msg), **dict(journal_args)) except IOError: # fall back to syslog since logging to journal failed - self._log_to_syslog(msg) + self._log_to_syslog(syslog_msg) else: - self._log_to_syslog(msg) + self._log_to_syslog(syslog_msg) def _log_invocation(self): ''' log that ansible ran the module ''' |
