2012-10-25 13:10:33 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.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/>.
|
|
|
|
|
2013-04-10 20:17:24 +00:00
|
|
|
from ansible.utils import safe_eval
|
2013-04-10 22:42:54 +00:00
|
|
|
import ansible.utils.template as template
|
2013-04-10 20:17:24 +00:00
|
|
|
|
2013-02-17 21:07:47 +00:00
|
|
|
def flatten(terms):
|
|
|
|
ret = []
|
|
|
|
for term in terms:
|
|
|
|
if isinstance(term, list):
|
|
|
|
ret.extend(term)
|
|
|
|
else:
|
|
|
|
ret.append(term)
|
|
|
|
return ret
|
|
|
|
|
2012-10-25 13:10:33 +00:00
|
|
|
class LookupModule(object):
|
|
|
|
|
2013-04-10 22:42:54 +00:00
|
|
|
def __init__(self, basedir=None, **kwargs):
|
|
|
|
self.basedir = basedir
|
2012-10-25 13:10:33 +00:00
|
|
|
|
2013-04-10 22:42:54 +00:00
|
|
|
def run(self, terms, inject=None, **kwargs):
|
2012-12-05 11:26:23 +00:00
|
|
|
if isinstance(terms, basestring):
|
2013-04-10 22:42:54 +00:00
|
|
|
# somewhat 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)
|
2013-04-10 19:22:08 +00:00
|
|
|
if '{' or '[' in terms:
|
2013-04-10 22:42:54 +00:00
|
|
|
# Jinja2 already evaluated a variable to a list.
|
2013-04-10 19:22:08 +00:00
|
|
|
# Jinja2-ified list needs to be converted back to a real type
|
|
|
|
# TODO: something a bit less heavy than eval
|
2013-04-10 20:17:24 +00:00
|
|
|
terms = safe_eval(terms)
|
2012-12-05 11:26:23 +00:00
|
|
|
terms = [ terms ]
|
2013-02-17 21:07:47 +00:00
|
|
|
return flatten(terms)
|
2013-04-10 19:22:08 +00:00
|
|
|
|
|
|
|
|