From 1def2a7ebeeab1cab80d71c351b0e02593e81e0e Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Thu, 4 Jun 2026 06:28:35 +0200 Subject: [PATCH] [PR #12140/48db8630 backport][stable-12] filetree lookup: handle invalid exclude regex with AnsibleError (#12183) 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 53f43b39cb..365fea1b6c 100644 --- a/plugins/lookup/filetree.py +++ b/plugins/lookup/filetree.py @@ -142,6 +142,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 @@ -223,7 +224,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: