Commit c0f8af52 by Michael DeHaan

Make more lookup plugins happy with newstyle variables. Not quite done, but…

Make more lookup plugins happy with newstyle variables.  Not quite done, but this fixes up with_items/with_nested/file/fileglob.
parent f72649d0
...@@ -24,13 +24,21 @@ class LookupModule(object): ...@@ -24,13 +24,21 @@ 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 = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
terms = [ terms ]
ret = [] ret = []
# this can happen if the variable contains a string, strictly not desired for lookup
# plugins, but users may try it, so make it work.
if not isinstance(terms, list):
terms = [ terms ]
for term in terms: for term in terms:
path = utils.path_dwim(self.basedir, term) path = utils.path_dwim(self.basedir, term)
if not os.path.exists(path): if not os.path.exists(path):
raise errors.AnsibleError("%s does not exist" % path) raise errors.AnsibleError("%s does not exist" % path)
ret.append(codecs.open(path, encoding="utf8").read().rstrip()) ret.append(codecs.open(path, encoding="utf8").read().rstrip())
return ret return ret
...@@ -24,18 +24,16 @@ class LookupModule(object): ...@@ -24,18 +24,16 @@ 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 ] utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
ret = [] ret = []
for term in terms: for term in terms:
dwimterms = utils.path_dwim(self.basedir, term)
# This skips whatever prefix the dwim added, leaving just the filename for the item dwimmed = utils.path_dwim(self.basedir, term)
i = -1 globbed = glob.glob(dwimmed)
while dwimterms[i] == term[i] and -i < len(term) and -i < len(dwimterms): ret.extend(os.path.basename(g) for g in globbed if os.path.isfile(g))
i = i - 1
orig_prefix_len = i + 1
dwim_prefix_len = len(dwimterms) + i + 1
ret.extend([ term[:orig_prefix_len] + f[dwim_prefix_len:]
for f in glob.glob(dwimterms) if os.path.isfile(f) ])
return ret return ret
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from ansible.utils import safe_eval from ansible.utils import safe_eval
import ansible.utils.template as template import ansible.utils as utils
import ansible.errors as errors
def flatten(terms): def flatten(terms):
ret = [] ret = []
...@@ -33,20 +34,11 @@ class LookupModule(object): ...@@ -33,20 +34,11 @@ 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 = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
# somewhat did:
# with_items: alist if not isinstance(terms, list):
# OR raise errors.AnsibleError("with_items expects a list")
# with_items: {{ alist }}
if not '{' in terms and not '[' in terms:
terms = '{{ %s }}' % terms
terms = template.template(self.basedir, terms, inject)
if '{' or '[' in terms:
# Jinja2 already evaluated a variable to a list.
# Jinja2-ified list needs to be converted back to a real type
# TODO: something a bit less heavy than eval
terms = safe_eval(terms)
terms = [ terms ]
return flatten(terms) return flatten(terms)
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import ansible.utils.template as template import ansible.utils as utils
from ansible.utils import safe_eval from ansible.utils import safe_eval
import ansible.errors as errors import ansible.errors as errors
...@@ -46,28 +46,13 @@ class LookupModule(object): ...@@ -46,28 +46,13 @@ class LookupModule(object):
# this code is common with 'items.py' consider moving to utils if we need it again # this code is common with 'items.py' consider moving to utils if we need it again
if isinstance(terms, basestring): terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
# someone did:
# with_items: alist
# OR
# with_items: {{ alist }}
if not '{' in terms and not '[' in terms:
terms = '{{ %s }}' % terms
terms = template.template(self.basedir, terms, inject)
if '{' or '[' in terms:
# Jinja2 already evaluated a variable to a list.
# Jinja2-ified list needs to be converted back to a real type
# TODO: something a bit less heavy than eval
terms = safe_eval(terms)
if not isinstance(terms, list):
raise errors.AnsibleError("a list is required for with_nested")
my_list = terms[:] my_list = terms[:]
my_list.reverse() my_list.reverse()
result = [] result = []
if len(my_list) == 0: if len(my_list) == 0:
raise errors.AnsibleError("with_nested requires at least one list") raise errors.AnsibleError("with_nested requires at least one element in the nested list")
result = my_list.pop() result = my_list.pop()
while len(my_list) > 0: while len(my_list) > 0:
result2 = combine(result, my_list.pop()) result2 = combine(result, my_list.pop())
......
...@@ -26,6 +26,7 @@ import operator ...@@ -26,6 +26,7 @@ import operator
from ansible import errors from ansible import errors
from ansible import __version__ from ansible import __version__
from ansible.utils.plugins import * from ansible.utils.plugins import *
from ansible.utils import template
import ansible.constants as C import ansible.constants as C
import time import time
import StringIO import StringIO
...@@ -713,6 +714,31 @@ def safe_eval(str): ...@@ -713,6 +714,31 @@ def safe_eval(str):
return str return str
def listify_lookup_plugin_terms(terms, basedir, inject):
if isinstance(terms, basestring):
print "A0"
# somewhat did:
# with_items: alist
# OR
# with_items: {{ alist }}
if not '{' in terms and not '[' in terms and not terms.strip().startswith("/"):
try:
terms = template.template(basedir, "{{ %s }}" % terms, inject)
except:
pass
if '{' or '[' in terms:
# Jinja2 already evaluated a variable to a list.
# Jinja2-ified list needs to be converted back to a real type
# TODO: something a bit less heavy than eval
return safe_eval(terms)
if isinstance(terms, basestring):
terms = [ terms ]
return terms
......
...@@ -23,7 +23,6 @@ from jinja2.runtime import StrictUndefined ...@@ -23,7 +23,6 @@ from jinja2.runtime import StrictUndefined
import yaml import yaml
import json import json
from ansible import errors from ansible import errors
import ansible.utils as utils
import ansible.constants as C import ansible.constants as C
import time import time
import subprocess import subprocess
...@@ -41,6 +40,7 @@ _LISTRE = re.compile(r"(\w+)\[(\d+)\]") ...@@ -41,6 +40,7 @@ _LISTRE = re.compile(r"(\w+)\[(\d+)\]")
JINJA2_OVERRIDE='#jinja2:' JINJA2_OVERRIDE='#jinja2:'
def lookup(name, *args, **kwargs): def lookup(name, *args, **kwargs):
from ansible import utils
instance = utils.plugins.lookup_loader.get(name.lower(), basedir=kwargs.get('basedir',None)) instance = utils.plugins.lookup_loader.get(name.lower(), basedir=kwargs.get('basedir',None))
if instance is not None: if instance is not None:
return ",".join(instance.run(*args, inject=vars, **kwargs)) return ",".join(instance.run(*args, inject=vars, **kwargs))
......
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