mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-29 03:30:22 -07:00
Fix YAML callback (#10212)
Some checks are pending
EOL CI / EOL Sanity (Ⓐ2.16) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.16+py2.7) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.16+py3.11) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.16+py3.6) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/3/) (push) Waiting to run
nox / Run extra sanity tests (push) Waiting to run
Some checks are pending
EOL CI / EOL Sanity (Ⓐ2.16) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.16+py2.7) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.16+py3.11) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.16+py3.6) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+alpine3+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+fedora38+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.16+opensuse15+py:azp/posix/3/) (push) Waiting to run
nox / Run extra sanity tests (push) Waiting to run
Fix YAML callback.
This commit is contained in:
parent
f7f2db365e
commit
996ffc8f8c
2 changed files with 114 additions and 36 deletions
2
changelogs/fragments/10212-yaml.yml
Normal file
2
changelogs/fragments/10212-yaml.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- "yaml callback plugin - adjust to latest changes in ansible-core devel (https://github.com/ansible-collections/community.general/pull/10212)."
|
|
@ -83,47 +83,123 @@ except: # noqa: E722, pylint: disable=bare-except
|
||||||
from ansible._internal._yaml import _dumper
|
from ansible._internal._yaml import _dumper
|
||||||
import typing as t
|
import typing as t
|
||||||
|
|
||||||
class MyDumper(_dumper._BaseDumper):
|
if hasattr(_dumper, "SafeRepresenter"):
|
||||||
|
# This was the current state until https://github.com/ansible/ansible/commit/1c06c46cc14324df35ac4f39a45fb3ccd602195d
|
||||||
|
class MyDumper(_dumper._BaseDumper):
|
||||||
|
# This code is mostly taken from ansible._internal._yaml._dumper
|
||||||
|
@classmethod
|
||||||
|
def _register_representers(cls) -> None:
|
||||||
|
cls.add_multi_representer(_dumper.AnsibleTaggedObject, cls.represent_ansible_tagged_object)
|
||||||
|
cls.add_multi_representer(_dumper.Tripwire, cls.represent_tripwire)
|
||||||
|
cls.add_multi_representer(_dumper.c.Mapping, _dumper.SafeRepresenter.represent_dict)
|
||||||
|
cls.add_multi_representer(_dumper.c.Sequence, _dumper.SafeRepresenter.represent_list)
|
||||||
|
|
||||||
|
def represent_ansible_tagged_object(self, data):
|
||||||
|
if ciphertext := _dumper.VaultHelper.get_ciphertext(data, with_tags=False):
|
||||||
|
return self.represent_scalar('!vault', ciphertext, style='|')
|
||||||
|
|
||||||
|
return self.represent_data(_dumper.AnsibleTagHelper.as_native_type(data)) # automatically decrypts encrypted strings
|
||||||
|
|
||||||
|
def represent_tripwire(self, data: _dumper.Tripwire) -> t.NoReturn:
|
||||||
|
data.trip()
|
||||||
|
|
||||||
|
# The following function is the same as in the try/except
|
||||||
|
def represent_scalar(self, tag, value, style=None):
|
||||||
|
"""Uses block style for multi-line strings"""
|
||||||
|
if style is None:
|
||||||
|
if should_use_block(value):
|
||||||
|
style = '|'
|
||||||
|
# we care more about readable than accuracy, so...
|
||||||
|
# ...no trailing space
|
||||||
|
value = value.rstrip()
|
||||||
|
# ...and non-printable characters
|
||||||
|
value = ''.join(x for x in value if x in string.printable or ord(x) >= 0xA0)
|
||||||
|
# ...tabs prevent blocks from expanding
|
||||||
|
value = value.expandtabs()
|
||||||
|
# ...and odd bits of whitespace
|
||||||
|
value = re.sub(r'[\x0b\x0c\r]', '', value)
|
||||||
|
# ...as does trailing space
|
||||||
|
value = re.sub(r' +\n', '\n', value)
|
||||||
|
else:
|
||||||
|
style = self.default_style
|
||||||
|
node = yaml.representer.ScalarNode(tag, value, style=style)
|
||||||
|
if self.alias_key is not None:
|
||||||
|
self.represented_objects[self.alias_key] = node
|
||||||
|
return node
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Adjusting to https://github.com/ansible/ansible/commit/1c06c46cc14324df35ac4f39a45fb3ccd602195d
|
||||||
|
# and https://github.com/ansible/ansible/commit/2b7204527b0906172e5ba719f1a0fb64070c7b5e
|
||||||
|
|
||||||
# This code is mostly taken from ansible._internal._yaml._dumper
|
# This code is mostly taken from ansible._internal._yaml._dumper
|
||||||
@classmethod
|
import collections.abc as c
|
||||||
def _register_representers(cls) -> None:
|
|
||||||
cls.add_multi_representer(_dumper.AnsibleTaggedObject, cls.represent_ansible_tagged_object)
|
|
||||||
cls.add_multi_representer(_dumper.Tripwire, cls.represent_tripwire)
|
|
||||||
cls.add_multi_representer(_dumper.c.Mapping, _dumper.SafeRepresenter.represent_dict)
|
|
||||||
cls.add_multi_representer(_dumper.c.Sequence, _dumper.SafeRepresenter.represent_list)
|
|
||||||
|
|
||||||
def represent_ansible_tagged_object(self, data):
|
from yaml.nodes import ScalarNode, Node
|
||||||
if ciphertext := _dumper.VaultHelper.get_ciphertext(data, with_tags=False):
|
|
||||||
return self.represent_scalar('!vault', ciphertext, style='|')
|
|
||||||
|
|
||||||
return self.represent_data(_dumper.AnsibleTagHelper.as_native_type(data)) # automatically decrypts encrypted strings
|
from ansible._internal._templating import _jinja_common
|
||||||
|
from ansible.module_utils import _internal
|
||||||
|
from ansible.module_utils._internal._datatag import AnsibleTaggedObject, Tripwire, AnsibleTagHelper
|
||||||
|
from ansible.parsing.vault import VaultHelper
|
||||||
|
|
||||||
def represent_tripwire(self, data: _dumper.Tripwire) -> t.NoReturn:
|
class MyDumper(_dumper._BaseDumper):
|
||||||
data.trip()
|
@classmethod
|
||||||
|
def _register_representers(cls) -> None:
|
||||||
|
cls.add_multi_representer(AnsibleTaggedObject, cls.represent_ansible_tagged_object)
|
||||||
|
cls.add_multi_representer(Tripwire, cls.represent_tripwire)
|
||||||
|
cls.add_multi_representer(c.Mapping, cls.represent_dict)
|
||||||
|
cls.add_multi_representer(c.Collection, cls.represent_list)
|
||||||
|
cls.add_multi_representer(_jinja_common.VaultExceptionMarker, cls.represent_vault_exception_marker)
|
||||||
|
|
||||||
# The following function is the same as in the try/except
|
def get_node_from_ciphertext(self, data: object) -> ScalarNode | None:
|
||||||
def represent_scalar(self, tag, value, style=None):
|
if ciphertext := VaultHelper.get_ciphertext(data, with_tags=False):
|
||||||
"""Uses block style for multi-line strings"""
|
return self.represent_scalar('!vault', ciphertext, style='|')
|
||||||
if style is None:
|
|
||||||
if should_use_block(value):
|
return None
|
||||||
style = '|'
|
|
||||||
# we care more about readable than accuracy, so...
|
def represent_vault_exception_marker(self, data: _jinja_common.VaultExceptionMarker) -> ScalarNode:
|
||||||
# ...no trailing space
|
if node := self.get_node_from_ciphertext(data):
|
||||||
value = value.rstrip()
|
return node
|
||||||
# ...and non-printable characters
|
|
||||||
value = ''.join(x for x in value if x in string.printable or ord(x) >= 0xA0)
|
data.trip()
|
||||||
# ...tabs prevent blocks from expanding
|
|
||||||
value = value.expandtabs()
|
def represent_ansible_tagged_object(self, data: AnsibleTaggedObject) -> Node:
|
||||||
# ...and odd bits of whitespace
|
if _internal.is_intermediate_mapping(data):
|
||||||
value = re.sub(r'[\x0b\x0c\r]', '', value)
|
return self.represent_dict(data)
|
||||||
# ...as does trailing space
|
|
||||||
value = re.sub(r' +\n', '\n', value)
|
if _internal.is_intermediate_iterable(data):
|
||||||
else:
|
return self.represent_list(data)
|
||||||
style = self.default_style
|
|
||||||
node = yaml.representer.ScalarNode(tag, value, style=style)
|
if node := self.get_node_from_ciphertext(data):
|
||||||
if self.alias_key is not None:
|
return node
|
||||||
self.represented_objects[self.alias_key] = node
|
|
||||||
return node
|
return self.represent_data(AnsibleTagHelper.as_native_type(data)) # automatically decrypts encrypted strings
|
||||||
|
|
||||||
|
def represent_tripwire(self, data: Tripwire) -> t.NoReturn:
|
||||||
|
data.trip()
|
||||||
|
|
||||||
|
# The following function is the same as in the try/except
|
||||||
|
def represent_scalar(self, tag, value, style=None):
|
||||||
|
"""Uses block style for multi-line strings"""
|
||||||
|
if style is None:
|
||||||
|
if should_use_block(value):
|
||||||
|
style = '|'
|
||||||
|
# we care more about readable than accuracy, so...
|
||||||
|
# ...no trailing space
|
||||||
|
value = value.rstrip()
|
||||||
|
# ...and non-printable characters
|
||||||
|
value = ''.join(x for x in value if x in string.printable or ord(x) >= 0xA0)
|
||||||
|
# ...tabs prevent blocks from expanding
|
||||||
|
value = value.expandtabs()
|
||||||
|
# ...and odd bits of whitespace
|
||||||
|
value = re.sub(r'[\x0b\x0c\r]', '', value)
|
||||||
|
# ...as does trailing space
|
||||||
|
value = re.sub(r' +\n', '\n', value)
|
||||||
|
else:
|
||||||
|
style = self.default_style
|
||||||
|
node = yaml.representer.ScalarNode(tag, value, style=style)
|
||||||
|
if self.alias_key is not None:
|
||||||
|
self.represented_objects[self.alias_key] = node
|
||||||
|
return node
|
||||||
|
|
||||||
|
|
||||||
class CallbackModule(Default):
|
class CallbackModule(Default):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue