Commit 86d47bce by Michael DeHaan

Make more lookup plugins tolerant of new variable system, with a little better…

Make more lookup plugins tolerant of new variable system, with a little better 'do what I mean' logic to resolving
what happens if you get a string back as a template result.
parent c0f8af52
...@@ -40,9 +40,13 @@ class LookupModule(object): ...@@ -40,9 +40,13 @@ class LookupModule(object):
if HAVE_DNS == False: if HAVE_DNS == False:
raise errors.AnsibleError("Can't LOOKUP(dnstxt): module dns.resolver is not installed") raise errors.AnsibleError("Can't LOOKUP(dnstxt): module dns.resolver is not installed")
def run(self, terms, **kwargs): def run(self, terms, inject=None, **kwargs):
if isinstance(terms, basestring):
terms = [ terms ] terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
ret = [] ret = []
for term in terms: for term in terms:
domain = term.split()[0] domain = term.split()[0]
......
...@@ -23,9 +23,10 @@ class LookupModule(object): ...@@ -23,9 +23,10 @@ class LookupModule(object):
def __init__(self, basedir=None, **kwargs): def __init__(self, basedir=None, **kwargs):
self.basedir = basedir self.basedir = basedir
def run(self, terms, **kwargs): def run(self, terms, inject=None, **kwargs):
if isinstance(terms, basestring):
terms = [ terms ] terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
ret = [] ret = []
for term in terms: for term in terms:
var = term.split()[0] var = term.split()[0]
......
...@@ -66,10 +66,6 @@ ...@@ -66,10 +66,6 @@
# this will include the tasks in the file generic where it is found first (staging or production) # this will include the tasks in the file generic where it is found first (staging or production)
from ansible import utils, errors from ansible import utils, errors
import os import os
...@@ -78,8 +74,12 @@ class LookupModule(object): ...@@ -78,8 +74,12 @@ class LookupModule(object):
def __init__(self, basedir=None, **kwargs): def __init__(self, basedir=None, **kwargs):
self.basedir = basedir self.basedir = basedir
def run(self, terms, **kwargs): def run(self, terms, inject=None, **kwargs):
terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
result = None result = None
for term in terms: for term in terms:
if isinstance(term, dict): if isinstance(term, dict):
files = term.get('files', []) files = term.get('files', [])
......
...@@ -23,9 +23,10 @@ class LookupModule(object): ...@@ -23,9 +23,10 @@ class LookupModule(object):
def __init__(self, basedir=None, **kwargs): def __init__(self, basedir=None, **kwargs):
self.basedir = basedir self.basedir = basedir
def run(self, terms, **kwargs): def run(self, terms, inject=None, **kwargs):
if isinstance(terms, basestring):
terms = [ terms ] terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
ret = [] ret = []
for term in terms: for term in terms:
p = subprocess.Popen(term, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) p = subprocess.Popen(term, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
......
...@@ -30,9 +30,10 @@ class LookupModule(object): ...@@ -30,9 +30,10 @@ class LookupModule(object):
def __init__(self, length=None, basedir=None, **kwargs): def __init__(self, length=None, basedir=None, **kwargs):
self.basedir = basedir self.basedir = basedir
def run(self, terms, **kwargs): def run(self, terms, inject=None, **kwargs):
if isinstance(terms, basestring):
terms = [ terms ] terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
ret = [] ret = []
for term in terms: for term in terms:
......
...@@ -23,9 +23,13 @@ class LookupModule(object): ...@@ -23,9 +23,13 @@ class LookupModule(object):
def __init__(self, basedir=None, **kwargs): def __init__(self, basedir=None, **kwargs):
self.basedir = basedir self.basedir = basedir
def run(self, terms, **kwargs): def run(self, terms, inject=None, **kwargs):
terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
if isinstance(terms, basestring): if isinstance(terms, basestring):
terms = [ terms ] terms = [ terms ]
ret = [] ret = []
for term in terms: for term in terms:
p = subprocess.Popen(term, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) p = subprocess.Popen(term, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import random import random
from ansible import utils
# useful for introducing chaos ... or just somewhat reasonably fair selection # useful for introducing chaos ... or just somewhat reasonably fair selection
# amongst available mirrors # amongst available mirrors
...@@ -32,8 +33,9 @@ class LookupModule(object): ...@@ -32,8 +33,9 @@ class LookupModule(object):
def __init__(self, basedir=None, **kwargs): def __init__(self, basedir=None, **kwargs):
self.basedir = basedir self.basedir = basedir
def run(self, terms, **kwargs): def run(self, terms, inject=None, **kwargs):
if isinstance(terms, basestring):
terms = [ terms ] terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
return [ random.choice(terms) ] return [ random.choice(terms) ]
...@@ -39,9 +39,10 @@ class LookupModule(object): ...@@ -39,9 +39,10 @@ class LookupModule(object):
if HAVE_REDIS == False: if HAVE_REDIS == False:
raise errors.AnsibleError("Can't LOOKUP(redis_kv): module redis is not installed") raise errors.AnsibleError("Can't LOOKUP(redis_kv): module redis is not installed")
def run(self, terms, **kwargs): def run(self, terms, inject=None, **kwargs):
if isinstance(terms, basestring):
terms = [ terms ] terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
ret = [] ret = []
for term in terms: for term in terms:
(url,key) = term.split(',') (url,key) = term.split(',')
......
...@@ -73,9 +73,9 @@ class LookupModule(object): ...@@ -73,9 +73,9 @@ class LookupModule(object):
calculating the number of entries in a sequence when a stride is specified. calculating the number of entries in a sequence when a stride is specified.
""" """
def __init__(self, **kwargs): def __init__(self, basedir, **kwargs):
"""absorb any keyword args""" """absorb any keyword args"""
pass self.basedir = basedir
def reset(self): def reset(self):
"""set sensible defaults""" """set sensible defaults"""
...@@ -170,11 +170,10 @@ class LookupModule(object): ...@@ -170,11 +170,10 @@ class LookupModule(object):
"problem formatting %r with %r" % self.format "problem formatting %r with %r" % self.format
) )
def run(self, terms, **kwargs): def run(self, terms, inject=None, **kwargs):
results = [] results = []
if isinstance(terms, basestring): terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
terms = [terms]
for term in terms: for term in terms:
try: try:
......
...@@ -23,8 +23,9 @@ class LookupModule(object): ...@@ -23,8 +23,9 @@ class LookupModule(object):
self.basedir = basedir self.basedir = basedir
def run(self, terms, inject=None, **kwargs): def run(self, terms, inject=None, **kwargs):
if isinstance(terms, basestring):
terms = [ terms ] terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
ret = [] ret = []
for term in terms: for term in terms:
ret.append(template.template_from_file(self.basedir, term, inject)) ret.append(template.template_from_file(self.basedir, term, inject))
......
...@@ -725,7 +725,11 @@ def listify_lookup_plugin_terms(terms, basedir, inject): ...@@ -725,7 +725,11 @@ def listify_lookup_plugin_terms(terms, basedir, inject):
if not '{' in terms and not '[' in terms and not terms.strip().startswith("/"): if not '{' in terms and not '[' in terms and not terms.strip().startswith("/"):
try: try:
terms = template.template(basedir, "{{ %s }}" % terms, inject) new_terms = template.template(basedir, "{{ %s }}" % terms, inject)
if isinstance(new_terms, basestring) and new_terms.find("{{") != -1:
pass
else:
terms = new_terms
except: except:
pass pass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment