mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Warn when jinja2 delimiters are found in a when statement (#20312)
* Warn when jinja2 delimiters are found in a when statement * Use templar._clean_data to look for templating
This commit is contained in:
parent
1fe67f9f43
commit
ff20ab7d44
1 changed files with 11 additions and 0 deletions
|
@ -32,6 +32,12 @@ from ansible.template import Templar
|
||||||
from ansible.template.safe_eval import safe_eval
|
from ansible.template.safe_eval import safe_eval
|
||||||
from ansible.module_utils._text import to_native
|
from ansible.module_utils._text import to_native
|
||||||
|
|
||||||
|
try:
|
||||||
|
from __main__ import display
|
||||||
|
except ImportError:
|
||||||
|
from ansible.utils.display import Display
|
||||||
|
display = Display()
|
||||||
|
|
||||||
DEFINED_REGEX = re.compile(r'(hostvars\[.+\]|[\w_]+)\s+(not\s+is|is|is\s+not)\s+(defined|undefined)')
|
DEFINED_REGEX = re.compile(r'(hostvars\[.+\]|[\w_]+)\s+(not\s+is|is|is\s+not)\s+(defined|undefined)')
|
||||||
LOOKUP_REGEX = re.compile(r'lookup\s*\(')
|
LOOKUP_REGEX = re.compile(r'lookup\s*\(')
|
||||||
VALID_VAR_REGEX = re.compile("^[_A-Za-z][_a-zA-Z0-9]*$")
|
VALID_VAR_REGEX = re.compile("^[_A-Za-z][_a-zA-Z0-9]*$")
|
||||||
|
@ -132,6 +138,11 @@ class Conditional:
|
||||||
if conditional in all_vars and VALID_VAR_REGEX.match(conditional):
|
if conditional in all_vars and VALID_VAR_REGEX.match(conditional):
|
||||||
conditional = all_vars[conditional]
|
conditional = all_vars[conditional]
|
||||||
|
|
||||||
|
if templar._clean_data(conditional) != conditional:
|
||||||
|
display.warning('when statements should not include jinja2 '
|
||||||
|
'templating delimiters such as {{ }} or {%% %%}. '
|
||||||
|
'Found: %s' % conditional)
|
||||||
|
|
||||||
# make sure the templar is using the variables specified with this method
|
# make sure the templar is using the variables specified with this method
|
||||||
templar.set_available_variables(variables=all_vars)
|
templar.set_available_variables(variables=all_vars)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue