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