Debian Codegraph @

lib/ansible/executor/task_result.py

raw
1 # Copyright: (c) 2012-2014, Michael DeHaan <[email protected]>
2
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 import collections.abc as _c
8 import functools
9 import typing as t
10
11 from ansible._internal import _collection_proxy
12 from ansible.module_utils._internal import _messages
13 from ansible.utils.display import Display
14
15 if t.TYPE_CHECKING:
16 from ansible._internal import _task
17 from ansible.inventory.host import Host
18 from ansible.playbook.task import Task
19
20
21 class CallbackTaskResult:
22 def __init__(
23 self,
24 host: Host,
25 task: Task,
26 utr: _task.UnifiedTaskResult,
27 mask_result: bool = False,
28 ) -> None:
29 self.__host = host
30 self.__task = task
31 self.__utr = utr
32 self.__mask_result = mask_result
33
34 @property
35 def host(self) -> Host:
36 """The host associated with this result."""
37 return self.__host
38
39 @property
40 def _host(self) -> Host:
41 """Use the `host` property when supporting only ansible-core 2.19 or later."""
42 # deprecated: description='Deprecate `_host` in favor of `host`' core_version='2.23'
43 return self.__host
44
45 @property
46 def task(self) -> Task:
47 """The task associated with this result."""
48 return self.__task
49
50 @property
51 def _task(self) -> Task:
52 """Use the `task` property when supporting only ansible-core 2.19 or later."""
53 # deprecated: description='Deprecate `_task` in favor of `task`' core_version='2.23'
54 return self.__task
55
56 @property
57 def task_fields(self) -> _c.Mapping[str, t.Any]:
58 """The task fields associated with this result."""
59 Display().deprecated(
60 msg="The `CallbackTaskResult.task_fields` mapping is deprecated.",
61 help_text="Use `CallbackTaskResult.task` instead.",
62 version="2.24",
63 )
64
65 return self.__task.dump_attrs()
66
67 @property
68 def _task_fields(self) -> _c.Mapping[str, t.Any]:
69 """Use the `task_fields` property when supporting only ansible-core 2.19 or later."""
70 # deprecated: description='Deprecate `_task_fields` in favor of `task`' core_version='2.23'
71 # Display().deprecated(
72 # msg="The `CallbackTaskResult._task_fields` mapping is deprecated.",
73 # help_text="Use `CallbackTaskResult.task` instead.",
74 # version="2.26",
75 # )
76
77 return self.__task.dump_attrs()
78
79 @property
80 def exception(self) -> _messages.ErrorSummary | None:
81 """The error from this task result, if any."""
82 return self.__utr.exception
83
84 @property
85 def warnings(self) -> _c.Sequence[_messages.WarningSummary]:
86 """The warnings for this task, if any."""
87 return _collection_proxy.SequenceProxy(self.__utr.warnings or [])
88
89 @property
90 def deprecations(self) -> _c.Sequence[_messages.DeprecationSummary]:
91 """The deprecation warnings for this task, if any."""
92 return _collection_proxy.SequenceProxy(self.__utr.deprecations or [])
93
94 @property
95 def task_name(self) -> str:
96 return self.task.get_name()
97
98 def is_changed(self) -> bool:
99 return self.__utr.changed
100
101 def is_skipped(self) -> bool:
102 return bool(self.__utr.skipped)
103
104 def is_failed(self) -> bool:
105 return self.__utr.failed
106
107 def is_unreachable(self) -> bool:
108 return bool(self.__utr.unreachable)
109
110 @property
111 def _result(self) -> _c.MutableMapping[str, t.Any]:
112 """Use the `result` property when supporting only ansible-core 2.19 or later."""
113 # deprecated: description='Deprecate `_result` in favor of `result`' core_version='2.23'
114 return self.result
115
116 @functools.cached_property
117 def result(self) -> _c.MutableMapping[str, t.Any]:
118 """
119 Returns a cached copy of the task result dictionary for consumption by callbacks.
120 Internal custom types are transformed to native Python types to facilitate access and serialization.
121 """
122 # RPFIX-9: FUTURE: consolidate the no_log logic earlier so we don't have to check both the task and UTR here
123 return self.__utr.as_result_dict(
124 for_callback=True,
125 censor_callback_result=self.task.no_log or self.__utr.no_log,
126 mask_callback_result=self.__mask_result,
127 )
128
129
130 TaskResult = CallbackTaskResult
131 """Compatibility name for the pre-2.19 callback-shaped TaskResult passed to callbacks."""