From 0b3483cf035009880a2a97d48537b000c9ea3bda Mon Sep 17 00:00:00 2001 From: Stoned Elipot Date: Wed, 3 Jul 2013 02:13:19 +0200 Subject: [PATCH] Add Jinja2 filter |bool : return boolean interpretation of the value 'yes', 'on', '1', 'true', insensitively, and 1 are true, everything else is false --- lib/ansible/runner/filter_plugins/core.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/ansible/runner/filter_plugins/core.py b/lib/ansible/runner/filter_plugins/core.py index 76c12e88dc..107c5e1fcb 100644 --- a/lib/ansible/runner/filter_plugins/core.py +++ b/lib/ansible/runner/filter_plugins/core.py @@ -19,6 +19,7 @@ import base64 import json import os.path import yaml +import types from ansible import errors def to_nice_yaml(*a, **kw): @@ -49,6 +50,17 @@ def mandatory(a): raise errors.AnsibleError('Mandatory variable not defined.') return a +def bool(a): + ''' return a bool for the arg ''' + if a is None or type(a) == bool: + return a + if type(a) in types.StringTypes: + a = a.lower() + if a in ['yes', 'on', '1', 'true', 1]: + return True + else: + return False + class FilterModule(object): ''' Ansible core jinja2 filters ''' @@ -78,5 +90,8 @@ class FilterModule(object): # variable existence 'mandatory': mandatory, + + # value as boolean + 'bool': bool, }