Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add msg parameter to the mandatory filter #56724

Open
wants to merge 1 commit into
base: devel
from

Conversation

Projects
None yet
4 participants
@bcavns01
Copy link

commented May 21, 2019

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.

SUMMARY

This change would make it possible to pass a custom failure message to be displayed when the mandatory filter catches an undefined variable.

The mandatory filter is great, but its failure message sometimes lacks enough context to be helpful with more complex templates and nested dictionaries.

This PR will let template designers add more context with custom error messages via a msg parameter.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

mandatory

ADDITIONAL INFORMATION

Before:

{% set current_prefix = source_details['prefixes'][neighbor['uplink_name']]|mandatory %}
fatal: [mpr01.xxx]: FAILED! => {"changed": false, "msg": "AnsibleFilterError: Mandatory variable 'verizon'  not defined."}

After:

{% set current_prefix = source_details['prefixes'][neighbor['uplink_name']]|mandatory(neighbor['uplink_name'] ~ ' not properly defined in source_details dictionary.') %}
fatal: [mpr01.xxx]: FAILED! => {"changed": false, "msg": "AnsibleFilterError: verizon not properly defined in source_details dictionary."}
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.
@s-hertel
Copy link
Contributor

left a comment

Works for me.

raise AnsibleFilterError("Mandatory variable %s not defined." % name)

if msg is not None:
raise AnsibleFilterError(str(msg))

This comment has been minimized.

Copy link
@s-hertel

s-hertel May 24, 2019

Contributor

Use to_native instead of str.

Suggested change
raise AnsibleFilterError(str(msg))
raise AnsibleFilterError(to_native(msg))

@ansibot ansibot added needs_revision and removed core_review labels May 24, 2019

@ansibot ansibot added the stale_ci label Jun 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.