Debian Codegraph @

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

raw
1 # Copyright (c) 2012, Michael DeHaan, <[email protected]>
2 # Copyright (c) 2017 Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 author: Unknown (!UNKNOWN)
10 name: log_plays
11 type: notification
12 short_description: Write playbook output to log file
13 description:
14 - This callback writes playbook output to a file per host in the C(/var/log/ansible/hosts) directory.
15 requirements:
16 - Whitelist in configuration
17 - A writeable C(/var/log/ansible/hosts) directory by the user executing Ansible on the controller
18 options:
19 log_folder:
20 default: /var/log/ansible/hosts
21 description: The folder where log files are created.
22 type: str
23 env:
24 - name: ANSIBLE_LOG_FOLDER
25 ini:
26 - section: callback_log_plays
27 key: log_folder
28 """
29
30 import json
31 import os
32 import time
33 from collections.abc import MutableMapping
34
35 from ansible.module_utils.common.text.converters import to_bytes
36 from ansible.parsing.ajson import AnsibleJSONEncoder
37 from ansible.plugins.callback import CallbackBase
38 from ansible.utils.path import makedirs_safe
39
40 # NOTE: in Ansible 1.2 or later general logging is available without
41 # this plugin, just set ANSIBLE_LOG_PATH as an environment variable
42 # or log_path in the DEFAULTS section of your ansible configuration
43 # file. This callback is an example of per hosts logging for those
44 # that want it.
45
46
47 class CallbackModule(CallbackBase):
48 """
49 logs playbook results, per host, in /var/log/ansible/hosts
50 """
51
52 CALLBACK_VERSION = 2.0
53 CALLBACK_TYPE = "notification"
54 CALLBACK_NAME = "community.general.log_plays"
55 CALLBACK_NEEDS_WHITELIST = True
56
57 TIME_FORMAT = "%b %d %Y %H:%M:%S"
58
59 @staticmethod
60 def _make_msg(now, playbook, task_name, task_action, category, data):
61 return f"{now} - {playbook} - {task_name} - {task_action} - {category} - {data}\n\n"
62
63 def __init__(self):
64 super().__init__()
65
66 def set_options(self, task_keys=None, var_options=None, direct=None):
67 super().set_options(task_keys=task_keys, var_options=var_options, direct=direct)
68
69 self.log_folder = self.get_option("log_folder")
70
71 if not os.path.exists(self.log_folder):
72 makedirs_safe(self.log_folder)
73
74 def log(self, result, category):
75 data = result._result
76 if isinstance(data, MutableMapping):
77 if "_ansible_verbose_override" in data:
78 # avoid logging extraneous data
79 data = "omitted"
80 else:
81 data = data.copy()
82 invocation = data.pop("invocation", None)
83 data = json.dumps(data, cls=AnsibleJSONEncoder)
84 if invocation is not None:
85 data = f"{json.dumps(invocation)} => {data} "
86
87 path = os.path.join(self.log_folder, result._host.get_name())
88 now = time.strftime(self.TIME_FORMAT, time.localtime())
89
90 msg = to_bytes(self._make_msg(now, self.playbook, result._task.name, result._task.action, category, data))
91 with open(path, "ab") as fd:
92 fd.write(msg)
93
94 def v2_runner_on_failed(self, result, ignore_errors=False):
95 self.log(result, "FAILED")
96
97 def v2_runner_on_ok(self, result):
98 self.log(result, "OK")
99
100 def v2_runner_on_skipped(self, result):
101 self.log(result, "SKIPPED")
102
103 def v2_runner_on_unreachable(self, result):
104 self.log(result, "UNREACHABLE")
105
106 def v2_runner_on_async_failed(self, result):
107 self.log(result, "ASYNC_FAILED")
108
109 def v2_playbook_on_start(self, playbook):
110 self.playbook = playbook._file_name
111
112 def v2_playbook_on_import_for_host(self, result, imported_file):
113 self.log(result, "IMPORTED", imported_file)
114
115 def v2_playbook_on_not_import_for_host(self, result, missing_file):
116 self.log(result, "NOTIMPORTED", missing_file)