From d7df072b9622b4795d50b994d499121bceab1054 Mon Sep 17 00:00:00 2001 From: Hideki Saito Date: Fri, 8 Jun 2018 04:23:13 +0900 Subject: [PATCH] Add syslog_facility parameter handling with systemd.journal (#41078) * Add syslog_facility parameter handling with systemd.journal - Fixed issue #41072 Signed-off-by: Hideki Saito --- .../fragments/syslog_facility-for-journald.yml | 5 +++++ .../rst/porting_guides/porting_guide_2.7.rst | 8 ++++++++ lib/ansible/module_utils/basic.py | 14 +++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/syslog_facility-for-journald.yml diff --git a/changelogs/fragments/syslog_facility-for-journald.yml b/changelogs/fragments/syslog_facility-for-journald.yml new file mode 100644 index 00000000000..f07ac05191b --- /dev/null +++ b/changelogs/fragments/syslog_facility-for-journald.yml @@ -0,0 +1,5 @@ +--- +bugfixes: +- Fixed runtime module to be able to handle syslog_facility properly + when python systemd module installed in a target system. + (https://github.com/ansible/ansible/pull/41078) diff --git a/docs/docsite/rst/porting_guides/porting_guide_2.7.rst b/docs/docsite/rst/porting_guides/porting_guide_2.7.rst index b27ab146fb3..4a6c407a4bc 100644 --- a/docs/docsite/rst/porting_guides/porting_guide_2.7.rst +++ b/docs/docsite/rst/porting_guides/porting_guide_2.7.rst @@ -54,6 +54,14 @@ Modules Major changes in popular modules are detailed here +* The :ref:`DEFAULT_SYSLOG_FACILITY` configuration option tells Ansible modules to use a specific + `syslog facility `_ when logging information on all + managed machines. Due to a bug with older Ansible versions, this setting did not affect machines + using journald with the systemd Python bindings installed. On those machines, Ansible log + messages were sent to ``/var/log/messages``, even if you set :ref:`DEFAULT_SYSLOG_FACILITY`. + Ansible 2.7 fixes this bug, routing all Ansible log messages according to the value set for + :ref:`DEFAULT_SYSLOG_FACILITY`. If you have :ref:`DEFAULT_SYSLOG_FACILITY` configured, the + location of remote logs on systems which use journald may change. Modules removed diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 889a2a7fb34..1191416a1c2 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -2185,7 +2185,19 @@ class AnsibleModule(object): for arg in log_args: journal_args.append((arg.upper(), str(log_args[arg]))) try: - journal.send(u"%s %s" % (module, journal_msg), **dict(journal_args)) + if HAS_SYSLOG: + # If syslog_facility specified, it needs to convert + # from the facility name to the facility code, and + # set it as SYSLOG_FACILITY argument of journal.send() + facility = getattr(syslog, + self._syslog_facility, + syslog.LOG_USER) >> 3 + journal.send(MESSAGE=u"%s %s" % (module, journal_msg), + SYSLOG_FACILITY=facility, + **dict(journal_args)) + else: + journal.send(MESSAGE=u"%s %s" % (module, journal_msg), + **dict(journal_args)) except IOError: # fall back to syslog since logging to journal failed self._log_to_syslog(syslog_msg)