Debian Codegraph @

ansible_collections/community/general/plugins/callback/syslog_json.py

raw
1 # Copyright (c) 2017 Ansible Project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Make coding more python3-ish
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 author: Unknown (!UNKNOWN)
10 name: syslog_json
11 type: notification
12 requirements:
13 - whitelist in configuration
14 short_description: Sends JSON events to syslog
15 description:
16 - This plugin logs ansible-playbook and ansible runs to a syslog server in JSON format.
17 options:
18 server:
19 description: Syslog server that receives the event.
20 type: str
21 env:
22 - name: SYSLOG_SERVER
23 default: localhost
24 ini:
25 - section: callback_syslog_json
26 key: syslog_server
27 port:
28 description: Port on which the syslog server is listening.
29 type: int
30 env:
31 - name: SYSLOG_PORT
32 default: 514
33 ini:
34 - section: callback_syslog_json
35 key: syslog_port
36 facility:
37 description: Syslog facility to log as.
38 type: str
39 env:
40 - name: SYSLOG_FACILITY
41 default: user
42 ini:
43 - section: callback_syslog_json
44 key: syslog_facility
45 setup:
46 description: Log setup tasks.
47 env:
48 - name: ANSIBLE_SYSLOG_SETUP
49 type: bool
50 default: true
51 ini:
52 - section: callback_syslog_json
53 key: syslog_setup
54 version_added: 4.5.0
55 """
56
57 import logging
58 import logging.handlers
59 import socket
60
61 from ansible.plugins.callback import CallbackBase
62
63
64 class CallbackModule(CallbackBase):
65 """
66 logs ansible-playbook and ansible runs to a syslog server in json format
67 """
68
69 CALLBACK_VERSION = 2.0
70 CALLBACK_TYPE = "notification"
71 CALLBACK_NAME = "community.general.syslog_json"
72 CALLBACK_NEEDS_WHITELIST = True
73
74 def __init__(self):
75 super().__init__()
76
77 def set_options(self, task_keys=None, var_options=None, direct=None):
78 super().set_options(task_keys=task_keys, var_options=var_options, direct=direct)
79
80 syslog_host = self.get_option("server")
81 syslog_port = int(self.get_option("port"))
82 syslog_facility = self.get_option("facility")
83
84 self.logger = logging.getLogger("ansible logger")
85 self.logger.setLevel(logging.DEBUG)
86
87 self.handler = logging.handlers.SysLogHandler(address=(syslog_host, syslog_port), facility=syslog_facility)
88 self.logger.addHandler(self.handler)
89 self.hostname = socket.gethostname()
90
91 def v2_runner_on_failed(self, result, ignore_errors=False):
92 res = result._result
93 host = result._host.get_name()
94 self.logger.error(
95 "%s ansible-command: task execution FAILED; host: %s; message: %s",
96 self.hostname,
97 host,
98 self._dump_results(res),
99 )
100
101 def v2_runner_on_ok(self, result):
102 res = result._result
103 host = result._host.get_name()
104 if result._task.action != "gather_facts" or self.get_option("setup"):
105 self.logger.info(
106 "%s ansible-command: task execution OK; host: %s; message: %s",
107 self.hostname,
108 host,
109 self._dump_results(res),
110 )
111
112 def v2_runner_on_skipped(self, result):
113 host = result._host.get_name()
114 self.logger.info(
115 "%s ansible-command: task execution SKIPPED; host: %s; message: %s", self.hostname, host, "skipped"
116 )
117
118 def v2_runner_on_unreachable(self, result):
119 res = result._result
120 host = result._host.get_name()
121 self.logger.error(
122 "%s ansible-command: task execution UNREACHABLE; host: %s; message: %s",
123 self.hostname,
124 host,
125 self._dump_results(res),
126 )
127
128 def v2_runner_on_async_failed(self, result):
129 res = result._result
130 host = result._host.get_name()
131 # jid = result._result.get("ansible_job_id")
132 self.logger.error(
133 "%s ansible-command: task execution FAILED; host: %s; message: %s",
134 self.hostname,
135 host,
136 self._dump_results(res),
137 )
138
139 def v2_playbook_on_import_for_host(self, result, imported_file):
140 host = result._host.get_name()
141 self.logger.info(
142 "%s ansible-command: playbook IMPORTED; host: %s; message: imported file %s",
143 self.hostname,
144 host,
145 imported_file,
146 )
147
148 def v2_playbook_on_not_import_for_host(self, result, missing_file):
149 host = result._host.get_name()
150 self.logger.info(
151 "%s ansible-command: playbook NOT IMPORTED; host: %s; message: missing file %s",
152 self.hostname,
153 host,
154 missing_file,
155 )