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 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,
}