Debian Codegraph @

lib/ansible/plugins/callback/tree.py

raw
1 # (c) 2012-2014, Ansible, Inc
2 # (c) 2017 Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import annotations
6
7 DOCUMENTATION = """
8 name: tree
9 type: notification
10 requirements:
11 - invoked in the command line
12 short_description: Save host events to files
13 version_added: "2.0"
14 options:
15 directory:
16 version_added: '2.11'
17 description: directory that will contain the per host JSON files. Also set by the C(--tree) option when using adhoc.
18 ini:
19 - section: callback_tree
20 key: directory
21 env:
22 - name: ANSIBLE_CALLBACK_TREE_DIR
23 default: "~/.ansible/tree"
24 type: path
25 description:
26 - "This callback is used by the Ansible (adhoc) command line option C(-t|--tree)."
27 - This produces a JSON dump of events in a directory, a file for each host, the directory used MUST be passed as a command line option.
28 deprecated:
29 why: The P(ansible.builtin.tree#callback) callback is no longer recommended due to long term instability.
30 alternatives: Use a stable callback with test coverage, or consider vendoring and/or moving this plugin to a collection.
31 removed_in: "2.23"
32 """
33
34 import os
35
36 from ansible.constants import TREE_DIR
37 from ansible.executor.task_result import CallbackTaskResult
38 from ansible.module_utils.common.text.converters import to_bytes, to_text
39 from ansible.module_utils.secrets import mask_secrets
40 from ansible.plugins.callback import CallbackBase
41 from ansible.utils.path import makedirs_safe, unfrackpath
42
43
44 class CallbackModule(CallbackBase):
45 """
46 This callback puts results into a host specific file in a directory in json format.
47 """
48
49 CALLBACK_VERSION = 2.0
50 CALLBACK_TYPE = 'aggregate'
51 CALLBACK_NAME = 'tree'
52 CALLBACK_NEEDS_ENABLED = True
53
54 ANSIBLE_SUPPORTS_MASKING = True
55
56 def set_options(self, task_keys=None, var_options=None, direct=None):
57 """ override to set self.tree """
58
59 super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)
60
61 if TREE_DIR:
62 # TREE_DIR comes from the CLI option --tree, only available for adhoc
63 self.tree = unfrackpath(TREE_DIR)
64 else:
65 self.tree = self.get_option('directory')
66
67 def write_tree_file(self, hostname, buf):
68 """ write something into treedir/hostname """
69
70 # This callback opts in to receiving unmasked results, so any secrets must be redacted before they reach the file.
71 buf = to_bytes(mask_secrets(to_text(buf)))
72 try:
73 makedirs_safe(self.tree)
74 except OSError as ex:
75 self._display.error_as_warning(f"Unable to access or create the configured directory {self.tree!r}.", exception=ex)
76
77 try:
78 path = to_bytes(os.path.join(self.tree, hostname))
79 with open(path, 'wb+') as fd:
80 fd.write(buf)
81 except OSError as ex:
82 self._display.error_as_warning(f"Unable to write to {hostname!r}'s file.", exception=ex)
83
84 def result_to_tree(self, result: CallbackTaskResult) -> None:
85 self.write_tree_file(result.host.get_name(), self._dump_results(result.result))
86
87 def v2_runner_on_ok(self, result: CallbackTaskResult) -> None:
88 self.result_to_tree(result)
89
90 def v2_runner_on_failed(self, result: CallbackTaskResult, ignore_errors: bool = False) -> None:
91 self.result_to_tree(result)
92
93 def v2_runner_on_unreachable(self, result: CallbackTaskResult) -> None:
94 self.result_to_tree(result)