mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-03 12:44:22 -07:00
Allow organizing host/group vars in a directory
So instead of having: group_vars/production.yml A user could chose to reorganize to: group_vars/production/staff.yml group_vars/production/networks.yml group_vars/production/dns.yml (Backwards compatible.)
This commit is contained in:
parent
babde9a84c
commit
65e5331079
1 changed files with 28 additions and 8 deletions
|
@ -85,12 +85,8 @@ def _load_vars_from_path(path, results):
|
||||||
# directory
|
# directory
|
||||||
if stat.S_ISDIR(pathstat.st_mode):
|
if stat.S_ISDIR(pathstat.st_mode):
|
||||||
|
|
||||||
# we ignore directories. however in the future it may be useful to
|
# support organizing variables across multiple files in a directory
|
||||||
# allow variables to be contained in multiple files in the named
|
return True, _load_vars_from_folder(path, results)
|
||||||
# directory, so that users can better organize large variables
|
|
||||||
# across multiple files.
|
|
||||||
raise errors.AnsibleError("Expected a variable file at path %s "
|
|
||||||
"but found a directory instead." % (path, ))
|
|
||||||
|
|
||||||
# regular file
|
# regular file
|
||||||
elif stat.S_ISREG(pathstat.st_mode):
|
elif stat.S_ISREG(pathstat.st_mode):
|
||||||
|
@ -106,9 +102,33 @@ def _load_vars_from_path(path, results):
|
||||||
|
|
||||||
# something else? could be a fifo, socket, device, etc.
|
# something else? could be a fifo, socket, device, etc.
|
||||||
else:
|
else:
|
||||||
raise errors.AnsibleError("Expected a variable file but found a "
|
raise errors.AnsibleError("Expected a variable file or directory "
|
||||||
"non-file object at path %s" % (path, ))
|
"but found a non-file object at path %s" % (path, ))
|
||||||
|
|
||||||
|
def _load_vars_from_folder(folder_path, results):
|
||||||
|
"""
|
||||||
|
Load all variables within a folder recursively.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# this function and _load_vars_from_path are mutually recursive
|
||||||
|
|
||||||
|
try:
|
||||||
|
names = os.listdir(folder_path)
|
||||||
|
except os.error, err:
|
||||||
|
raise errors.AnsibleError(
|
||||||
|
"This folder cannot be listed: %s: %s."
|
||||||
|
% ( folder_path, err.strerror))
|
||||||
|
|
||||||
|
# evaluate files in a stable order rather than whatever order the
|
||||||
|
# filesystem lists them.
|
||||||
|
names.sort()
|
||||||
|
|
||||||
|
paths = [os.path.join(folder_path, name) for name in names]
|
||||||
|
for path in paths:
|
||||||
|
_found, results = _load_vars_from_path(path, results)
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
class VarsModule(object):
|
class VarsModule(object):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue