def main(): module = AnsibleModule(argument_spec=dict( host=dict(required=False), hosts=dict(required=False, type='list'), filename=dict(required=False), filepath=dict(required=False), anchor=dict(required=False, type='list'), ), mutually_exclusive=[['host', 'hosts', 'anchor']], supports_check_mode=True) m_args = module.params global debug_fname debug_fname = create_debug_file("/tmp/conn_graph_debug.txt") anchor = m_args['anchor'] if m_args['hosts']: hostnames = m_args['hosts'] elif m_args['host']: hostnames = [m_args['host']] else: # return the whole graph hostnames = [] try: # When called by pytest, the file path is obscured to /tmp/.../. # we need the caller to tell us where the graph files are with # filepath argument. if m_args['filepath']: global LAB_GRAPHFILE_PATH LAB_GRAPHFILE_PATH = m_args['filepath'] if m_args['filename']: filename = os.path.join(LAB_GRAPHFILE_PATH, m_args['filename']) lab_graph = Parse_Lab_Graph(filename) lab_graph.parse_graph() else: # When calling passed in anchor instead of hostnames, # the caller is asking to return the whole graph. This # is needed when configuring the root fanout switch. target = anchor if anchor else hostnames lab_graph = find_graph(target) # early return for the whole graph or empty graph file(vtestbed) if (not hostnames or not lab_graph.devices and not lab_graph.links and not lab_graph.vlanport): results = { 'device_info': lab_graph.devices, 'device_conn': lab_graph.links, 'device_port_vlans': lab_graph.vlanport, } module.exit_json(ansible_facts=results) succeed, results = build_results(lab_graph, hostnames) if succeed: module.exit_json(ansible_facts=results) else: module.fail_json(msg=results) except (IOError, OSError): module.fail_json(msg="Can not find lab graph file under {}".format( LAB_GRAPHFILE_PATH)) except Exception as e: module.fail_json(msg=traceback.format_exc())
def main(): module = AnsibleModule(argument_spec=dict( host=dict(required=False), hosts=dict(required=False, type='list'), filename=dict(required=False), filepath=dict(required=False), anchor=dict(required=False, type='list'), ), mutually_exclusive=[['host', 'hosts', 'anchor']], supports_check_mode=True) m_args = module.params global debug_fname debug_fname = create_debug_file("/tmp/conn_graph_debug.txt") anchor = m_args['anchor'] if m_args['hosts']: hostnames = m_args['hosts'] elif m_args['host']: hostnames = [m_args['host']] else: # return the whole graph hostnames = [] try: # When called by pytest, the file path is obscured to /tmp/.../. # we need the caller to tell us where the graph files are with # filepath argument. if m_args['filepath']: global LAB_GRAPHFILE_PATH LAB_GRAPHFILE_PATH = m_args['filepath'] if m_args['filename']: filename = os.path.join(LAB_GRAPHFILE_PATH, m_args['filename']) lab_graph = Parse_Lab_Graph(filename) lab_graph.parse_graph() else: # When calling passed in anchor instead of hostnames, # the caller is asking to return the whole graph. This # is needed when configuring the root fanout switch. target = anchor if anchor else hostnames lab_graph = find_graph(target) # early return for the whole graph or empty graph file(vtestbed) if (not hostnames or not lab_graph.devices and not lab_graph.links and not lab_graph.vlanport): results = { 'device_info': lab_graph.devices, 'device_conn': lab_graph.links, 'device_port_vlans': lab_graph.vlanport, } module.exit_json(ansible_facts=results) device_info = {} device_conn = {} device_port_vlans = {} device_vlan_range = {} device_vlan_list = {} device_vlan_map_list = {} device_console_info = {} device_console_link = {} device_pdu_info = {} device_pdu_links = {} for hostname in hostnames: dev = lab_graph.get_host_device_info(hostname) if dev is None: module.fail_json(msg="cannot find info for %s" % hostname) device_info[hostname] = dev device_conn[hostname] = lab_graph.get_host_connections(hostname) host_vlan = lab_graph.get_host_vlan(hostname) port_vlans = lab_graph.get_host_port_vlans(hostname) # for multi-DUTs, must ensure all have vlan configured. if host_vlan: device_vlan_range[hostname] = host_vlan["VlanRange"] device_vlan_list[hostname] = host_vlan["VlanList"] if dev["Type"].lower() != "devsonic": device_vlan_map_list[hostname] = host_vlan["VlanList"] else: device_vlan_map_list[hostname] = {} port_name_list_sorted = get_port_name_list(dev['HwSku']) print_debug_msg( debug_fname, "For %s with hwsku %s, port_name_list is %s" % (hostname, dev['HwSku'], port_name_list_sorted)) for a_host_vlan in host_vlan["VlanList"]: # Get the corresponding port for this vlan from the port vlan list for this hostname found_port_for_vlan = False for a_port in port_vlans: if a_host_vlan in port_vlans[a_port]['vlanlist']: if a_port in port_name_list_sorted: port_index = port_name_list_sorted.index( a_port) device_vlan_map_list[hostname][ port_index] = a_host_vlan found_port_for_vlan = True break else: module.fail_json( msg= "Did not find port for %s in the ports based on hwsku '%s' for host %s" % (a_port, dev['HwSku'], hostname)) if not found_port_for_vlan: module.fail_json( msg= "Did not find corresponding link for vlan %d in %s for host %s" % (a_host_vlan, port_vlans, hostname)) device_port_vlans[hostname] = port_vlans device_console_info[hostname] = lab_graph.get_host_console_info( hostname) device_console_link[hostname] = lab_graph.get_host_console_link( hostname) device_pdu_info[hostname] = lab_graph.get_host_pdu_info(hostname) device_pdu_links[hostname] = lab_graph.get_host_pdu_links(hostname) results = { k: v for k, v in locals().items() if (k.startswith("device_") and v) } module.exit_json(ansible_facts=results) except (IOError, OSError): module.fail_json(msg="Can not find lab graph file under {}".format( LAB_GRAPHFILE_PATH)) except Exception as e: module.fail_json(msg=traceback.format_exc())
def main(): module = AnsibleModule(argument_spec=dict( host=dict(required=False), hosts=dict(required=False, type='list'), filename=dict(required=False), filepath=dict(required=False), anchor=dict(required=False, type='list'), ), mutually_exclusive=[['host', 'hosts', 'anchor']], supports_check_mode=True) m_args = module.params global debug_fname debug_fname = create_debug_file("/tmp/conn_graph_debug.txt") hostnames = m_args['hosts'] anchor = m_args['anchor'] if not hostnames: hostnames = [m_args['host']] try: # When called by pytest, the file path is obscured to /tmp/.../. # we need the caller to tell us where the graph files are with # filepath argument. if m_args['filepath']: global LAB_GRAPHFILE_PATH LAB_GRAPHFILE_PATH = m_args['filepath'] if m_args['filename']: filename = os.path.join(LAB_GRAPHFILE_PATH, m_args['filename']) lab_graph = Parse_Lab_Graph(filename) lab_graph.parse_graph() else: # When calling passed in anchor instead of hostnames, # the caller is asking to return the whole graph. This # is needed when configuring the root fanout switch. target = anchor if anchor else hostnames lab_graph = find_graph(target) device_info = [] device_conn = {} device_port_vlans = [] device_vlan_range = [] device_vlan_list = [] device_vlan_map_list = {} for hostname in hostnames: dev = lab_graph.get_host_device_info(hostname) if dev is None: module.fail_json(msg="cannot find info for %s" % hostname) device_info.append(dev) device_conn.update(lab_graph.get_host_connections(hostname)) host_vlan = lab_graph.get_host_vlan(hostname) # for multi-DUTs, must ensure all have vlan configured. if host_vlan: device_vlan_range.append(host_vlan["VlanRange"]) device_vlan_list.append(host_vlan["VlanList"]) device_vlan_map_list[hostname] = host_vlan["VlanList"] device_port_vlans.append(lab_graph.get_host_port_vlans(hostname)) results = { k: v for k, v in locals().items() if (k.startswith("device_") and v) } # TODO: Currently the results values are heterogeneous, let's change # them all into dictionaries in the future. if m_args['hosts'] is None: results = { k: v[0] if isinstance(v, list) else v for k, v in results.items() } module.exit_json(ansible_facts=results) except (IOError, OSError): module.fail_json(msg="Can not find lab graph file under {}".format( LAB_GRAPHFILE_PATH)) except Exception as e: module.fail_json(msg=traceback.format_exc())