diff --git a/changelogs/fragments/84384-fix-undefined-key-host-group-vars.yml b/changelogs/fragments/84384-fix-undefined-key-host-group-vars.yml new file mode 100644 index 00000000000..00f7117d802 --- /dev/null +++ b/changelogs/fragments/84384-fix-undefined-key-host-group-vars.yml @@ -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) diff --git a/lib/ansible/plugins/vars/host_group_vars.py b/lib/ansible/plugins/vars/host_group_vars.py index cb5b4b0c2b1..7baa76bf28f 100644 --- a/lib/ansible/plugins/vars/host_group_vars.py +++ b/lib/ansible/plugins/vars/host_group_vars.py @@ -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)