From 590ff351b4535f32cf63199bfc5a4bad68ca4acd Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 16 Apr 2023 13:40:09 +0200 Subject: [PATCH] [PR #6294/cb3ca05b backport][stable-6] one_vm: fix syntax error when creating VMs with a more complex template (#6342) one_vm: fix syntax error when creating VMs with a more complex template (#6294) * one_vm: fix syntax error when creating VMs with a more complex template with more complex templates that make use of quoted strings the new "render" method fails to produce a template that is accepted by OpenNebula. ==> escape double quotes in strings to make OpenNebula happy again. I also tested whether newlines need to be escaped, looks like they are fine as they are. Fixes #6225 * module_utils/opennebula: skip empty values in render (cherry picked from commit cb3ca05bd13aacc9329c634207cef3c929aa5c82) Co-authored-by: Georg Gadinger --- .../fragments/6294-fix-one_vm-instantiation.yml | 2 ++ plugins/module_utils/opennebula.py | 5 +++++ tests/unit/plugins/module_utils/test_opennebula.py | 12 ++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 changelogs/fragments/6294-fix-one_vm-instantiation.yml diff --git a/changelogs/fragments/6294-fix-one_vm-instantiation.yml b/changelogs/fragments/6294-fix-one_vm-instantiation.yml new file mode 100644 index 0000000000..493ef1c10f --- /dev/null +++ b/changelogs/fragments/6294-fix-one_vm-instantiation.yml @@ -0,0 +1,2 @@ +bugfixes: + - one_vm - fix syntax error when creating VMs with a more complex template (https://github.com/ansible-collections/community.general/issues/6225). diff --git a/plugins/module_utils/opennebula.py b/plugins/module_utils/opennebula.py index 0fe649ba5c..94732e4f7c 100644 --- a/plugins/module_utils/opennebula.py +++ b/plugins/module_utils/opennebula.py @@ -45,6 +45,8 @@ def render(to_render): """Converts dictionary to OpenNebula template.""" def recurse(to_render): for key, value in sorted(to_render.items()): + if value is None: + continue if isinstance(value, dict): yield '{0:}=[{1:}]'.format(key, ','.join(recurse(value))) continue @@ -52,6 +54,9 @@ def render(to_render): for item in value: yield '{0:}=[{1:}]'.format(key, ','.join(recurse(item))) continue + if isinstance(value, str): + yield '{0:}="{1:}"'.format(key, value.replace('\\', '\\\\').replace('"', '\\"')) + continue yield '{0:}="{1:}"'.format(key, value) return '\n'.join(recurse(to_render)) diff --git a/tests/unit/plugins/module_utils/test_opennebula.py b/tests/unit/plugins/module_utils/test_opennebula.py index 550d403711..dd6516984d 100644 --- a/tests/unit/plugins/module_utils/test_opennebula.py +++ b/tests/unit/plugins/module_utils/test_opennebula.py @@ -75,6 +75,18 @@ RENDER_VALID = [ NIC=[NAME="NIC1",NETWORK_ID="1"] ''').strip() ), + ( + { + 'EMPTY_VALUE': None, + 'SCHED_REQUIREMENTS': 'CLUSTER_ID="100"', + 'BACKSLASH_ESCAPED': "this is escaped: \\n; this isn't: \"\nend", + }, + textwrap.dedent(''' + BACKSLASH_ESCAPED="this is escaped: \\\\n; this isn't: \\" + end" + SCHED_REQUIREMENTS="CLUSTER_ID=\\"100\\"" + ''').strip() + ), ]