1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Georg Gadinger 2023-04-16 13:22:49 +02:00 committed by GitHub
parent 29a7d24d75
commit cb3ca05bd1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View file

@ -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).

View file

@ -45,6 +45,8 @@ def render(to_render):
"""Converts dictionary to OpenNebula template.""" """Converts dictionary to OpenNebula template."""
def recurse(to_render): def recurse(to_render):
for key, value in sorted(to_render.items()): for key, value in sorted(to_render.items()):
if value is None:
continue
if isinstance(value, dict): if isinstance(value, dict):
yield '{0:}=[{1:}]'.format(key, ','.join(recurse(value))) yield '{0:}=[{1:}]'.format(key, ','.join(recurse(value)))
continue continue
@ -52,6 +54,9 @@ def render(to_render):
for item in value: for item in value:
yield '{0:}=[{1:}]'.format(key, ','.join(recurse(item))) yield '{0:}=[{1:}]'.format(key, ','.join(recurse(item)))
continue continue
if isinstance(value, str):
yield '{0:}="{1:}"'.format(key, value.replace('\\', '\\\\').replace('"', '\\"'))
continue
yield '{0:}="{1:}"'.format(key, value) yield '{0:}="{1:}"'.format(key, value)
return '\n'.join(recurse(to_render)) return '\n'.join(recurse(to_render))

View file

@ -75,6 +75,18 @@ RENDER_VALID = [
NIC=[NAME="NIC1",NETWORK_ID="1"] NIC=[NAME="NIC1",NETWORK_ID="1"]
''').strip() ''').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()
),
] ]