Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes issue where filter wouldn't error on undefined var #30921

Merged
merged 1 commit into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 3 additions & 9 deletions lib/ansible/module_utils/network_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from ansible.module_utils.basic import AnsibleFallbackNotFound

try:
from jinja2 import Environment
from jinja2 import Environment, StrictUndefined
from jinja2.exceptions import UndefinedError
HAS_JINJA2 = True
except ImportError:
Expand Down Expand Up @@ -376,11 +376,12 @@ def __init__(self):
raise ImportError("jinja2 is required but does not appear to be installed. "
"It can be installed using `pip install jinja2`")

self.env = Environment()
self.env = Environment(undefined=StrictUndefined)
self.env.filters.update({'ternary': ternary})

def __call__(self, value, variables=None, fail_on_undefined=True):
variables = variables or {}

if not self.contains_vars(value):
return value

Expand All @@ -399,13 +400,6 @@ def __call__(self, value, variables=None, fail_on_undefined=True):
else:
return None

def can_template(self, tmpl):
try:
self(tmpl)
return True
except:
return False

def contains_vars(self, data):
if isinstance(data, string_types):
for marker in (self.env.block_start_string, self.env.variable_start_string, self.env.comment_start_string):
Expand Down
4 changes: 3 additions & 1 deletion lib/ansible/plugins/filter/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ def parse_cli(output, tmpl):
for name, attrs in iteritems(spec['keys']):
value = attrs['value']

if template.can_template(value):
try:
variables = spec.get('vars', {})
value = template(value, variables)
except:
pass

if 'start_block' in attrs and 'end_block' in attrs:
start_block = re.compile(attrs['start_block'])
Expand Down