From 828eef091e3835937ea522dc5cf031b83951f88c Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Thu, 4 Jun 2026 06:28:33 +0200 Subject: [PATCH] [PR #12140/48db8630 backport][stable-13] filetree lookup: handle invalid exclude regex with AnsibleError (#12184) filetree lookup: handle invalid exclude regex with AnsibleError (#12140) * filetree lookup - handle invalid exclude regex with AnsibleError Wrap re.compile() for the exclude option so invalid regular expressions produce a clear AnsibleError instead of an uncaught re.error traceback. * add changelog fragment * add changelog fragment * Fix changelog fragment line endings (LF) * Used AnsibleLookupError instead of AnsibleError * Update changelogs/fragments/12140-filetree-exclude-regex-error.yml * Used AnsibleLookupError instead of AnsibleError * Updated changelog format --------- (cherry picked from commit 48db863096c9e9bdcff97828fbc11a35921b7369) Co-authored-by: Santosh Mahale Co-authored-by: Cursor Co-authored-by: Felix Fontein --- .../fragments/12140-filetree-exclude-regex-error.yml | 2 ++ plugins/lookup/filetree.py | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/12140-filetree-exclude-regex-error.yml diff --git a/changelogs/fragments/12140-filetree-exclude-regex-error.yml b/changelogs/fragments/12140-filetree-exclude-regex-error.yml new file mode 100644 index 0000000000..6608ceb3c1 --- /dev/null +++ b/changelogs/fragments/12140-filetree-exclude-regex-error.yml @@ -0,0 +1,2 @@ +bugfixes: + - filetree lookup plugin - raise ``AnsibleLookupError`` when the ``exclude`` option contains an invalid regular expression instead of an uncaught ``re.error`` (https://github.com/ansible-collections/community.general/pull/12140). diff --git a/plugins/lookup/filetree.py b/plugins/lookup/filetree.py index 159a9f46e0..ccad6e445c 100644 --- a/plugins/lookup/filetree.py +++ b/plugins/lookup/filetree.py @@ -143,6 +143,7 @@ try: except ImportError: pass +from ansible.errors import AnsibleLookupError from ansible.module_utils.common.text.converters import to_native, to_text from ansible.plugins.lookup import LookupBase from ansible.utils.display import Display @@ -227,7 +228,13 @@ class LookupModule(LookupBase): # Regular expression for exclude exclude = self.get_option("exclude") - exclude_pattern = re.compile(exclude) if exclude else None + if exclude: + try: + exclude_pattern = re.compile(exclude) + except re.error as e: + raise AnsibleLookupError(f"Invalid exclude regular expression {exclude!r}: {e}") from e + else: + exclude_pattern = None ret = [] for term in terms: