YAML representer for VarsWithSources (#68525)

This commit is contained in:
Nasser Alansari 2021-06-22 18:33:18 +04:00 committed by GitHub
parent 0f7f4e7b61
commit 6dbfd73174
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Add yaml representer for VarsWithSources (https://github.com/ansible/ansible/pull/68525).

View file

@ -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,

View file

@ -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!")