| 1 |
# Copyright 2012, Dag Wieers <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
import typing as t |
| 20 |
|
| 21 |
from ansible._internal._templating import _jinja_bits |
| 22 |
from ansible.errors import AnsibleTemplateError |
| 23 |
from ansible.module_utils.common.validation import _check_type_list_strict |
| 24 |
from ansible.plugins.action import ActionBase |
| 25 |
from ansible._internal._templating._engine import TemplateEngine |
| 26 |
|
| 27 |
|
| 28 |
class ActionModule(ActionBase): |
| 29 |
"""Assert that one or more conditional expressions evaluate to true.""" |
| 30 |
|
| 31 |
_requires_connection = False |
| 32 |
|
| 33 |
@classmethod |
| 34 |
def finalize_task_arg(cls, name: str, value: t.Any, templar: TemplateEngine, context: t.Any) -> t.Any: |
| 35 |
if name != 'that': |
| 36 |
# `that` is the only key requiring special handling; delegate to base handling otherwise |
| 37 |
return super().finalize_task_arg(name, value, templar, context) |
| 38 |
|
| 39 |
if not isinstance(value, str): |
| 40 |
# if `that` is not a string, we don't need to attempt to resolve it as a template before validation (which will also listify it) |
| 41 |
return value |
| 42 |
|
| 43 |
# if `that` is entirely a string template, we only want to resolve to the container and avoid templating the container contents |
| 44 |
if _jinja_bits.is_possibly_all_template(value): |
| 45 |
try: |
| 46 |
templated_that = templar.resolve_to_container(value) |
| 47 |
except AnsibleTemplateError: |
| 48 |
pass |
| 49 |
else: |
| 50 |
if isinstance(templated_that, list): # only use `templated_that` if it is a list |
| 51 |
return templated_that |
| 52 |
|
| 53 |
return value |
| 54 |
|
| 55 |
def run(self, tmp=None, task_vars=None): |
| 56 |
if task_vars is None: |
| 57 |
task_vars = dict() |
| 58 |
|
| 59 |
result = super(ActionModule, self).run(tmp, task_vars) |
| 60 |
del tmp # tmp no longer has any effect |
| 61 |
|
| 62 |
validation_result, new_module_args = self.validate_argument_spec( |
| 63 |
argument_spec=dict( |
| 64 |
fail_msg=dict(type=str_or_list_of_str, aliases=['msg'], default='Assertion failed'), |
| 65 |
success_msg=dict(type=str_or_list_of_str, default='All assertions passed'), |
| 66 |
quiet=dict(type='bool', default=False), |
| 67 |
# explicitly not validating types `elements` here to let type rules for conditionals apply |
| 68 |
that=dict(type=_check_type_list_strict, required=True), |
| 69 |
), |
| 70 |
) |
| 71 |
|
| 72 |
fail_msg = new_module_args['fail_msg'] |
| 73 |
success_msg = new_module_args['success_msg'] |
| 74 |
quiet = new_module_args['quiet'] |
| 75 |
that_list = new_module_args['that'] |
| 76 |
|
| 77 |
if not quiet: |
| 78 |
result['_ansible_verbose_always'] = True |
| 79 |
|
| 80 |
for that in that_list: |
| 81 |
test_result = self._templar.evaluate_conditional(conditional=that) |
| 82 |
if not test_result: |
| 83 |
result['failed'] = True |
| 84 |
result['evaluated_to'] = test_result |
| 85 |
result['assertion'] = that |
| 86 |
|
| 87 |
result['msg'] = fail_msg |
| 88 |
|
| 89 |
return result |
| 90 |
|
| 91 |
result['changed'] = False |
| 92 |
result['msg'] = success_msg |
| 93 |
return result |
| 94 |
|
| 95 |
|
| 96 |
def str_or_list_of_str(value: t.Any) -> str | list[str]: |
| 97 |
if isinstance(value, str): |
| 98 |
return value |
| 99 |
|
| 100 |
if not isinstance(value, list) or any(not isinstance(item, str) for item in value): |
| 101 |
raise TypeError("a string or list of strings is required") |
| 102 |
|
| 103 |
return value |