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: