mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
fixes issue where filter wouldn't error on undefined var (#30921)
The filter will now correctly error on an undefined variable when trying to template the key `value`
This commit is contained in:
parent
932f62ab57
commit
909100bd2c
2 changed files with 6 additions and 10 deletions
|
@ -35,7 +35,7 @@ from ansible.module_utils.six import iteritems, string_types
|
||||||
from ansible.module_utils.basic import AnsibleFallbackNotFound
|
from ansible.module_utils.basic import AnsibleFallbackNotFound
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from jinja2 import Environment
|
from jinja2 import Environment, StrictUndefined
|
||||||
from jinja2.exceptions import UndefinedError
|
from jinja2.exceptions import UndefinedError
|
||||||
HAS_JINJA2 = True
|
HAS_JINJA2 = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -376,11 +376,12 @@ class Template:
|
||||||
raise ImportError("jinja2 is required but does not appear to be installed. "
|
raise ImportError("jinja2 is required but does not appear to be installed. "
|
||||||
"It can be installed using `pip install jinja2`")
|
"It can be installed using `pip install jinja2`")
|
||||||
|
|
||||||
self.env = Environment()
|
self.env = Environment(undefined=StrictUndefined)
|
||||||
self.env.filters.update({'ternary': ternary})
|
self.env.filters.update({'ternary': ternary})
|
||||||
|
|
||||||
def __call__(self, value, variables=None, fail_on_undefined=True):
|
def __call__(self, value, variables=None, fail_on_undefined=True):
|
||||||
variables = variables or {}
|
variables = variables or {}
|
||||||
|
|
||||||
if not self.contains_vars(value):
|
if not self.contains_vars(value):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@ -399,13 +400,6 @@ class Template:
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def can_template(self, tmpl):
|
|
||||||
try:
|
|
||||||
self(tmpl)
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def contains_vars(self, data):
|
def contains_vars(self, data):
|
||||||
if isinstance(data, string_types):
|
if isinstance(data, string_types):
|
||||||
for marker in (self.env.block_start_string, self.env.variable_start_string, self.env.comment_start_string):
|
for marker in (self.env.block_start_string, self.env.variable_start_string, self.env.comment_start_string):
|
||||||
|
|
|
@ -87,9 +87,11 @@ def parse_cli(output, tmpl):
|
||||||
for name, attrs in iteritems(spec['keys']):
|
for name, attrs in iteritems(spec['keys']):
|
||||||
value = attrs['value']
|
value = attrs['value']
|
||||||
|
|
||||||
if template.can_template(value):
|
try:
|
||||||
variables = spec.get('vars', {})
|
variables = spec.get('vars', {})
|
||||||
value = template(value, variables)
|
value = template(value, variables)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
if 'start_block' in attrs and 'end_block' in attrs:
|
if 'start_block' in attrs and 'end_block' in attrs:
|
||||||
start_block = re.compile(attrs['start_block'])
|
start_block = re.compile(attrs['start_block'])
|
||||||
|
|
Loading…
Add table
Reference in a new issue