From 37d9acc0978a573c97996d514ab2a88c057e0e9f Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Wed, 31 Oct 2012 16:52:31 +0100 Subject: [PATCH] Make lookup API extensible and allow basedir to be passed This allows using with_fileglob in play context, e.g. - include: $item with_fileglob: tasks/*.yml as well as prepares for other data to be passed. --- lib/ansible/playbook/play.py | 2 +- lib/ansible/runner/__init__.py | 4 ++-- lib/ansible/runner/lookup_plugins/fileglob.py | 8 ++++---- lib/ansible/runner/lookup_plugins/items.py | 8 +++----- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index c12a3077c3..023c820d60 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -112,7 +112,7 @@ class Play(object): if plugin_name not in self.playbook.lookup_plugins_list: raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) terms = utils.varReplaceWithItems(self.basedir, x[k], task_vars) - items = self.playbook.lookup_plugins_list[plugin_name].LookupModule(None).run(terms) + items = self.playbook.lookup_plugins_list[plugin_name].LookupModule(basedir=self.basedir, runner=None).run(terms) for item in items: mv = task_vars.copy() diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 60f87d9ab9..0aba777c75 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -170,12 +170,12 @@ class Runner(object): for (k,v) in action_plugin_list.iteritems(): self.action_plugins[k] = v.ActionModule(self) for (k,v) in lookup_plugin_list.iteritems(): - self.lookup_plugins[k] = v.LookupModule(self) + self.lookup_plugins[k] = v.LookupModule(runner=self, basedir=self.basedir) for (k,v) in utils.import_plugins(os.path.join(self.basedir, 'action_plugins')).iteritems(): self.action_plugins[k] = v.ActionModule(self) for (k,v) in utils.import_plugins(os.path.join(self.basedir, 'lookup_plugins')).iteritems(): - self.lookup_plugins[k] = v.LookupModule(self) + self.lookup_plugins[k] = v.LookupModule(runner=self, basedir=self.basedir) # ***************************************************** diff --git a/lib/ansible/runner/lookup_plugins/fileglob.py b/lib/ansible/runner/lookup_plugins/fileglob.py index fe58173555..456ca4ed1f 100644 --- a/lib/ansible/runner/lookup_plugins/fileglob.py +++ b/lib/ansible/runner/lookup_plugins/fileglob.py @@ -21,11 +21,11 @@ from ansible import utils class LookupModule(object): - def __init__(self, runner): - self.runner = runner + def __init__(self, basedir=None, **kwargs): + self.basedir = basedir - def run(self, terms): - return [ f for f in glob.glob(utils.path_dwim(self.runner.basedir, terms)) if os.path.isfile(f) ] + def run(self, terms, **kwargs): + return [ f for f in glob.glob(utils.path_dwim(self.basedir, terms)) if os.path.isfile(f) ] diff --git a/lib/ansible/runner/lookup_plugins/items.py b/lib/ansible/runner/lookup_plugins/items.py index 2a4757db0e..efc4169602 100644 --- a/lib/ansible/runner/lookup_plugins/items.py +++ b/lib/ansible/runner/lookup_plugins/items.py @@ -15,14 +15,12 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -import os - class LookupModule(object): - def __init__(self, runner): - self.runner = runner + def __init__(self, **kwargs): + pass - def run(self, terms): + def run(self, terms, **kwargs): return terms