Add msg parameter to the mandatory filter (#56724)

* Add msg parameter to the mandatory filter

The `mandatory` filter would be more useful, particularly when dealing with nested dictionaries, with the simple addition of a `msg` parameter for supplying it with a custom failure message.
pull/58945/head
bcavns01 5 years ago committed by Sloane Hertel
parent 3ae2d15e45
commit 0e7e3c0ae8

@ -280,7 +280,7 @@ def to_uuid(string):
return str(uuid.uuid5(UUID_NAMESPACE_ANSIBLE, str(string))) return str(uuid.uuid5(UUID_NAMESPACE_ANSIBLE, str(string)))
def mandatory(a): def mandatory(a, msg=None):
from jinja2.runtime import Undefined from jinja2.runtime import Undefined
''' Make a variable mandatory ''' ''' Make a variable mandatory '''
@ -289,7 +289,12 @@ def mandatory(a):
name = "'%s' " % to_text(a._undefined_name) name = "'%s' " % to_text(a._undefined_name)
else: else:
name = '' name = ''
if msg is not None:
raise AnsibleFilterError(to_native(msg))
else:
raise AnsibleFilterError("Mandatory variable %s not defined." % name) raise AnsibleFilterError("Mandatory variable %s not defined." % name)
return a return a

Loading…
Cancel
Save