Debian Codegraph @

ansible_collections/community/general/plugins/callback/context_demo.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: context_demo
11 type: aggregate
12 short_description: Demo callback that adds play/task context
13 description:
14 - Displays some play and task context along with normal output.
15 - This is mostly for demo purposes.
16 requirements:
17 - whitelist in configuration
18 """
19
20 from ansible.plugins.callback import CallbackBase
21
22
23 class CallbackModule(CallbackBase):
24 """
25 This is a very trivial example of how any callback function can get at play and task objects.
26 play will be 'None' for runner invocations, and task will be None for 'setup' invocations.
27 """
28
29 CALLBACK_VERSION = 2.0
30 CALLBACK_TYPE = "aggregate"
31 CALLBACK_NAME = "community.general.context_demo"
32 CALLBACK_NEEDS_WHITELIST = True
33
34 def __init__(self, *args, **kwargs):
35 super().__init__(*args, **kwargs)
36 self.task = None
37 self.play = None
38
39 def v2_on_any(self, *args, **kwargs):
40 self._display.display(f"--- play: {getattr(self.play, 'name', None)} task: {self.task} ---")
41
42 self._display.display(" --- ARGS ")
43 for i, a in enumerate(args):
44 self._display.display(f" {i}: {a}")
45
46 self._display.display(" --- KWARGS ")
47 for k in kwargs:
48 self._display.display(f" {k}: {kwargs[k]}")
49
50 def v2_playbook_on_play_start(self, play):
51 self.play = play
52
53 def v2_playbook_on_task_start(self, task, is_conditional):
54 self.task = task