From ff8f751a1b407655e7e7dcdc65ec30f951d7201b Mon Sep 17 00:00:00 2001 From: Dong Zhang Date: Wed, 28 Jul 2021 11:27:28 +0200 Subject: [PATCH] Disable aliases in inventory.yaml for better readibility User complained about the anchors and aliases in inventory.yaml, which is intended for huram to read. With anchors and aliases reduced the readbility. This change is intended to fix this. Change-Id: I99333f85d7fdda213ddf2c14e5db89825b093b56 --- tests/unit/test_yamlutil.py | 26 ++++++++++++++++++++++++++ zuul/executor/server.py | 5 ++++- zuul/lib/yamlutil.py | 12 +++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_yamlutil.py b/tests/unit/test_yamlutil.py index 9c3fb3f66a..c1292fda93 100644 --- a/tests/unit/test_yamlutil.py +++ b/tests/unit/test_yamlutil.py @@ -82,3 +82,29 @@ list: """ yaml_out = yamlutil.ansible_unsafe_dump(data, default_flow_style=False) self.assertEqual(yaml_out, expected) + + def test_ansible_dumper_with_aliases(self): + foo = {'bar': 'baz'} + data = {'foo1': foo, 'foo2': foo} + expected = """\ +foo1: &id001 + bar: baz +foo2: *id001 +""" + yaml_out = yamlutil.ansible_unsafe_dump(data, default_flow_style=False) + self.assertEqual(yaml_out, expected) + + def test_ansible_dumper_ignore_aliases(self): + foo = {'bar': 'baz'} + data = {'foo1': foo, 'foo2': foo} + expected = """\ +foo1: + bar: baz +foo2: + bar: baz +""" + yaml_out = yamlutil.ansible_unsafe_dump( + data, + ignore_aliases=True, + default_flow_style=False) + self.assertEqual(yaml_out, expected) diff --git a/zuul/executor/server.py b/zuul/executor/server.py index 8293f52068..da96bb6df9 100644 --- a/zuul/executor/server.py +++ b/zuul/executor/server.py @@ -2430,7 +2430,10 @@ class AnsibleJob(object): inventory['all']['vars']['zuul'] = self.zuul_vars with open(self.jobdir.inventory, 'w') as inventory_yaml: inventory_yaml.write( - yaml.ansible_unsafe_dump(inventory, default_flow_style=False)) + yaml.ansible_unsafe_dump( + inventory, + ignore_aliases=True, + default_flow_style=False)) def writeSetupInventory(self): jobdir_playbook = self.jobdir.setup_playbook diff --git a/zuul/lib/yamlutil.py b/zuul/lib/yamlutil.py index a040c426c5..2d46750d5f 100644 --- a/zuul/lib/yamlutil.py +++ b/zuul/lib/yamlutil.py @@ -142,6 +142,11 @@ class AnsibleUnsafeDumper(yaml.SafeDumper): pass +class AnsibleUnsafeDumperWithoutAliases(yaml.SafeDumper): + def ignore_aliases(self, data): + return True + + class AnsibleUnsafeLoader(yaml.SafeLoader): pass @@ -153,7 +158,12 @@ AnsibleUnsafeLoader.add_constructor(AnsibleUnsafeStr.yaml_tag, def ansible_unsafe_dump(data, *args, **kwargs): - return yaml.dump(data, *args, Dumper=AnsibleUnsafeDumper, **kwargs) + ignore_aliases = kwargs.pop('ignore_aliases', False) + if ignore_aliases: + return yaml.dump(data, *args, Dumper=AnsibleUnsafeDumperWithoutAliases, + **kwargs) + else: + return yaml.dump(data, *args, Dumper=AnsibleUnsafeDumper, **kwargs) def ansible_unsafe_load(stream, *args, **kwargs):