Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML representer for VarsWithSources #68525

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,2 @@
bugfixes:
- Add yaml representer for VarsWithSources (https://github.com/ansible/ansible/pull/68525).
6 changes: 6 additions & 0 deletions lib/ansible/parsing/yaml/dumper.py
Expand Up @@ -26,6 +26,7 @@
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):
Expand Down Expand Up @@ -83,6 +84,11 @@ def represent_binary(self, data):
represent_hostvars,
)

AnsibleDumper.add_representer(
VarsWithSources,
represent_hostvars,
)

AnsibleDumper.add_representer(
AnsibleSequence,
yaml.representer.SafeRepresenter.represent_list,
Expand Down
8 changes: 8 additions & 0 deletions test/units/parsing/yaml/test_dumper.py
Expand Up @@ -19,6 +19,7 @@
__metaclass__ = type

import io
import yaml

from units.compat import unittest
from ansible.parsing import vault
Expand All @@ -29,6 +30,7 @@

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):
Expand Down Expand Up @@ -101,3 +103,9 @@ def test_unicode(self):
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!")