ansible.builtin.mandatory filter – make a variable’s existence mandatory

Note

This filter plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name mandatory. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.mandatory for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

Synopsis

  • Depending on context undefined variables can be ignored or skipped, this ensures they force an error.

Input

This describes the input of the filter, the value before | ansible.builtin.mandatory.

Parameter

Comments

Input

any / required

Mandatory expression.

Keyword parameters

This describes keyword parameters of the filter. These are the values key1=value1, key2=value2 and so on in the following example: input | ansible.builtin.mandatory(key1=value1, key2=value2, ...)

Parameter

Comments

msg

string

The customized message that is printed when the given variable is not defined.

Examples

# results in a Filter Error
{{ notdefined | mandatory }}

# print a custom error message
{{ notdefined | mandatory(msg='This variable is required.') }}

Return Value

Key

Description

Return value

any

The input if defined, otherwise an error.

Returned: success

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.