def update_switch_names(module, switch_name): """ Method to update switch names. :param module: The Ansible module to fetch input parameters. :param switch_name: Name to assign to the switch. :return: String describing switch name got modified or not. """ cli = pn_cli(module) cli += ' switch-setup-show format switch-name ' if switch_name == run_cli(module, cli).split()[1]: return ' Switch name is same as hostname! ' else: cli = pn_cli(module) cli += ' switch-setup-modify switch-name ' + switch_name run_cli(module, cli) return ' Updated switch name to match hostname! '
def make_switch_setup_static(module): """ Method to assign static values to different switch setup parameters. :param module: The Ansible module to fetch input parameters. """ dns_ip = module.params['pn_dns_ip'] dns_secondary_ip = module.params['pn_dns_secondary_ip'] domain_name = module.params['pn_domain_name'] ntp_server = module.params['pn_ntp_server'] cli = pn_cli(module) cli += ' switch-setup-modify ' if dns_ip: cli += ' dns-ip ' + dns_ip if dns_secondary_ip: cli += ' dns-secondary-ip ' + dns_secondary_ip if domain_name: cli += ' domain-name ' + domain_name if ntp_server: cli += ' ntp-server ' + ntp_server clicopy = cli if clicopy.split('switch-setup-modify')[1] != ' ': run_cli(module, cli)
def create_vlag(module, switch, name, peer_switch, port, peer_port): """ Method to create virtual link aggregation groups. :param module: The Ansible module to fetch input parameters. :param switch: Name of the local switch. :param name: The name of the vlag to create. :param peer_switch: Name of the peer switch. :param port: Name of the trunk on local switch. :param peer_port: Name of the trunk on peer switch. :return: String describing if vlag got created or if it already exists. """ global CHANGED_FLAG cli = pn_cli(module) clicopy = cli cli += ' switch %s vlag-show format name no-show-headers ' % switch vlag_list = run_cli(module, cli).split() if name not in vlag_list: cli = clicopy cli += ' switch %s vlag-create name %s port %s ' % (switch, name, port) cli += ' peer-switch %s peer-port %s mode active-active' % (peer_switch, peer_port) if 'Success' in run_cli(module, cli): CHANGED_FLAG.append(True) return ' %s: Configured vLag %s \n' % (switch, name)
def add_ospf_redistribute(module, vrouter_names): """ Method to add ospf_redistribute to the vrouters. :param module: The Ansible module to fetch input parameters. :param vrouter_names: List of vrouter names. :return: String describing if ospf-redistribute got added or not. """ global CHANGED_FLAG output = '' cli = pn_cli(module) clicopy = cli for vrouter in vrouter_names: cli = clicopy cli += ' vrouter-modify name %s' % vrouter cli += ' ospf-redistribute static,connected' if 'Success' in run_cli(module, cli): cli = clicopy cli += ' vrouter-show name ' + vrouter cli += ' format location no-show-headers ' switch = run_cli(module, cli).split()[0] output += ' %s: Added ospf_redistribute to %s \n' % (switch, vrouter) CHANGED_FLAG.append(True) return output
def add_ospf_loopback_spine(module, switch, vrouter, ospf_network, ospf_area_id): """ Method to add ospf_neighbor for loopback network for spines. :param module: The Ansible module to fetch input parameters. :param switch: The name of the ansible switch to add neighbor. :param vrouter: The vrouter name to add ospf bfd. :param ospf_network: The network for adding the ospf neighbor. :param ospf_area_id: The area_id for the spines loopback neighbor. :return: String describing if OSPF Neighbor got added or not. """ global CHANGED_FLAG output = '' cli = pn_cli(module) clicopy = cli cli += ' vrouter-ospf-show' cli += ' network %s format switch no-show-headers ' % ospf_network already_added = run_cli(module, cli).split() if vrouter in already_added: pass else: cli = clicopy cli += ' vrouter-ospf-add vrouter-name ' + vrouter cli += ' network %s ospf-area %s' % (ospf_network, ospf_area_id) if 'Success' in run_cli(module, cli): output += ' %s: Added OSPF neighbor %s to %s \n' % ( switch, ospf_network, vrouter) CHANGED_FLAG.append(True) return output
def configure_bgp(module, vrouter_names, dict_bgp_as, bgp_max, bgp_redis): """ Method to add bgp_as, bgp_max_path and bgp_redistribute to the vrouters. :param module: The Ansible module to fetch input parameters. :param dict_bgp_as: Dictionary containing the bgp-as for all the switches. :param vrouter_names: List of vrouter names. :param bgp_max: Maxpath for bgp. :param bgp_redis: Bgp redistribute for bgp. :return: String describing if bgp config got added or not. """ global CHANGED_FLAG output = '' cli = pn_cli(module) clicopy = cli for vrouter in vrouter_names: cli = clicopy cli += ' vrouter-show name ' + vrouter cli += ' format location no-show-headers ' switch = run_cli(module, cli).split()[0] cli = clicopy cli += ' vrouter-modify name %s ' % vrouter cli += ' bgp-as %s ' % dict_bgp_as[switch] cli += ' bgp-max-paths %s ' % bgp_max cli += ' bgp-redistribute %s ' % bgp_redis if 'Success' in run_cli(module, cli): output += ' %s: Added bgp_redistribute %s ' % (switch, bgp_redis) output += 'bgp_as %s bgp_maxpath %s to %s\n' % ( dict_bgp_as[switch], bgp_max, vrouter) CHANGED_FLAG.append(True) return output
def update_fabric_network_to_inband(module): """ Method to update fabric network type to in-band :param module: The Ansible module to fetch input parameters. :return: The output of run_cli() method. """ global CHANGED_FLAG output = '' cli = pn_cli(module) clicopy = cli for switch in (module.params['pn_spine_list'] + module.params['pn_leaf_list']): cli = clicopy cli += ' fabric-info format fabric-network ' fabric_network = run_cli(module, cli).split()[1] if fabric_network != 'in-band': cli = clicopy cli += ' switch ' + switch cli += ' fabric-local-modify fabric-network in-band ' if 'Success' in run_cli(module, cli): CHANGED_FLAG.append(True) output += ' %s: Updated fabric network to in-band \n' % switch return output
def modify_stp(module, modify_flag): """ Method to enable/disable STP (Spanning Tree Protocol) on a switch. :param module: The Ansible module to fetch input parameters. :param modify_flag: Enable/disable flag to set. """ cli = pn_cli(module) cli += ' switch-local stp-show format enable ' current_state = run_cli(module, cli).split()[1] state = 'yes' if modify_flag == 'enable' else 'no' if current_state == state: cli = pn_cli(module) cli += ' switch-local stp-modify %s ' % modify_flag run_cli(module, cli)
def ports_modify_jumbo(module, modify_flag): """ Method to enable/disable Jumbo flag on a switch ports. :param module: The Ansible module to fetch input parameters. :param modify_flag: Enable/disable flag to set. :return: The output of run_cli() method. """ cli = pn_cli(module) clicopy = cli trunk_ports = [] cli += ' switch-local port-show format port,trunk status trunk no-show-headers' cli_out = run_cli(module, cli) if cli_out is None: pass else: cli_out = cli_out.strip().split('\n') for output in cli_out: output = output.strip().split() port, trunk_name = output[0], output[1] trunk_ports.append(port) cli = clicopy cli += 'trunk-modify name %s jumbo ' % trunk_name run_cli(module, cli) cli = clicopy cli += ' switch-local port-config-show format port no-show-headers' ports = run_cli(module, cli).split() ports_to_modify = list(set(ports) - set(trunk_ports)) ports_to_modify = ','.join(ports_to_modify) cli = clicopy cli += ' switch-local port-config-modify port %s %s' \ % (ports_to_modify, modify_flag) return run_cli(module, cli)
def port_modify(module): """ Method to disable the ports which doesnt belong to ztp fabric. :param module: The Ansible module to fetch input parameters. :return: switches with disabled ports """ result = list() cli = pn_cli(module) clicopy = cli if module.params['pn_port_disable'] is True: cli += 'fabric-node-show format name parsable-delim ,' switch_list = list(set(run_cli(module, cli).strip().split())) cli = clicopy cli += 'lldp-show format switch,local-port,sys-name parsable-delim ,' lldp_list = run_cli(module, cli).strip().split('\n') for row in lldp_list: row = row.split(',') if row[2] in switch_list: continue else: cli = clicopy cli += 'switch %s port-config-modify ' % row[0] cli += 'port %s disable ' % row[1] run_cli(module, cli) CHANGED_FLAG.append(True) result.append({ 'switch': row[0], 'output': 'Port %s connected to %s disabled' % (row[1], row[2]) }) return result
def modify_stp(module, modify_flag): """ Method to enable/disable STP (Spanning Tree Protocol) on all switches. :param module: The Ansible module to fetch input parameters. :param modify_flag: Enable/disable flag to set. :return: The output of run_cli() method. """ global CHANGED_FLAG output = '' cli = pn_cli(module) clicopy = cli for switch in module.params['pn_leaf_list']: cli = clicopy cli += ' switch %s stp-show format enable ' % switch current_state = run_cli(module, cli).split()[1] if current_state != 'yes': cli = clicopy cli += ' switch ' + switch cli += ' stp-modify ' + modify_flag if 'Success' in run_cli(module, cli): output += ' %s: STP enabled \n' % switch CHANGED_FLAG.append(True) else: output += ' %s: STP is already enabled \n' % switch return output
def enable_web_api(module): """ Enable web api on a switch. :param module: The Ansible module to fetch input parameters. """ cli = pn_cli(module) cli += ' admin-service-modify web if mgmt ' run_cli(module, cli)
def create_vrouter(module): """ Create a hardware vrouter. :param module: The Ansible module to fetch input parameters. :return: String describing if vrouter got created or not. """ global CHANGED_FLAG output = '' vrrp_id = module.params['pn_vrrp_id'] cli = pn_cli(module) cli += ' fabric-node-show format fab-name no-show-headers ' fabric_name = list(set(run_cli(module, cli).split()))[0] vnet_name = fabric_name + '-global' cli = pn_cli(module) cli += ' vrouter-show format name no-show-headers ' existing_vrouter_names = run_cli(module, cli) if existing_vrouter_names is not None: existing_vrouter_names = existing_vrouter_names.split() for switch in module.params['pn_switch_list']: new_vrouter = False vrouter_name = switch + '-vrouter' if (existing_vrouter_names is not None and vrouter_name not in existing_vrouter_names): new_vrouter = True if new_vrouter or existing_vrouter_names is None: cli = pn_cli(module) cli += ' switch %s ' % switch cli += ' vrouter-create name %s vnet %s ' % (vrouter_name, vnet_name) if vrrp_id: cli += ' hw-vrrp-id %s ' % vrrp_id cli += ' enable router-type hardware ' run_cli(module, cli) CHANGED_FLAG.append(True) output += '%s: Created vrouter with name %s\n' % (switch, vrouter_name) return output
def modify_auto_neg(module): """ Module to enable/disable auto-neg for T2+ platforms. :param module: The Ansible module to fetch input parameters. """ cli = pn_cli(module) cli += ' switch-local bezel-portmap-show format port no-show-headers ' cli = shlex.split(cli) out = module.run_command(cli)[1] all_ports = out.splitlines() all_ports = [port.strip() for port in all_ports] time.sleep(1) cli = pn_cli(module) cli += ' switch-local lldp-show format local-port no-show-headers ' cli = shlex.split(cli) out = module.run_command(cli)[1] lldp_ports = out.splitlines() lldp_ports = [port.strip() for port in lldp_ports] time.sleep(1) idle_ports = list(set(all_ports) ^ set(lldp_ports)) cli = pn_cli(module) cli += ' switch-local port-config-modify port ' + ','.join(idle_ports) cli += ' autoneg ' cli = shlex.split(cli) module.run_command(cli) time.sleep(1) cli = pn_cli(module) cli += ' switch-local lldp-show format local-port no-show-headers ' cli = shlex.split(cli) out = module.run_command(cli)[1] lldp_ports = out.splitlines() lldp_ports = [port.strip() for port in lldp_ports] time.sleep(1) idle_ports = list(set(all_ports) ^ set(lldp_ports)) cli = pn_cli(module) cli += ' switch-local port-config-modify port ' + ','.join(idle_ports) cli += ' no-autoneg ' module.run_command(cli) time.sleep(1) return "Auto-neg Configured"
def assign_leafcluster_ospf_interface(module, dict_area_id): """ Method to create interfaces and add ospf neighbor for leaf cluster. :param module: The Ansible module to fetch input parameters. :param dict_area_id: Dictionary containing area_id of leafs. :return: The output of vrouter_interface_ibgp_add() method. """ output = '' iospf_ip_range = module.params['pn_iospf_ip_range'] spine_list = module.params['pn_spine_list'] leaf_list = module.params['pn_leaf_list'] subnet_count = 0 supernet = 30 cli = pn_cli(module) clicopy = cli address = iospf_ip_range.split('.') static_part = str(address[0]) + '.' + str(address[1]) + '.' static_part += str(address[2]) + '.' cli += ' cluster-show format name no-show-headers ' cluster_list = run_cli(module, cli).split() if len(cluster_list) > 0 and cluster_list[0] != 'Success': for cluster in cluster_list: cli = clicopy cli += ' cluster-show name %s format cluster-node-1' % cluster cli += ' no-show-headers' cluster_node_1 = run_cli(module, cli).split()[0] if cluster_node_1 not in spine_list and cluster_node_1 in leaf_list: ip_count = subnet_count * 4 ip1 = static_part + str(ip_count + 1) + '/' + str(supernet) ip2 = static_part + str(ip_count + 2) + '/' + str(supernet) ospf_network = static_part + str(ip_count) + '/' + str( supernet) cli = clicopy cli += ' cluster-show name %s format cluster-node-2' % cluster cli += ' no-show-headers' cluster_node_2 = run_cli(module, cli).split()[0] ospf_area_id = dict_area_id[cluster_node_1] output += vrouter_leafcluster_ospf_add(module, cluster_node_1, ip1, ospf_network, ospf_area_id) output += vrouter_leafcluster_ospf_add(module, cluster_node_2, ip2, ospf_network, ospf_area_id) subnet_count += 1 else: output += ' No leaf clusters present to add iOSPF \n' return output
def ospf_configuration(module): """ Method to configure create interfaces and configure OSPF. :param module: The Ansible module to fetch input parameters. :return: String containing output of all the commands. """ output = '' cli = pn_cli(module) clicopy = cli switch_list = module.params['pn_switch_list'] # Disable auto trunk on all switches. for switch in switch_list: modify_auto_trunk_setting(module, switch, 'disable') ospf_data = module.params['pn_ospf_data'] ospf_data = ospf_data.strip() if ospf_data: ospf_data = ospf_data.replace(' ', '') ospf_data_list = ospf_data.split('\n') for row in ospf_data_list: row = row.strip() if not row or row.startswith('#'): continue else: elements = row.split(',') switch = elements.pop(0).strip() l3_port = elements.pop(0).strip() interface_ip = elements.pop(0).strip() area_id = elements.pop(0).strip() address = interface_ip.split('/') cidr = int(address[1]) address = address[0].split('.') mask = [0, 0, 0, 0] for i in range(cidr): mask[i / 8] += (1 << (7 - i % 8)) # Initialize net and binary and netmask with addr to get network network = [] for i in range(4): network.append(int(address[i]) & mask[i]) ospf_network = '.'.join(map(str, network)) + '/' + str(cidr) cli = clicopy cli += ' vrouter-show location %s ' % switch cli += ' format name no-show-headers ' vrouter_name = run_cli(module, cli).split()[0] delete_trunk(module, switch, l3_port) output += vrouter_interface_ospf_add(module, switch, l3_port, interface_ip, vrouter_name, ospf_network, area_id) return output
def vrouter_interface_ospf_add(module, switch_name, l3_port, interface_ip, vrouter, ospf_network, area_id): """ Method to create interfaces and add ibgp neighbors. :param module: The Ansible module to fetch input parameters. :param switch_name: The name of the switch to run interface. :param l3_port: The l3_port number to create the vrouter interface. :param interface_ip: Interface ip to create a vrouter interface. :param neighbor_ip: Neighbor_ip for the ibgp neighbor. :param remote_as: Bgp-as for remote switch. :return: String describing if ibgp neighbours got added or already exists. """ global CHANGED_FLAG output = '' cli = pn_cli(module) clicopy = cli cli = clicopy cli += ' vrouter-show location %s format name' % switch_name cli += ' no-show-headers' vrouter = run_cli(module, cli).split()[0] cli = clicopy cli += ' vrouter-interface-show ip %s l3-port %s' % (interface_ip, l3_port) cli += ' format switch no-show-headers' existing_vrouter_interface = run_cli(module, cli).split() if vrouter not in existing_vrouter_interface: cli = clicopy cli += ' vrouter-interface-add vrouter-name %s ip %s l3-port %s ' % ( vrouter, interface_ip, l3_port ) run_cli(module, cli) output += '%s: Added vrouter interface with ip %s on %s \n' % ( switch_name, interface_ip, vrouter ) CHANGED_FLAG.append(True) cli = clicopy cli += ' vrouter-ospf-show ' cli += ' network %s format switch no-show-headers' % ospf_network already_added = run_cli(module, cli).split() if vrouter not in already_added: cli = clicopy cli += ' vrouter-ospf-add vrouter-name ' + vrouter cli += ' network %s ospf-area %s' % (ospf_network, area_id) if 'Success' in run_cli(module, cli): output += '%s: Added ospf neighbor %s for %s \n' % (switch_name, ospf_network, vrouter) CHANGED_FLAG.append(True) return output
def configure_control_network(module, network): """ Method to configure the fabric control network. :param module: The Ansible module to fetch input parameters. :param network: It can be in-band or management. :return: The output of run_cli() method. """ cli = pn_cli(module) cli += ' fabric-info format control-network ' current_control_network = run_cli(module, cli).split() if len(current_control_network) == 1: cli = pn_cli(module) cli += ' fabric-local-modify control-network ' + network return run_cli(module, cli) elif current_control_network[1] != network: cli = pn_cli(module) cli += ' fabric-local-modify control-network ' + network return run_cli(module, cli) else: return ' Already configured '
def create_fabric(module, fabric_name): """ Create a fabric :param module: The Ansible module to fetch input parameters. :param fabric_name: Name of the fabric to create. :return: 'Created fabric fabric_name' """ cli = pn_cli(module) cli += ' fabric-create name %s fabric-network mgmt ' % fabric_name run_cli(module, cli) CHANGED_FLAG.append(True) return 'Created fabric {}'.format(fabric_name)
def join_fabric(module, fabric_name): """ Join existing fabric :param module: The Ansible module to fetch input parameters. :param fabric_name: Name of the fabric to join to. :return: 'Joined fabric fabric_name' """ cli = pn_cli(module) cli += ' fabric-join name %s ' % fabric_name run_cli(module, cli) CHANGED_FLAG.append(True) return 'Joined fabric {}'.format(fabric_name)
def modify_stp_local(module, modify_flag): """ Method to enable/disable STP (Spanning Tree Protocol) on a switch. :param module: The Ansible module to fetch input parameters. :param modify_flag: Enable/disable flag to set. :return: The output of run_cli() method. """ cli = pn_cli(module) cli += ' switch-local stp-show format enable ' current_state = run_cli(module, cli).split() if len(current_state) == 1: cli = pn_cli(module) cli += ' switch-local stp-modify ' + modify_flag return run_cli(module, cli) elif current_state[1] == 'yes': cli = pn_cli(module) cli += ' switch-local stp-modify ' + modify_flag return run_cli(module, cli) else: return ' Already modified '
def get_ports(module, switch, peer_switch): """ Method to figure out connected ports between two switches. :param module: The Ansible module to fetch input parameters. :param switch: Name of the local switch. :param peer_switch: Name of the connected peer switch. :return: List of connected ports. """ cli = pn_cli(module) cli += ' switch %s port-show hostname %s' % (switch, peer_switch) cli += ' format port no-show-headers ' return run_cli(module, cli).split()
def vrouter_interface_bgp_add(module, switch_name, l3_port, interface_ip, vrouter, neighbor_ip, remote_as): """ Method to create interfaces and add bgp neighbors. :param module: The Ansible module to fetch input parameters. :param switch_name: The name of the switch to run interface. :param l3_port: The l3-port number to be used to create interface. :param interface_ip: Interface ip to create a vrouter interface. :param neighbor_ip: Neighbor_ip for the ibgp neighbor. :param remote_as: Bgp-as for remote switch. :return: String describing if ibgp neighbours got added or already exists. """ global CHANGED_FLAG output = '' cli = pn_cli(module) clicopy = cli cli = clicopy cli += ' vrouter-interface-show ip %s l3-port %s' % (interface_ip, l3_port) cli += ' format switch no-show-headers' existing_vrouter_interface = run_cli(module, cli).split() if vrouter not in existing_vrouter_interface: cli = clicopy cli += ' vrouter-interface-add vrouter-name %s ip %s l3-port %s ' % ( vrouter, interface_ip, l3_port ) run_cli(module, cli) output += '%s: Added vrouter interface with ip %s on %s \n' % ( switch_name, interface_ip, vrouter ) CHANGED_FLAG.append(True) cli = clicopy cli += ' vrouter-bgp-show remote-as ' + remote_as cli += ' neighbor %s format switch no-show-headers' % neighbor_ip already_added = run_cli(module, cli).split() if vrouter not in already_added: cli = clicopy cli += ' vrouter-bgp-add vrouter-name %s' % vrouter cli += ' neighbor %s remote-as %s' % (neighbor_ip, remote_as) if 'Success' in run_cli(module, cli): output += '%s: Added BGP neighbor %s for %s \n' % (switch_name, neighbor_ip, vrouter) CHANGED_FLAG.append(True) return output
def create_leaf_clusters(module): """ Method to create cluster between two physically connected leaf switches. :param module: The Ansible module to fetch input parameters. :return: Output of create_cluster() method. """ output = '' non_clustered_leafs = find_non_clustered_leafs(module) non_clustered_leafs_count = 0 cli = pn_cli(module) clicopy = cli while non_clustered_leafs_count == 0: if len(non_clustered_leafs) == 0: non_clustered_leafs_count += 1 else: node1 = non_clustered_leafs[0] non_clustered_leafs.remove(node1) cli = clicopy cli += ' switch %s lldp-show ' % node1 cli += ' format sys-name no-show-headers ' system_names = run_cli(module, cli).split() system_names = list(set(system_names)) cli = clicopy cli += ' switch %s fabric-node-show ' % node1 cli += ' format name no-show-headers ' nodes_in_fabric = run_cli(module, cli).split() nodes_in_fabric = list(set(nodes_in_fabric)) for system in system_names: if system not in nodes_in_fabric: system_names.remove(system) terminate_flag = 0 node_count = 0 while (node_count < len(system_names)) and (terminate_flag == 0): node2 = system_names[node_count] if node2 in non_clustered_leafs: # Cluster creation cluster_name = node1 + '-to-' + node2 + '-cluster' output += create_cluster(module, cluster_name, node1, node2) non_clustered_leafs.remove(node2) terminate_flag += 1 node_count += 1 return output
def modify_auto_trunk(module, flag): """ Method to enable/disable auto trunk setting of a switch. :param module: The Ansible module to fetch input parameters. :param flag: Enable/disable flag for the cli command. :return: The output of run_cli() method. """ cli = pn_cli(module) if flag.lower() == 'enable': cli += ' system-settings-modify auto-trunk ' return run_cli(module, cli) elif flag.lower() == 'disable': cli += ' system-settings-modify no-auto-trunk ' return run_cli(module, cli)
def bgp_configuration(module): """ Method to configure create interfaces and configure eBGP. :param module: The Ansible module to fetch input parameters. :return: String containing output of all the commands. """ output = '' cli = pn_cli(module) clicopy = cli switch_list = module.params['pn_switch_list'] # Disable auto trunk on all switches. for switch in switch_list: modify_auto_trunk_setting(module, switch, 'disable') bgp_data = module.params['pn_bgp_data'] bgp_data = bgp_data.strip() if bgp_data: bgp_data = bgp_data.replace(' ', '') bgp_data_list = bgp_data.split('\n') for row in bgp_data_list: row = row.strip() if not row or row.startswith('#'): continue else: elements = row.split(',') switch = elements.pop(0).strip() l3_port = elements.pop(0).strip() interface_ip = elements.pop(0).strip() bgp_as = elements.pop(0).strip() neighbor_ip = elements.pop(0).strip() remote_as = elements.pop(0).strip() cli = clicopy cli += ' vrouter-show location %s ' % switch cli += ' format name no-show-headers ' vrouter_name = run_cli(module, cli).split()[0] cli = clicopy cli += ' vrouter-modify name %s ' % vrouter_name cli += ' bgp-as %s ' % bgp_as if 'Success' in run_cli(module, cli): output += '%s: Added bgp_as %s \n' % (switch, bgp_as) delete_trunk(module, switch, l3_port) output += vrouter_interface_bgp_add(module, switch, l3_port, interface_ip, vrouter_name, neighbor_ip, remote_as) return output
def find_non_clustered_leafs(module): """ Method to find leafs which are not part of any cluster. :param module: The Ansible module to fetch input parameters. :return: List of non clustered leaf switches. """ non_clustered_leafs = [] cli = pn_cli(module) cli += ' cluster-show format cluster-node-1,cluster-node-2 ' cli += ' no-show-headers ' clustered_nodes = run_cli(module, cli).split() for leaf in module.params['pn_leaf_list']: if leaf not in clustered_nodes: non_clustered_leafs.append(leaf) return non_clustered_leafs
def configure_ospf_bfd(module, vrouter, ip): """ Method to add ospf_bfd to the vrouter. :param module: The Ansible module to fetch input parameters. :param vrouter: The vrouter name to add ospf bfd. :param ip: The interface ip to associate the ospf bfd. :return: String describing if OSPF BFD got added or if it already exists. """ global CHANGED_FLAG cli = pn_cli(module) clicopy = cli cli += ' vrouter-interface-show vrouter-name %s' % vrouter cli += ' ip %s format nic no-show-headers ' % ip nic_interface = run_cli(module, cli).split() nic_interface = list(set(nic_interface)) nic_interface.remove(vrouter) cli = clicopy cli += ' vrouter-interface-config-show vrouter-name %s' % vrouter cli += ' nic %s format ospf-bfd no-show-headers ' % nic_interface[0] ospf_status = run_cli(module, cli).split() ospf_status = list(set(ospf_status)) cli = clicopy cli += ' vrouter-show name ' + vrouter cli += ' format location no-show-headers ' switch = run_cli(module, cli).split()[0] if 'Success' in ospf_status: cli = clicopy cli += ' vrouter-interface-config-add vrouter-name %s' % vrouter cli += ' nic %s ospf-bfd enable' % nic_interface[0] if 'Success' in run_cli(module, cli): CHANGED_FLAG.append(True) return ' %s: Added OSPF BFD config to %s \n' % (switch, vrouter) elif 'enable' not in ospf_status: ospf_status.remove(vrouter) cli = clicopy cli += ' vrouter-interface-config-modify vrouter-name %s' % vrouter cli += ' nic %s ospf-bfd enable' % nic_interface[0] if 'Success' in run_cli(module, cli): CHANGED_FLAG.append(True) return ' %s: Enabled OSPF BFD for %s \n' % (switch, vrouter) else: return ''
def assign_inband_ipv4(module): global CHANGED_FLAG switches_list = [] switch_ip = {} spines = module.params['pn_spine_list'] leafs = module.params['pn_leaf_list'] switch = module.params['pn_current_switch'] if module.params['pn_inband_ipv4']: address = module.params['pn_inband_ipv4'].split('.') static_part = str(address[0]) + '.' + str(address[1]) + '.' static_part += str(address[2]) + '.' last_octet = str(address[3]).split('/') subnet = last_octet[1] count = int(last_octet[0]) else: return 'in-band ipv4 not specified ' if spines: switches_list += spines if leafs: switches_list += leafs for sw in switches_list: switch_ip[sw] = static_part + str(count) + '/' + subnet count += 1 # Get existing in-band ip. cli = pn_cli(module) clicopy = cli cli += ' switch-local switch-setup-show format in-band-ip' existing_inband_ip = run_cli(module, cli).split() if switch_ip[switch] not in existing_inband_ip: cli = clicopy cli += ' switch %s switch-setup-modify ' % switch cli += ' in-band-ip ' + switch_ip[switch] run_cli(module, cli) CHANGED_FLAG.append(True) return 'Assigned in-band ip ' + switch_ip[switch]
def is_switches_connected(module): """ Check if switches are physically connected to each other. :param module: The Ansible module to fetch input parameters. :return: True if connected else False. """ cli = pn_cli(module) cli += ' lldp-show format switch,sys-name parsable-delim , ' sys_names = run_cli(module, cli) if sys_names is not None: switch1 = module.params['pn_switch_list'][0] switch2 = module.params['pn_switch_list'][1] sys_names = list(set(sys_names.split())) for cluster in sys_names: if switch1 in cluster and switch2 in cluster: return True return False