Merge pull request #1593 from dhozac/lookup_generic

Add support for generic $XXX() to call $LOOKUP()
This commit is contained in:
Michael DeHaan 2012-11-12 05:07:52 -08:00
commit 3e855a5fcd

View file

@ -141,8 +141,8 @@ def varReplace(raw, vars, depth=0, expand_lists=False):
return ''.join(done) return ''.join(done)
_FILEPIPECRE = re.compile(r"\$(?P<special>FILE|PIPE|LOOKUP)\(([^\)]+)\)") _FILEPIPECRE = re.compile(r"\$(?P<special>[A-Z]+)\(([^\)]*)\)")
def _varReplaceFilesAndPipes(basedir, raw, vars): def _varReplaceLookups(basedir, raw, vars):
from ansible import utils from ansible import utils
done = [] # Completed chunks to return done = [] # Completed chunks to return
@ -152,7 +152,7 @@ def _varReplaceFilesAndPipes(basedir, raw, vars):
done.append(raw) done.append(raw)
break break
# Determine replacement value (if unknown variable then preserve # Determine replacement value (if unknown lookup plugin then preserve
# original) # original)
replacement = m.group() replacement = m.group()
@ -165,10 +165,16 @@ def _varReplaceFilesAndPipes(basedir, raw, vars):
elif m.group(1) == "LOOKUP": elif m.group(1) == "LOOKUP":
module_name, args = m.group(2).split(",", 1) module_name, args = m.group(2).split(",", 1)
args = args.strip() args = args.strip()
else:
module_name = m.group(1).lower()
args = m.group(2)
instance = utils.plugins.lookup_loader.get(module_name, basedir=basedir) instance = utils.plugins.lookup_loader.get(module_name, basedir=basedir)
replacement = instance.run(args, inject=vars) if instance is not None:
if not isinstance(replacement, basestring): replacement = instance.run(args, inject=vars)
replacement = ",".join(replacement) if not isinstance(replacement, basestring):
replacement = ",".join(replacement)
else:
replacement = m.group(0)
start, end = m.span() start, end = m.span()
done.append(raw[:start]) # Keep stuff leading up to token done.append(raw[:start]) # Keep stuff leading up to token
@ -209,7 +215,7 @@ def template(basedir, text, vars, expand_lists=False):
except UnicodeEncodeError: except UnicodeEncodeError:
pass # already unicode pass # already unicode
text = varReplace(unicode(text), vars, expand_lists=expand_lists) text = varReplace(unicode(text), vars, expand_lists=expand_lists)
text = _varReplaceFilesAndPipes(basedir, text, vars) text = _varReplaceLookups(basedir, text, vars)
return text return text
def template_from_file(basedir, path, vars): def template_from_file(basedir, path, vars):