From 57d8b68c03efe01c570a4cd36b686117c812626f Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 24 Jun 2019 12:43:18 -0500 Subject: [PATCH] Use to_text on value to activate _fail_with_undefined_error (#58214) * Use to_text on value to activate _fail_with_undefined_error. Fixes #12186 * Add comment explaining in depth what to_text gives us --- changelogs/fragments/regex-tests-undefined.yml | 3 +++ lib/ansible/plugins/test/core.py | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 changelogs/fragments/regex-tests-undefined.yml diff --git a/changelogs/fragments/regex-tests-undefined.yml b/changelogs/fragments/regex-tests-undefined.yml new file mode 100644 index 00000000000..f227ebd7bae --- /dev/null +++ b/changelogs/fragments/regex-tests-undefined.yml @@ -0,0 +1,3 @@ +bugfixes: +- regex tests - Fail with undefined error if the value is + undefined (https://github.com/ansible/ansible/issues/12186) diff --git a/lib/ansible/plugins/test/core.py b/lib/ansible/plugins/test/core.py index 2cd96a1b430..66aff2a14bf 100644 --- a/lib/ansible/plugins/test/core.py +++ b/lib/ansible/plugins/test/core.py @@ -24,6 +24,7 @@ import operator as py_operator from distutils.version import LooseVersion, StrictVersion from ansible import errors +from ansible.module_utils._text import to_text from ansible.module_utils.common._collections_compat import MutableMapping, MutableSequence from ansible.utils.display import Display @@ -114,6 +115,9 @@ def regex(value='', pattern='', ignorecase=False, multiline=False, match_type='s This is likely only useful for `search` and `match` which already have their own filters. ''' + # In addition to ensuring the correct type, to_text here will ensure + # _fail_with_undefined_error happens if the value is Undefined + value = to_text(value, errors='surrogate_or_strict') flags = 0 if ignorecase: flags |= re.I