host_group_vars - fix Python API traceback caused by undefined 'key' variable (#84488)

Fixes:

Traceback (most recent call last):
  File "/home/shertel/ansible/lib/ansible/plugins/vars/host_group_vars.py", line 139, in get_vars
    FOUND[key] = found_files = loader.find_vars_files(opath, entity_name)
          ^^^
UnboundLocalError: cannot access local variable 'key' where it is not associated with a value

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/shertel/ansible/lib/ansible/plugins/vars/host_group_vars.py", line 151, in get_vars
    raise AnsibleParserError(to_native(e))
ansible.errors.AnsibleParserError: cannot access local variable 'key' where it is not associated with a value
pull/84549/head
xzeck 11 months ago committed by GitHub
parent eb475e23f7
commit 40d364985d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,4 @@
# Filename: changelogs/fragments/84384-fix-undefined-key-host-group-vars.yml
bugfixes:
- host_group_vars - fixed defining the 'key' variable if the get_vars method is called with cache=False (https://github.com/ansible/ansible/issues/84384)

@ -119,21 +119,19 @@ class VarsModule(BaseVarsPlugin):
else:
raise AnsibleParserError("Supplied entity must be Host or Group, got %s instead" % (type(entity)))
if cache:
try:
opath = PATH_CACHE[(realpath_basedir, subdir)]
except KeyError:
opath = PATH_CACHE[(realpath_basedir, subdir)] = os.path.join(realpath_basedir, subdir)
try:
opath = PATH_CACHE[(realpath_basedir, subdir)]
except KeyError:
opath = PATH_CACHE[(realpath_basedir, subdir)] = os.path.join(realpath_basedir, subdir)
key = '%s.%s' % (entity_name, opath)
if cache:
if opath in NAK:
continue
key = '%s.%s' % (entity_name, opath)
if key in FOUND:
data = self.load_found_files(loader, data, FOUND[key])
continue
else:
opath = PATH_CACHE[(realpath_basedir, subdir)] = os.path.join(realpath_basedir, subdir)
if os.path.isdir(opath):
self._display.debug("\tprocessing dir %s" % opath)
FOUND[key] = found_files = loader.find_vars_files(opath, entity_name)

Loading…
Cancel
Save