Debian Codegraph @

lib/ansible/plugins/action/debug.py

raw
1 # Copyright 2012, Dag Wieers <[email protected]>
2 # Copyright 2016, Toshio Kuratomi <[email protected]>
3 #
4 # This file is part of Ansible
5 #
6 # Ansible is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Ansible is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
18 from __future__ import annotations
19
20 from ansible.errors import AnsibleValueOmittedError, AnsibleError
21 from ansible.module_utils.common.validation import _check_type_str_no_conversion
22 from ansible.module_utils.datatag import deprecate_value
23 from ansible.plugins.action import ActionBase, VariableLayer
24 from ansible._internal._templating._jinja_common import UndefinedMarker, TruncationMarker
25 from ansible._internal._templating._utils import Omit
26 from ansible._internal._templating._marker_behaviors import ReplacingMarkerBehavior, RoutingMarkerBehavior
27 from ansible.utils.display import Display
28
29 display = Display()
30
31
32 class ActionModule(ActionBase):
33 """
34 Emits informational messages, with special diagnostic handling of some templating failures.
35 """
36
37 TRANSFERS_FILES = False
38 _requires_connection = False
39
40 def run(self, tmp=None, task_vars=None):
41 validation_result, new_module_args = self.validate_argument_spec(
42 argument_spec=dict(
43 msg=dict(type='raw', default='Hello world!'),
44 var=dict(type=_check_type_str_no_conversion),
45 verbosity=dict(type='int', default=0),
46 ),
47 mutually_exclusive=(
48 ('msg', 'var'),
49 ),
50 )
51
52 result = super(ActionModule, self).run(tmp, task_vars)
53 del tmp # tmp no longer has any effect
54
55 # get task verbosity
56 verbosity = new_module_args['verbosity']
57
58 replacing_behavior = ReplacingMarkerBehavior()
59
60 var_behavior = RoutingMarkerBehavior({
61 UndefinedMarker: replacing_behavior,
62 TruncationMarker: replacing_behavior,
63 })
64
65 if verbosity <= self._display.verbosity:
66 if raw_var_arg := new_module_args['var']:
67 # If var name is same as result, try to template it
68 try:
69 results = self._templar._engine.extend(marker_behavior=var_behavior).evaluate_expression(raw_var_arg)
70 except AnsibleValueOmittedError as ex:
71 results = repr(Omit)
72 display.warning("The result of the `var` expression could not be omitted; a placeholder was used instead.", obj=ex.obj)
73 except Exception as ex:
74 raise AnsibleError('Error while resolving `var` expression.', obj=raw_var_arg) from ex
75
76 result[raw_var_arg] = results
77 else:
78 result['msg'] = new_module_args['msg']
79
80 # force flag to make debug output module always verbose
81 result['_ansible_verbose_always'] = True
82
83 # propagate any warnings in the task result unless we're skipping the task
84 replacing_behavior.emit_warnings()
85
86 else:
87 result['skipped_reason'] = deprecate_value(
88 'Verbosity threshold not met.',
89 msg='modules returning `skipped_reason` is deprecated.',
90 version='2.25',
91 help_text='Use the `msg` return value instead.`'
92 )
93 result['skipped'] = True # deprecated: description='returning skipped from actions/modules' core_version='2.25'
94 result['changed'] = False
95 result['msg'] = 'Verbosity threshold not met.'
96
97 result['failed'] = False
98
99 # RPFIX-5: DEPRECATION: add deferred deprecation to remove this when INJECT_FACTS_AS_VARS is gone
100 self.register_host_variables({}, VariableLayer.CACHEABLE_FACT) # ensure that legacy ansible_facts behavior is not triggered
101
102 return result