Commit 62a6378c by Brian Coca

Merge pull request #8792 from bcoca/ternary_filter

ternary filter
parents 09127813 88daac49
...@@ -384,6 +384,10 @@ Hash types available depend on the master system running ansible, ...@@ -384,6 +384,10 @@ Hash types available depend on the master system running ansible,
Other Useful Filters Other Useful Filters
-------------------- --------------------
To use one value on true and another on false::
{{ name == "John" | ternary('Mr','Ms') }}
To concatenate a list into a string:: To concatenate a list into a string::
{{ list | join(" ") }} {{ list | join(" ") }}
......
...@@ -174,6 +174,13 @@ def regex_replace(value='', pattern='', replacement='', ignorecase=False): ...@@ -174,6 +174,13 @@ def regex_replace(value='', pattern='', replacement='', ignorecase=False):
_re = re.compile(pattern, flags=flags) _re = re.compile(pattern, flags=flags)
return _re.sub(replacement, value) return _re.sub(replacement, value)
def ternary(value, true_val, false_val):
''' value ? true_val : false_val '''
if value:
return true_val
else:
return false_val
def unique(a): def unique(a):
if isinstance(a,collections.Hashable): if isinstance(a,collections.Hashable):
c = set(a) c = set(a)
...@@ -374,6 +381,9 @@ class FilterModule(object): ...@@ -374,6 +381,9 @@ class FilterModule(object):
'regex': regex, 'regex': regex,
'regex_replace': regex_replace, 'regex_replace': regex_replace,
# ? : ;
'ternary': ternary,
# list # list
'unique' : unique, 'unique' : unique,
'intersect': intersect, 'intersect': intersect,
......
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