From 9c3704bd00d67d1419e8058a21c99592f61df8a7 Mon Sep 17 00:00:00 2001 From: andrewroffey <36812998+andrewroffey@users.noreply.github.com> Date: Thu, 3 Mar 2022 16:33:29 +1000 Subject: [PATCH] =?UTF-8?q?[plugins/callback/syslog=5Fjson.py]=20use=20v2?= =?UTF-8?q?=20api,=20add=20option=20to=20skip=20sysl=E2=80=A6=20(#4223)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [plugins/callback/syslog_json.py] use v2 api, add option to skip syslog on gather_facts * Update plugins/callback/syslog_json.py Add version Co-authored-by: Felix Fontein * Update plugins/callback/syslog_json.py consistency for description Co-authored-by: Felix Fontein * Update plugins/callback/syslog_json.py ANSIBLE_ environment variable prefix Co-authored-by: Felix Fontein * add changelog fragment for PR 4223 * Update changelogs/fragments/4223-syslog-json-skip-syslog-option.yml Co-authored-by: Felix Fontein --- .../4223-syslog-json-skip-syslog-option.yml | 2 + plugins/callback/syslog_json.py | 39 +++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/4223-syslog-json-skip-syslog-option.yml diff --git a/changelogs/fragments/4223-syslog-json-skip-syslog-option.yml b/changelogs/fragments/4223-syslog-json-skip-syslog-option.yml new file mode 100644 index 0000000000..4850a3a967 --- /dev/null +++ b/changelogs/fragments/4223-syslog-json-skip-syslog-option.yml @@ -0,0 +1,2 @@ +minor_changes: + - syslog_json - add option to skip logging of ``gather_facts`` playbook tasks; use v2 callback API (https://github.com/ansible-collections/community.general/pull/4223). diff --git a/plugins/callback/syslog_json.py b/plugins/callback/syslog_json.py index f4865f2a26..0d71545495 100644 --- a/plugins/callback/syslog_json.py +++ b/plugins/callback/syslog_json.py @@ -41,6 +41,16 @@ DOCUMENTATION = ''' ini: - section: callback_syslog_json key: syslog_facility + setup: + description: Log setup tasks. + env: + - name: ANSIBLE_SYSLOG_SETUP + type: bool + default: true + ini: + - section: callback_syslog_json + key: syslog_setup + version_added: 4.5.0 ''' import os @@ -86,23 +96,36 @@ class CallbackModule(CallbackBase): self.logger.addHandler(self.handler) self.hostname = socket.gethostname() - def runner_on_failed(self, host, res, ignore_errors=False): + def v2_runner_on_failed(self, result, ignore_errors=False): + res = result._result + host = result._host.get_name() self.logger.error('%s ansible-command: task execution FAILED; host: %s; message: %s', self.hostname, host, self._dump_results(res)) - def runner_on_ok(self, host, res): - self.logger.info('%s ansible-command: task execution OK; host: %s; message: %s', self.hostname, host, self._dump_results(res)) + def v2_runner_on_ok(self, result): + res = result._result + host = result._host.get_name() + if result._task.action != "gather_facts" or self.get_option("setup"): + self.logger.info('%s ansible-command: task execution OK; host: %s; message: %s', self.hostname, host, self._dump_results(res)) - def runner_on_skipped(self, host, item=None): + def v2_runner_on_skipped(self, result): + host = result._host.get_name() self.logger.info('%s ansible-command: task execution SKIPPED; host: %s; message: %s', self.hostname, host, 'skipped') - def runner_on_unreachable(self, host, res): + def v2_runner_on_unreachable(self, result): + res = result._result + host = result._host.get_name() self.logger.error('%s ansible-command: task execution UNREACHABLE; host: %s; message: %s', self.hostname, host, self._dump_results(res)) - def runner_on_async_failed(self, host, res, jid): + def v2_runner_on_async_failed(self, result): + res = result._result + host = result._host.get_name() + jid = result._result.get('ansible_job_id') self.logger.error('%s ansible-command: task execution FAILED; host: %s; message: %s', self.hostname, host, self._dump_results(res)) - def playbook_on_import_for_host(self, host, imported_file): + def v2_playbook_on_import_for_host(self, result, imported_file): + host = result._host.get_name() self.logger.info('%s ansible-command: playbook IMPORTED; host: %s; message: imported file %s', self.hostname, host, imported_file) - def playbook_on_not_import_for_host(self, host, missing_file): + def v2_playbook_on_not_import_for_host(self, result, missing_file): + host = result._host.get_name() self.logger.info('%s ansible-command: playbook NOT IMPORTED; host: %s; message: missing file %s', self.hostname, host, missing_file)