Debian Codegraph @

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

raw
1 # Copyright (c) 2025, Max Mitschke <[email protected]>
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 from __future__ import annotations
6
7 DOCUMENTATION = r"""
8 name: print_task
9 type: aggregate
10 short_description: Prints playbook task snippet to job output
11 description:
12 - This plugin prints the currently executing playbook task to the job output.
13 version_added: 10.7.0
14 requirements:
15 - enable in configuration
16 """
17
18 EXAMPLES = r"""
19 ansible.cfg: |-
20 # Enable plugin
21 [defaults]
22 callbacks_enabled=community.general.print_task
23 """
24
25 from yaml import dump, load
26
27 try:
28 from yaml import CSafeDumper as SafeDumper
29 from yaml import CSafeLoader as SafeLoader
30 except ImportError:
31 from yaml import SafeDumper, SafeLoader # type: ignore
32
33 from ansible.plugins.callback import CallbackBase
34
35
36 class CallbackModule(CallbackBase):
37 """
38 This callback module tells you how long your plays ran for.
39 """
40
41 CALLBACK_VERSION = 2.0
42 CALLBACK_TYPE = "aggregate"
43 CALLBACK_NAME = "community.general.print_task"
44
45 CALLBACK_NEEDS_ENABLED = True
46
47 def __init__(self):
48 super().__init__()
49 self._printed_message = False
50
51 def _print_task(self, task):
52 if hasattr(task, "_ds"):
53 task_snippet = load(str([task._ds.copy()]), Loader=SafeLoader)
54 task_yaml = dump(task_snippet, sort_keys=False, Dumper=SafeDumper)
55 self._display.display(f"\n{task_yaml}\n")
56 self._printed_message = True
57
58 def v2_playbook_on_task_start(self, task, is_conditional):
59 self._printed_message = False
60
61 def v2_runner_on_start(self, host, task):
62 if not self._printed_message:
63 self._print_task(task)