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.
This commit is contained in:
bcavns01 2019-07-10 15:53:48 -04:00 committed by Sloane Hertel
parent 3ae2d15e45
commit 0e7e3c0ae8

View file

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