Skip to content

Commit

Permalink
virtualbox: Fix crash when handling deeply nested hostvars (ansible-c…
Browse files Browse the repository at this point in the history
…ollections#5348)

* 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 <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
2 people authored and russoz committed Nov 5, 2022
1 parent 6b65aeb commit bf7a346
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions 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).
7 changes: 5 additions & 2 deletions plugins/inventory/virtualbox.py
Expand Up @@ -186,10 +186,13 @@ def _populate_from_source(self, source_data, using_current_cache=False):
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
Expand Down

0 comments on commit bf7a346

Please sign in to comment.