diff --git a/lib/ansible/modules/network/nxos/nxos_facts.py b/lib/ansible/modules/network/nxos/nxos_facts.py index 6a517f6034..bf3ebd9e00 100644 --- a/lib/ansible/modules/network/nxos/nxos_facts.py +++ b/lib/ansible/modules/network/nxos/nxos_facts.py @@ -218,7 +218,6 @@ class FactsBase(object): yield self.transform_dict(item, keymap) - class Default(FactsBase): VERSION_MAP = frozenset([ @@ -315,23 +314,38 @@ class Interfaces(FactsBase): return interfaces def populate_neighbors(self, data): - # if there are no neighbors the show command returns - # ERROR: No neighbour information - if data.startswith('ERROR'): - return dict() - - lines = data.split('\n') - regex = re.compile('(\S+)\s+(\S+)\s+\d+\s+\w+\s+(\S+)') - objects = dict() + if isinstance(data, str): + # if there are no neighbors the show command returns + # ERROR: No neighbour information + if data.startswith('ERROR'): + return dict() + + lines = data.split('\n') + regex = re.compile('(\S+)\s+(\S+)\s+\d+\s+\w+\s+(\S+)') + + for item in data.split('\n')[4:-1]: + match = regex.match(item) + if match: + nbor = {'host': match.group(1), 'port': match.group(3)} + if match.group(2) not in objects: + objects[match.group(2)] = [] + objects[match.group(2)].append(nbor) + + elif isinstance(data, dict): + data = data['TABLE_nbor']['ROW_nbor'] + if isinstance(data, dict): + data = [data] + + for item in data: + local_intf = item['l_port_id'] + if local_intf not in objects: + objects[local_intf] = list() + nbor = dict() + nbor['port'] = item['port_id'] + nbor['host'] = item['chassis_id'] + objects[local_intf].append(nbor) - for item in data.split('\n')[4:-1]: - match = regex.match(item) - if match: - nbor = {'host': match.group(1), 'port': match.group(3)} - if match.group(2) not in objects: - objects[match.group(2)] = [] - objects[match.group(2)].append(nbor) return objects def parse_ipv6_interfaces(self, data): diff --git a/test/integration/targets/nxos_facts/tasks/nxapi.yaml b/test/integration/targets/nxos_facts/tasks/nxapi.yaml index 148d8d6486..ea525379f7 100644 --- a/test/integration/targets/nxos_facts/tasks/nxapi.yaml +++ b/test/integration/targets/nxos_facts/tasks/nxapi.yaml @@ -8,8 +8,21 @@ - name: set test_items set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" +- name: enable nxapi + nxos_config: + lines: + - feature nxapi + - nxapi http port 80 + provider: "{{ cli }}" + - name: run test case include: "{{ test_case_to_run }}" with_items: "{{ test_items }}" loop_control: loop_var: test_case_to_run + +- name: disable nxapi + nxos_config: + lines: + - no feature nxapi + provider: "{{ cli }}"