Add $LOOKUP(<lookup plugin>,<data>) as a templating option

Also moves file and pipe to lookup_plugins.
pull/1648/head
Daniel Hokka Zakrisson 12 years ago
parent 176cc8380b
commit 47082a9171

@ -149,9 +149,6 @@ class Task(object):
# allow the user to list comma delimited tags
import_tags = import_tags.split(",")
self.name = utils.template(None, self.name, self.module_vars)
self.action = utils.template(None, self.action, self.module_vars)
# handle mutually incompatible options
incompatibles = [ x for x in [ self.first_available_file, self.items_lookup_plugin ] if x is not None ]
if len(incompatibles) > 1:

@ -0,0 +1,30 @@
# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from ansible import utils, errors
import os
class LookupModule(object):
def __init__(self, basedir=None, **kwargs):
self.basedir = basedir
def run(self, terms, **kwargs):
path = utils.path_dwim(self.basedir, terms)
if not os.path.exists(path):
raise errors.AnsibleError("%s does not exist" % path)
return [open(path).read().rstrip()]

@ -0,0 +1,32 @@
# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import subprocess
from ansible import utils, errors
class LookupModule(object):
def __init__(self, basedir=None, **kwargs):
self.basedir = basedir
def run(self, terms, **kwargs):
p = subprocess.Popen(terms, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate()
if p.returncode == 0:
return stdout.splitlines()
else:
raise errors.AnsibleError("lookup_plugin.lines(%s) returned %d" % (terms, p.returncode))

@ -0,0 +1,32 @@
# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import subprocess
from ansible import utils, errors
class LookupModule(object):
def __init__(self, basedir=None, **kwargs):
self.basedir = basedir
def run(self, terms, **kwargs):
p = subprocess.Popen(terms, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate()
if p.returncode == 0:
return [stdout.rstrip()]
else:
raise errors.AnsibleError("lookup_plugin.pipe(%s) returned %d" % (terms, p.returncode))

@ -141,8 +141,9 @@ def varReplace(raw, vars, depth=0, expand_lists=False):
return ''.join(done)
_FILEPIPECRE = re.compile(r"\$(?P<special>FILE|PIPE)\(([^\)]+)\)")
def _varReplaceFilesAndPipes(basedir, raw):
_FILEPIPECRE = re.compile(r"\$(?P<special>FILE|PIPE|LOOKUP)\(([^\)]+)\)")
def _varReplaceFilesAndPipes(basedir, raw, vars):
from ansible import utils
done = [] # Completed chunks to return
while raw:
@ -156,21 +157,18 @@ def _varReplaceFilesAndPipes(basedir, raw):
replacement = m.group()
if m.group(1) == "FILE":
from ansible import utils
path = utils.path_dwim(basedir, m.group(2))
try:
f = open(path, "r")
replacement = f.read()
f.close()
except IOError:
raise errors.AnsibleError("$FILE(%s) failed" % path)
module_name = "file"
args = m.group(2)
elif m.group(1) == "PIPE":
p = subprocess.Popen(m.group(2), shell=True, stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate()
if p.returncode == 0:
replacement = stdout
else:
raise errors.AnsibleError("$PIPE(%s) returned %d" % (m.group(2), p.returncode))
module_name = "pipe"
args = m.group(2)
elif m.group(1) == "LOOKUP":
module_name, args = m.group(2).split(",", 1)
args = args.strip()
instance = utils.plugins.lookup_loader.get(module_name, basedir=basedir)
replacement = instance.run(args, inject=vars)
if not isinstance(replacement, basestring):
replacement = ",".join(replacement)
start, end = m.span()
done.append(raw[:start]) # Keep stuff leading up to token
@ -211,7 +209,7 @@ def template(basedir, text, vars, expand_lists=False):
except UnicodeEncodeError:
pass # already unicode
text = varReplace(unicode(text), vars, expand_lists=expand_lists)
text = _varReplaceFilesAndPipes(basedir, text)
text = _varReplaceFilesAndPipes(basedir, text, vars)
return text
def template_from_file(basedir, path, vars):

@ -272,18 +272,18 @@ class TestUtils(unittest.TestCase):
assert res == u'hello oh great one'
def test_varReplace_include(self):
template = 'hello $FILE(world)'
template = 'hello $FILE(world) $LOOKUP(file, world)'
res = ansible.utils.template("test", template, {})
assert res == u'hello world'
assert res == u'hello world world'
def test_varReplace_include_script(self):
template = 'hello $PIPE(echo world)'
template = 'hello $PIPE(echo world) $LOOKUP(pipe, echo world)'
res = ansible.utils.template("test", template, {})
assert res == u'hello world'
assert res == u'hello world world'
#####################################
### varReplaceWithItems function tests

Loading…
Cancel
Save