1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Added flexible filename handling for main files

tasks, handlers, and vars main files can now be any of main, main.yml, or main.yaml
This commit is contained in:
Ninety Thirty 2013-06-20 22:03:17 -06:00
parent c7c5ac1ab6
commit 7b1e87b62d

View file

@ -170,9 +170,12 @@ class Play(object):
path = path2 path = path2
elif not os.path.isdir(path): elif not os.path.isdir(path):
raise errors.AnsibleError("cannot find role in %s" % (path)) raise errors.AnsibleError("cannot find role in %s" % (path))
task = utils.path_dwim(self.basedir, os.path.join(path, 'tasks', 'main.yml')) task_basepath = utils.path_dwim(self.basedir, os.path.join(path, 'tasks'))
handler = utils.path_dwim(self.basedir, os.path.join(path, 'handlers', 'main.yml')) handler_basepath = utils.path_dwim(self.basedir, os.path.join(path, 'handlers'))
vars_file = utils.path_dwim(self.basedir, os.path.join(path, 'vars', 'main.yml')) vars_basepath = utils.path_dwim(self.basedir, os.path.join(path, 'vars'))
task = self._resolve_main(task_basepath)
handler = self._resolve_main(handler_basepath)
vars_file = self._resolve_main(vars_basepath)
library = utils.path_dwim(self.basedir, os.path.join(path, 'library')) library = utils.path_dwim(self.basedir, os.path.join(path, 'library'))
if not os.path.isfile(task) and not os.path.isfile(handler) and not os.path.isfile(vars_file) and not os.path.isdir(library): if not os.path.isfile(task) and not os.path.isfile(handler) and not os.path.isfile(vars_file) and not os.path.isdir(library):
raise errors.AnsibleError("found role at %s, but cannot find %s or %s or %s or %s" % (path, task, handler, vars_file, library)) raise errors.AnsibleError("found role at %s, but cannot find %s or %s or %s or %s" % (path, task, handler, vars_file, library))
@ -226,6 +229,24 @@ class Play(object):
# ************************************************* # *************************************************
def _resolve_main(self, basepath):
''' flexibly handle variations in main filenames '''
# these filenames are acceptable:
mains = (
os.path.join(basepath, 'main'),
os.path.join(basepath, 'main.yml'),
os.path.join(basepath, 'main.yaml'),
)
if sum([os.path.isfile(x) for x in mains]) > 1:
raise errors.AnsibleError("found multiple main files at %s, only one allowed" % (basepath))
else:
for m in mains:
if os.path.isfile(m):
return m # exactly one main file
return mains[0] # zero mains (we still need to return something)
# *************************************************
def _load_tasks(self, tasks, vars={}, additional_conditions=[], original_file=None): def _load_tasks(self, tasks, vars={}, additional_conditions=[], original_file=None):
''' handle task and handler include statements ''' ''' handle task and handler include statements '''