diff --git a/changelogs/fragments/68525-add-varswithsources-yaml-representer.yml b/changelogs/fragments/68525-add-varswithsources-yaml-representer.yml new file mode 100644 index 00000000000..4c348a92ebd --- /dev/null +++ b/changelogs/fragments/68525-add-varswithsources-yaml-representer.yml @@ -0,0 +1,2 @@ +bugfixes: +- Add yaml representer for VarsWithSources (https://github.com/ansible/ansible/pull/68525). diff --git a/lib/ansible/parsing/yaml/dumper.py b/lib/ansible/parsing/yaml/dumper.py index e0bf8c32c3f..a445f4bf73c 100644 --- a/lib/ansible/parsing/yaml/dumper.py +++ b/lib/ansible/parsing/yaml/dumper.py @@ -26,6 +26,7 @@ from ansible.module_utils.common.yaml import SafeDumper from ansible.parsing.yaml.objects import AnsibleUnicode, AnsibleSequence, AnsibleMapping, AnsibleVaultEncryptedUnicode from ansible.utils.unsafe_proxy import AnsibleUnsafeText, AnsibleUnsafeBytes from ansible.vars.hostvars import HostVars, HostVarsVars +from ansible.vars.manager import VarsWithSources class AnsibleDumper(SafeDumper): @@ -83,6 +84,11 @@ AnsibleDumper.add_representer( represent_hostvars, ) +AnsibleDumper.add_representer( + VarsWithSources, + represent_hostvars, +) + AnsibleDumper.add_representer( AnsibleSequence, yaml.representer.SafeRepresenter.represent_list, diff --git a/test/units/parsing/yaml/test_dumper.py b/test/units/parsing/yaml/test_dumper.py index 8129ca3ab2a..6578a6d1bfc 100644 --- a/test/units/parsing/yaml/test_dumper.py +++ b/test/units/parsing/yaml/test_dumper.py @@ -19,6 +19,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import io +import yaml from units.compat import unittest from ansible.parsing import vault @@ -29,6 +30,7 @@ from ansible.utils.unsafe_proxy import AnsibleUnsafeText, AnsibleUnsafeBytes from units.mock.yaml_helper import YamlTestUtils from units.mock.vault_helper import TextVaultSecret +from ansible.vars.manager import VarsWithSources class TestAnsibleDumper(unittest.TestCase, YamlTestUtils): @@ -101,3 +103,9 @@ class TestAnsibleDumper(unittest.TestCase, YamlTestUtils): data_from_yaml = loader.get_single_data() self.assertEqual(u_text, data_from_yaml) + + def test_vars_with_sources(self): + try: + self._dump_string(VarsWithSources(), dumper=self.dumper) + except yaml.representer.RepresenterError: + self.fail("Dump VarsWithSources raised RepresenterError unexpectedly!")