From 24593f2ffbd589c1e88a73423a1e2955f6bc6f37 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 27 Sep 2018 15:58:41 -0400 Subject: [PATCH] avoid spurious warnigns on unique filter fixes #46189 --- .../avoid_spurious_unique_warnings.yml | 2 ++ lib/ansible/plugins/filter/mathstuff.py | 20 +++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/avoid_spurious_unique_warnings.yml diff --git a/changelogs/fragments/avoid_spurious_unique_warnings.yml b/changelogs/fragments/avoid_spurious_unique_warnings.yml new file mode 100644 index 00000000000..2bb8e6331a3 --- /dev/null +++ b/changelogs/fragments/avoid_spurious_unique_warnings.yml @@ -0,0 +1,2 @@ +bugfixes: + - Narrow the cases in which we warn about Jinja2 unique filters https://github.com/ansible/ansible/issues/46189 diff --git a/lib/ansible/plugins/filter/mathstuff.py b/lib/ansible/plugins/filter/mathstuff.py index b368403d386..67f628388ff 100644 --- a/lib/ansible/plugins/filter/mathstuff.py +++ b/lib/ansible/plugins/filter/mathstuff.py @@ -51,7 +51,12 @@ except ImportError: @environmentfilter def unique(environment, a, case_sensitive=False, attribute=None): - error = None + def _do_fail(e): + if case_sensitive or attribute: + raise AnsibleFilterError("Jinja2's unique filter failed and we cannot fall back to Ansible's version " + "as it does not support the parameters supplied", orig_exc=e) + + error = e = None try: if HAS_UNIQUE: c = do_unique(environment, a, case_sensitive=case_sensitive, attribute=attribute) @@ -59,14 +64,13 @@ def unique(environment, a, case_sensitive=False, attribute=None): c = set(c) else: c = list(c) - + except TypeError as e: + _do_fail(e) except Exception as e: - if case_sensitive or attribute: - raise AnsibleFilterError("Jinja2's unique filter failed and we cannot fall back to Ansible's version " - "as it does not support the parameters supplied", orig_exc=e) - else: - display.warning('Falling back to Ansible unique filter as Jinja2 one failed: %s' % to_text(e)) - error = e + _do_fail(e) + display.warning('Falling back to Ansible unique filter as Jinja2 one failed: %s' % to_text(e)) + finally: + error = e if not HAS_UNIQUE or error: