Make ready for data tagging (#9833)

* Fix dependent lookup.

* Fix ansible_type plugin utils and adjust documentation of reveal_ansible_type filter and ansible_type test.

* Fix diy callback plugin.

* Adjust to Data Tagging.

* Vendor and use internal code from ansible-core to fix YAML callback.

Ref: https://github.com/ansible/ansible/issues/84781
This commit is contained in:
Felix Fontein 2025-04-14 19:04:26 +02:00 committed by GitHub
parent 1375cb65d6
commit 04cfce78ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 346 additions and 139 deletions

View file

@ -8,17 +8,31 @@ __metaclass__ = type
from ansible.errors import AnsibleFilterError
from ansible.module_utils.common._collections_compat import Mapping
try:
# Introduced with Data Tagging (https://github.com/ansible/ansible/pull/84621):
from ansible.module_utils.datatag import native_type_name as _native_type_name
except ImportError:
_native_type_name = None
def _atype(data, alias):
def _atype(data, alias, *, use_native_type: bool = False):
"""
Returns the name of the type class.
"""
data_type = type(data).__name__
if use_native_type and _native_type_name:
data_type = _native_type_name(data)
else:
data_type = type(data).__name__
# The following types were introduced with Data Tagging (https://github.com/ansible/ansible/pull/84621):
if data_type == "_AnsibleLazyTemplateDict":
data_type = "dict"
elif data_type == "_AnsibleLazyTemplateList":
data_type = "list"
return alias.get(data_type, data_type)
def _ansible_type(data, alias):
def _ansible_type(data, alias, *, use_native_type: bool = False):
"""
Returns the Ansible data type.
"""
@ -30,16 +44,16 @@ def _ansible_type(data, alias):
msg = "The argument alias must be a dictionary. %s is %s"
raise AnsibleFilterError(msg % (alias, type(alias)))
data_type = _atype(data, alias)
data_type = _atype(data, alias, use_native_type=use_native_type)
if data_type == 'list' and len(data) > 0:
items = [_atype(i, alias) for i in data]
items = [_atype(i, alias, use_native_type=use_native_type) for i in data]
items_type = '|'.join(sorted(set(items)))
return ''.join((data_type, '[', items_type, ']'))
if data_type == 'dict' and len(data) > 0:
keys = [_atype(i, alias) for i in data.keys()]
vals = [_atype(i, alias) for i in data.values()]
keys = [_atype(i, alias, use_native_type=use_native_type) for i in data.keys()]
vals = [_atype(i, alias, use_native_type=use_native_type) for i in data.values()]
keys_type = '|'.join(sorted(set(keys)))
vals_type = '|'.join(sorted(set(vals)))
return ''.join((data_type, '[', keys_type, ', ', vals_type, ']'))