From 7b1e87b62d58e00fdf3739bd279ea09f4eada988 Mon Sep 17 00:00:00 2001 From: Ninety Thirty Date: Thu, 20 Jun 2013 22:03:17 -0600 Subject: [PATCH] Added flexible filename handling for main files tasks, handlers, and vars main files can now be any of main, main.yml, or main.yaml --- lib/ansible/playbook/play.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index db42387bb82..ba849eb640b 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -170,9 +170,12 @@ class Play(object): path = path2 elif not os.path.isdir(path): raise errors.AnsibleError("cannot find role in %s" % (path)) - task = utils.path_dwim(self.basedir, os.path.join(path, 'tasks', 'main.yml')) - handler = utils.path_dwim(self.basedir, os.path.join(path, 'handlers', 'main.yml')) - vars_file = utils.path_dwim(self.basedir, os.path.join(path, 'vars', 'main.yml')) + task_basepath = utils.path_dwim(self.basedir, os.path.join(path, 'tasks')) + handler_basepath = utils.path_dwim(self.basedir, os.path.join(path, 'handlers')) + 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')) 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)) @@ -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): ''' handle task and handler include statements '''