From 03b888052b43fa777f54936e0086317a5ee97c83 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Fri, 18 Aug 2017 14:05:26 -0400 Subject: [PATCH] fixes bugs in network filter for parsing ip address (#28404) also adds parse_cli and parse_cli_textfsm to changelog --- CHANGELOG.md | 4 ++++ lib/ansible/module_utils/network_common.py | 11 ++++++++--- lib/ansible/plugins/filter/network.py | 8 +++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60fcfd9593..bbc9c58802 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -128,6 +128,10 @@ Ansible Changes By Release - profile_roles - stderr +#### New Filters: +- parse_cli +- parse_cli_textfsm + #### New Inventory Plugins: - advanced_host_list - constructed_groups diff --git a/lib/ansible/module_utils/network_common.py b/lib/ansible/module_utils/network_common.py index 898618948a..7d6f2be12a 100644 --- a/lib/ansible/module_utils/network_common.py +++ b/lib/ansible/module_utils/network_common.py @@ -345,17 +345,22 @@ class Template: self.env = Environment() self.env.filters.update({'ternary': ternary}) - def __call__(self, value, variables=None): + def __call__(self, value, variables=None, fail_on_undefined=True): variables = variables or {} if not self.contains_vars(value): return value - value = self.env.from_string(value).render(variables) + try: + value = self.env.from_string(value).render(variables) + except UndefinedError: + if not fail_on_undefined: + return None + raise if value: try: return ast.literal_eval(value) - except ValueError: + except: return str(value) else: return None diff --git a/lib/ansible/plugins/filter/network.py b/lib/ansible/plugins/filter/network.py index 98dae86c60..50952b4fa2 100644 --- a/lib/ansible/plugins/filter/network.py +++ b/lib/ansible/plugins/filter/network.py @@ -131,7 +131,13 @@ def parse_cli(output, tmpl): else: items.append(None) - objects.append(dict([(k, template(v, {'item': items})) for k, v in iteritems(value)])) + obj = {} + for k, v in iteritems(value): + try: + obj[k] = template(v, {'item': items}, fail_on_undefined=False) + except: + obj[k] = None + objects.append(obj) elif isinstance(value, Mapping): items = list()