From 607f3d83a0e246747ad9afd2e1a79859ff9c1aae Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 18 Oct 2022 10:26:29 +0200 Subject: [PATCH] virtualbox: Fix crash when handling deeply nested hostvars (#5348) (#5381) * virtualbox: Fix nested data parsing - Skip parsing values with keys that have both a value and nested data. - Skip parsing values that are nested more than two keys deep. * Update changelogs/fragments/5348-fix-vbox-deeply-nested-hostvars.yml Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein (cherry picked from commit b0bb994c3e965381a7ce1f3b2dae6280d7e8b741) Co-authored-by: Paul Sanchez <124954+basicdays@users.noreply.github.com> --- .../fragments/5348-fix-vbox-deeply-nested-hostvars.yml | 2 ++ plugins/inventory/virtualbox.py | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/5348-fix-vbox-deeply-nested-hostvars.yml diff --git a/changelogs/fragments/5348-fix-vbox-deeply-nested-hostvars.yml b/changelogs/fragments/5348-fix-vbox-deeply-nested-hostvars.yml new file mode 100644 index 0000000000..f8084d6345 --- /dev/null +++ b/changelogs/fragments/5348-fix-vbox-deeply-nested-hostvars.yml @@ -0,0 +1,2 @@ +bugfixes: + - virtualbox inventory plugin - skip parsing values with keys that have both a value and nested data. Skip parsing values that are nested more than two keys deep (https://github.com/ansible-collections/community.general/issues/5332, https://github.com/ansible-collections/community.general/pull/5348). diff --git a/plugins/inventory/virtualbox.py b/plugins/inventory/virtualbox.py index 829fc7b971..c926d8b449 100644 --- a/plugins/inventory/virtualbox.py +++ b/plugins/inventory/virtualbox.py @@ -186,10 +186,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): else: # found vars, accumulate in hostvars for clean inventory set pref_k = 'vbox_' + k.strip().replace(' ', '_') - if k.startswith(' '): - if prevkey not in hostvars[current_host]: + leading_spaces = len(k) - len(k.lstrip(' ')) + if 0 < leading_spaces <= 2: + if prevkey not in hostvars[current_host] or not isinstance(hostvars[current_host][prevkey], dict): hostvars[current_host][prevkey] = {} hostvars[current_host][prevkey][pref_k] = v + elif leading_spaces > 2: + continue else: if v != '': hostvars[current_host][pref_k] = v