1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add Jinja2 filter |bool : return boolean interpretation of the value

'yes', 'on', '1', 'true', insensitively, and 1 are true,
 everything else is false
This commit is contained in:
Stoned Elipot 2013-07-03 02:13:19 +02:00
parent 25aaee4c38
commit 0b3483cf03

View file

@ -19,6 +19,7 @@ import base64
import json import json
import os.path import os.path
import yaml import yaml
import types
from ansible import errors from ansible import errors
def to_nice_yaml(*a, **kw): def to_nice_yaml(*a, **kw):
@ -49,6 +50,17 @@ def mandatory(a):
raise errors.AnsibleError('Mandatory variable not defined.') raise errors.AnsibleError('Mandatory variable not defined.')
return a 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): class FilterModule(object):
''' Ansible core jinja2 filters ''' ''' Ansible core jinja2 filters '''
@ -78,5 +90,8 @@ class FilterModule(object):
# variable existence # variable existence
'mandatory': mandatory, 'mandatory': mandatory,
# value as boolean
'bool': bool,
} }