fix: Ensure compatibility with ansible-core >= 2.19

ansible-core 2.19 changes the way templates are trusted and provides a
new way of patching module args in unit tests.

With this commit the following changes are made to ensure compatibility
with ansible-core >= 2.19:

- Mark inputs to composable as trusted to align with the new template
  trust model.
- Utilize the updated method for patching module arguments in unit tests
  if available.
- Replace direct access to the self._cache attribute with the inventory's
  cache property.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2025-04-23 11:21:51 +02:00
parent 41ee9470bd
commit 93473cdd47
6 changed files with 85 additions and 26 deletions

View File

@@ -10,16 +10,24 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
import json
from contextlib import contextmanager
from json import dumps
from typing import (
Any,
Dict,
)
from unittest import mock
from ansible.module_utils import basic
from ansible.module_utils.common.text.converters import to_bytes
def set_module_args(args):
@contextmanager
def patch_module_args(args: Dict[str, Any] | None = None):
"""prepare arguments so that they will be picked up during module creation"""
args = json.dumps({"ANSIBLE_MODULE_ARGS": args})
basic._ANSIBLE_ARGS = to_bytes(args)
args = dumps({"ANSIBLE_MODULE_ARGS": args})
with mock.patch.object(basic, "_ANSIBLE_ARGS", to_bytes(args)):
yield
class AnsibleExitJson(Exception):