Commit 803fb397 by James Cammarata

Fixing filter plugins directory from switch

parent 249fd2a7
...@@ -180,7 +180,8 @@ class TaskExecutor: ...@@ -180,7 +180,8 @@ class TaskExecutor:
final_items = [] final_items = []
for item in items: for item in items:
variables['item'] = item variables['item'] = item
if self._task.evaluate_conditional(variables): templar = Templar(loader=self._loader, shared_loader_obj=self._shared_loader_obj, variables=variables)
if self._task.evaluate_conditional(templar, variables):
final_items.append(item) final_items.append(item)
return [",".join(final_items)] return [",".join(final_items)]
else: else:
...@@ -208,13 +209,13 @@ class TaskExecutor: ...@@ -208,13 +209,13 @@ class TaskExecutor:
# get the connection and the handler for this execution # get the connection and the handler for this execution
self._connection = self._get_connection(variables) self._connection = self._get_connection(variables)
self._handler = self._get_action_handler(connection=self._connection) self._handler = self._get_action_handler(connection=self._connection, templar=templar)
# Evaluate the conditional (if any) for this task, which we do before running # Evaluate the conditional (if any) for this task, which we do before running
# the final task post-validation. We do this before the post validation due to # the final task post-validation. We do this before the post validation due to
# the fact that the conditional may specify that the task be skipped due to a # the fact that the conditional may specify that the task be skipped due to a
# variable not being present which would otherwise cause validation to fail # variable not being present which would otherwise cause validation to fail
if not self._task.evaluate_conditional(variables): if not self._task.evaluate_conditional(templar, variables):
debug("when evaulation failed, skipping this task") debug("when evaulation failed, skipping this task")
return dict(changed=False, skipped=True, skip_reason='Conditional check failed') return dict(changed=False, skipped=True, skip_reason='Conditional check failed')
...@@ -268,7 +269,7 @@ class TaskExecutor: ...@@ -268,7 +269,7 @@ class TaskExecutor:
return dict(failed=True, msg="The async task did not return valid JSON: %s" % str(e)) return dict(failed=True, msg="The async task did not return valid JSON: %s" % str(e))
if self._task.poll > 0: if self._task.poll > 0:
result = self._poll_async_result(result=result) result = self._poll_async_result(result=result, templar=templar)
# update the local copy of vars with the registered value, if specified, # update the local copy of vars with the registered value, if specified,
# or any facts which may have been generated by the module execution # or any facts which may have been generated by the module execution
...@@ -284,15 +285,15 @@ class TaskExecutor: ...@@ -284,15 +285,15 @@ class TaskExecutor:
# FIXME: make sure until is mutually exclusive with changed_when/failed_when # FIXME: make sure until is mutually exclusive with changed_when/failed_when
if self._task.until: if self._task.until:
cond.when = self._task.until cond.when = self._task.until
if cond.evaluate_conditional(vars_copy): if cond.evaluate_conditional(templar, vars_copy):
break break
elif (self._task.changed_when or self._task.failed_when) and 'skipped' not in result: elif (self._task.changed_when or self._task.failed_when) and 'skipped' not in result:
if self._task.changed_when: if self._task.changed_when:
cond.when = [ self._task.changed_when ] cond.when = [ self._task.changed_when ]
result['changed'] = cond.evaluate_conditional(vars_copy) result['changed'] = cond.evaluate_conditional(templar, vars_copy)
if self._task.failed_when: if self._task.failed_when:
cond.when = [ self._task.failed_when ] cond.when = [ self._task.failed_when ]
failed_when_result = cond.evaluate_conditional(vars_copy) failed_when_result = cond.evaluate_conditional(templar, vars_copy)
result['failed_when_result'] = result['failed'] = failed_when_result result['failed_when_result'] = result['failed'] = failed_when_result
if failed_when_result: if failed_when_result:
break break
...@@ -315,7 +316,7 @@ class TaskExecutor: ...@@ -315,7 +316,7 @@ class TaskExecutor:
debug("attempt loop complete, returning result") debug("attempt loop complete, returning result")
return result return result
def _poll_async_result(self, result): def _poll_async_result(self, result, templar):
''' '''
Polls for the specified JID to be complete Polls for the specified JID to be complete
''' '''
...@@ -339,6 +340,7 @@ class TaskExecutor: ...@@ -339,6 +340,7 @@ class TaskExecutor:
connection=self._connection, connection=self._connection,
connection_info=self._connection_info, connection_info=self._connection_info,
loader=self._loader, loader=self._loader,
templar=templar,
shared_loader_obj=self._shared_loader_obj, shared_loader_obj=self._shared_loader_obj,
) )
...@@ -391,7 +393,7 @@ class TaskExecutor: ...@@ -391,7 +393,7 @@ class TaskExecutor:
return connection return connection
def _get_action_handler(self, connection): def _get_action_handler(self, connection, templar):
''' '''
Returns the correct action plugin to handle the requestion task action Returns the correct action plugin to handle the requestion task action
''' '''
...@@ -411,6 +413,7 @@ class TaskExecutor: ...@@ -411,6 +413,7 @@ class TaskExecutor:
connection=connection, connection=connection,
connection_info=self._connection_info, connection_info=self._connection_info,
loader=self._loader, loader=self._loader,
templar=templar,
shared_loader_obj=self._shared_loader_obj, shared_loader_obj=self._shared_loader_obj,
) )
......
...@@ -225,21 +225,21 @@ class Block(Base, Become, Conditional, Taggable): ...@@ -225,21 +225,21 @@ class Block(Base, Become, Conditional, Taggable):
ti.deserialize(ti_data) ti.deserialize(ti_data)
self._task_include = ti self._task_include = ti
def evaluate_conditional(self, all_vars): def evaluate_conditional(self, templar, all_vars):
if len(self._dep_chain): if len(self._dep_chain):
for dep in self._dep_chain: for dep in self._dep_chain:
if not dep.evaluate_conditional(all_vars): if not dep.evaluate_conditional(templar, all_vars):
return False return False
if self._task_include is not None: if self._task_include is not None:
if not self._task_include.evaluate_conditional(all_vars): if not self._task_include.evaluate_conditional(templar, all_vars):
return False return False
if self._parent_block is not None: if self._parent_block is not None:
if not self._parent_block.evaluate_conditional(all_vars): if not self._parent_block.evaluate_conditional(templar, all_vars):
return False return False
elif self._role is not None: elif self._role is not None:
if not self._role.evaluate_conditional(all_vars): if not self._role.evaluate_conditional(templar, all_vars):
return False return False
return super(Block, self).evaluate_conditional(all_vars) return super(Block, self).evaluate_conditional(templar, all_vars)
def set_loader(self, loader): def set_loader(self, loader):
self._loader = loader self._loader = loader
......
...@@ -47,16 +47,16 @@ class Conditional: ...@@ -47,16 +47,16 @@ class Conditional:
if not isinstance(value, list): if not isinstance(value, list):
setattr(self, name, [ value ]) setattr(self, name, [ value ])
def evaluate_conditional(self, all_vars): def evaluate_conditional(self, templar, all_vars):
''' '''
Loops through the conditionals set on this object, returning Loops through the conditionals set on this object, returning
False if any of them evaluate as such. False if any of them evaluate as such.
''' '''
templar = Templar(loader=self._loader, variables=all_vars, fail_on_undefined=False)
for conditional in self.when: for conditional in self.when:
if not self._check_conditional(conditional, templar, all_vars): if not self._check_conditional(conditional, templar, all_vars):
return False return False
return True return True
def _check_conditional(self, conditional, templar, all_vars): def _check_conditional(self, conditional, templar, all_vars):
......
...@@ -266,14 +266,14 @@ class Task(Base, Conditional, Taggable, Become): ...@@ -266,14 +266,14 @@ class Task(Base, Conditional, Taggable, Become):
super(Task, self).deserialize(data) super(Task, self).deserialize(data)
def evaluate_conditional(self, all_vars): def evaluate_conditional(self, templar, all_vars):
if self._block is not None: if self._block is not None:
if not self._block.evaluate_conditional(all_vars): if not self._block.evaluate_conditional(templar, all_vars):
return False return False
if self._task_include is not None: if self._task_include is not None:
if not self._task_include.evaluate_conditional(all_vars): if not self._task_include.evaluate_conditional(templar, all_vars):
return False return False
return super(Task, self).evaluate_conditional(all_vars) return super(Task, self).evaluate_conditional(templar, all_vars)
def set_loader(self, loader): def set_loader(self, loader):
''' '''
......
...@@ -44,11 +44,12 @@ class ActionBase: ...@@ -44,11 +44,12 @@ class ActionBase:
action in use. action in use.
''' '''
def __init__(self, task, connection, connection_info, loader, shared_loader_obj): def __init__(self, task, connection, connection_info, loader, templar, shared_loader_obj):
self._task = task self._task = task
self._connection = connection self._connection = connection
self._connection_info = connection_info self._connection_info = connection_info
self._loader = loader self._loader = loader
self._templar = templar
self._shared_loader_obj = shared_loader_obj self._shared_loader_obj = shared_loader_obj
self._shell = self.get_shell() self._shell = self.get_shell()
......
...@@ -48,7 +48,7 @@ class ActionModule(ActionBase): ...@@ -48,7 +48,7 @@ class ActionModule(ActionBase):
cond = Conditional(loader=self._loader) cond = Conditional(loader=self._loader)
for that in thats: for that in thats:
cond.when = [ that ] cond.when = [ that ]
test_result = cond.evaluate_conditional(all_vars=task_vars) test_result = cond.evaluate_conditional(templar=self._templar, all_vars=task_vars)
if not test_result: if not test_result:
result = dict( result = dict(
failed = True, failed = True,
......
...@@ -19,7 +19,6 @@ __metaclass__ = type ...@@ -19,7 +19,6 @@ __metaclass__ = type
from ansible.plugins.action import ActionBase from ansible.plugins.action import ActionBase
from ansible.utils.boolean import boolean from ansible.utils.boolean import boolean
from ansible.template import Templar
class ActionModule(ActionBase): class ActionModule(ActionBase):
''' Print statements during execution ''' ''' Print statements during execution '''
...@@ -35,8 +34,7 @@ class ActionModule(ActionBase): ...@@ -35,8 +34,7 @@ class ActionModule(ActionBase):
result = dict(msg=self._task.args['msg']) result = dict(msg=self._task.args['msg'])
# FIXME: move the LOOKUP_REGEX somewhere else # FIXME: move the LOOKUP_REGEX somewhere else
elif 'var' in self._task.args: # and not utils.LOOKUP_REGEX.search(self._task.args['var']): elif 'var' in self._task.args: # and not utils.LOOKUP_REGEX.search(self._task.args['var']):
templar = Templar(loader=self._loader, shared_loader_obj=self._shared_loader_obj, variables=task_vars) results = self._templar.template(self._task.args['var'], convert_bare=True)
results = templar.template(self._task.args['var'], convert_bare=True)
result = dict() result = dict()
result[self._task.args['var']] = results result[self._task.args['var']] = results
else: else:
......
...@@ -19,7 +19,6 @@ __metaclass__ = type ...@@ -19,7 +19,6 @@ __metaclass__ = type
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansible.plugins.action import ActionBase from ansible.plugins.action import ActionBase
from ansible.template import Templar
from ansible.utils.boolean import boolean from ansible.utils.boolean import boolean
class ActionModule(ActionBase): class ActionModule(ActionBase):
...@@ -27,11 +26,10 @@ class ActionModule(ActionBase): ...@@ -27,11 +26,10 @@ class ActionModule(ActionBase):
TRANSFERS_FILES = False TRANSFERS_FILES = False
def run(self, tmp=None, task_vars=dict()): def run(self, tmp=None, task_vars=dict()):
templar = Templar(loader=self._loader, variables=task_vars)
facts = dict() facts = dict()
if self._task.args: if self._task.args:
for (k, v) in self._task.args.iteritems(): for (k, v) in self._task.args.iteritems():
k = templar.template(k) k = self._templar.template(k)
if isinstance(v, basestring) and v.lower() in ('true', 'false', 'yes', 'no'): if isinstance(v, basestring) and v.lower() in ('true', 'false', 'yes', 'no'):
v = boolean(v) v = boolean(v)
facts[k] = v facts[k] = v
......
...@@ -21,7 +21,6 @@ import base64 ...@@ -21,7 +21,6 @@ import base64
import os import os
from ansible.plugins.action import ActionBase from ansible.plugins.action import ActionBase
from ansible.template import Templar
from ansible.utils.hashing import checksum_s from ansible.utils.hashing import checksum_s
class ActionModule(ActionBase): class ActionModule(ActionBase):
...@@ -99,11 +98,10 @@ class ActionModule(ActionBase): ...@@ -99,11 +98,10 @@ class ActionModule(ActionBase):
dest = os.path.join(dest, base) dest = os.path.join(dest, base)
# template the source data locally & get ready to transfer # template the source data locally & get ready to transfer
templar = Templar(loader=self._loader, variables=task_vars)
try: try:
with open(source, 'r') as f: with open(source, 'r') as f:
template_data = f.read() template_data = f.read()
resultant = templar.template(template_data, preserve_trailing_newlines=True) resultant = self._templar.template(template_data, preserve_trailing_newlines=True)
except Exception as e: except Exception as e:
return dict(failed=True, msg=type(e).__name__ + ": " + str(e)) return dict(failed=True, msg=type(e).__name__ + ": " + str(e))
......
../../../lib/ansible/runner/filter_plugins
\ No newline at end of file
# (c) 2014, Brian Coca <bcoca@ansible.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 __future__ import absolute_import
import math
import collections
from ansible import errors
def unique(a):
if isinstance(a,collections.Hashable):
c = set(a)
else:
c = []
for x in a:
if x not in c:
c.append(x)
return c
def intersect(a, b):
if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable):
c = set(a) & set(b)
else:
c = unique(filter(lambda x: x in b, a))
return c
def difference(a, b):
if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable):
c = set(a) - set(b)
else:
c = unique(filter(lambda x: x not in b, a))
return c
def symmetric_difference(a, b):
if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable):
c = set(a) ^ set(b)
else:
c = unique(filter(lambda x: x not in intersect(a,b), union(a,b)))
return c
def union(a, b):
if isinstance(a,collections.Hashable) and isinstance(b,collections.Hashable):
c = set(a) | set(b)
else:
c = unique(a + b)
return c
def min(a):
_min = __builtins__.get('min')
return _min(a);
def max(a):
_max = __builtins__.get('max')
return _max(a);
def isnotanumber(x):
try:
return math.isnan(x)
except TypeError:
return False
def logarithm(x, base=math.e):
try:
if base == 10:
return math.log10(x)
else:
return math.log(x, base)
except TypeError, e:
raise errors.AnsibleFilterError('log() can only be used on numbers: %s' % str(e))
def power(x, y):
try:
return math.pow(x, y)
except TypeError, e:
raise errors.AnsibleFilterError('pow() can only be used on numbers: %s' % str(e))
def inversepower(x, base=2):
try:
if base == 2:
return math.sqrt(x)
else:
return math.pow(x, 1.0/float(base))
except TypeError, e:
raise errors.AnsibleFilterError('root() can only be used on numbers: %s' % str(e))
class FilterModule(object):
''' Ansible math jinja2 filters '''
def filters(self):
return {
# general math
'isnan': isnotanumber,
'min' : min,
'max' : max,
# exponents and logarithms
'log': logarithm,
'pow': power,
'root': inversepower,
# set theory
'unique' : unique,
'intersect': intersect,
'difference': difference,
'symmetric_difference': symmetric_difference,
'union': union,
}
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