def populate_interfaces(self, interfaces): facts = dict() for key, value in iteritems(interfaces): intf = dict() if get_interface_type(key) == 'svi': intf['state'] = self.parse_state(key, value, intf_type='svi') intf['macaddress'] = self.parse_macaddress(value, intf_type='svi') intf['mtu'] = self.parse_mtu(value, intf_type='svi') intf['bandwidth'] = self.parse_bandwidth(value, intf_type='svi') intf['type'] = self.parse_type(value, intf_type='svi') if 'Internet Address' in value: intf['ipv4'] = self.parse_ipv4_address(value, intf_type='svi') facts[key] = intf else: intf['state'] = self.parse_state(key, value) intf['description'] = self.parse_description(value) intf['macaddress'] = self.parse_macaddress(value) intf['mode'] = self.parse_mode(value) intf['mtu'] = self.parse_mtu(value) intf['bandwidth'] = self.parse_bandwidth(value) intf['duplex'] = self.parse_duplex(value) intf['speed'] = self.parse_speed(value) intf['type'] = self.parse_type(value) if 'Internet Address' in value: intf['ipv4'] = self.parse_ipv4_address(value) facts[key] = intf return facts
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf config['mode'] = utils.parse_conf_arg(conf, 'switchport mode') config['ip_forward'] = utils.parse_conf_arg(conf, 'ip forward') config['access']['vlan'] = utils.parse_conf_arg( conf, 'switchport access vlan') config['trunk']['allowed_vlans'] = utils.parse_conf_arg( conf, 'switchport trunk allowed vlan') config['trunk']['native_vlan'] = utils.parse_conf_arg( conf, 'switchport trunk native vlan') return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r"^ (\S+)", conf) if match is None: return {} intf = match.group(1) if get_interface_type(intf) not in ["management", "ethernet"]: return {} config["name"] = intf if "lldp receive" in conf: # for parsed state only config["receive"] = True if "no lldp receive" in conf: config["receive"] = False if "lldp transmit" in conf: # for parsed state only config["transmit"] = True if "no lldp transmit" in conf: config["transmit"] = False if "management-address" in conf: config["tlv_set"]["management_address"] = re.search( r"management-address (\S*)", conf, ).group(1) if "vlan" in conf: config["tlv_set"]["vlan"] = re.search(r"vlan (\S*)", conf).group(1) return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^ (\S+)', conf) if match is None: return {} intf = match.group(1) if get_interface_type(intf) not in ['management', 'ethernet']: return {} config['name'] = intf if 'lldp receive' in conf: # for parsed state only config['receive'] = True if 'no lldp receive' in conf: config['receive'] = False if 'lldp transmit' in conf: # for parsed state only config['transmit'] = True if 'no lldp transmit' in conf: config['transmit'] = False if 'management-address' in conf: config['tlv_set']['management_address'] = re.search( r'management-address (\S*)', conf).group(1) if 'vlan' in conf: config['tlv_set']['vlan'] = re.search(r'vlan (\S*)', conf).group(1) return utils.remove_empties(config)
def set_config(self, existing_lacp_interfaces_facts): """ Collect the configuration from the args passed to the module, collect the current configuration (as a dict from facts) :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ config = self._module.params.get("config") want = [] if config: for w in config: if get_interface_type(w["name"]) not in ( "portchannel", "ethernet", ): self._module.fail_json( msg= "This module works with either portchannel or ethernet" ) w.update({"name": normalize_interface(w["name"])}) want.append(remove_empties(w)) have = existing_lacp_interfaces_facts resp = self.set_state(want, have) return to_list(resp)
def populate_interfaces(self, interfaces): facts = dict() for key, value in iteritems(interfaces): intf = dict() if get_interface_type(key) == "svi": intf["state"] = self.parse_state(key, value, intf_type="svi") intf["macaddress"] = self.parse_macaddress(value, intf_type="svi") intf["mtu"] = self.parse_mtu(value, intf_type="svi") intf["bandwidth"] = self.parse_bandwidth(value, intf_type="svi") intf["type"] = self.parse_type(value, intf_type="svi") if "Internet Address" in value: intf["ipv4"] = self.parse_ipv4_address(value, intf_type="svi") facts[key] = intf else: intf["state"] = self.parse_state(key, value) intf["description"] = self.parse_description(value) intf["macaddress"] = self.parse_macaddress(value) intf["mode"] = self.parse_mode(value) intf["mtu"] = self.parse_mtu(value) intf["bandwidth"] = self.parse_bandwidth(value) intf["duplex"] = self.parse_duplex(value) intf["speed"] = self.parse_speed(value) intf["type"] = self.parse_type(value) if "Internet Address" in value: intf["ipv4"] = self.parse_ipv4_address(value) facts[key] = intf return facts
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf config['description'] = utils.parse_conf_arg(conf, 'description') config['speed'] = utils.parse_conf_arg(conf, 'speed') config['mtu'] = utils.parse_conf_arg(conf, 'mtu') config['duplex'] = utils.parse_conf_arg(conf, 'duplex') config['mode'] = utils.parse_conf_cmd_arg(conf, 'switchport', 'layer2', 'layer3') config['enabled'] = utils.parse_conf_cmd_arg(conf, 'shutdown', False, True) config['fabric_forwarding_anycast_gateway'] = utils.parse_conf_arg( conf, 'fabric forwarding mode anycast-gateway') config['ip_forward'] = utils.parse_conf_arg(conf, 'ip forward') interfaces_cfg = utils.remove_empties(config) return interfaces_cfg
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf # 'bfd'/'bfd echo' do not nvgen when enabled thus set to 'enable' when None. # 'bfd' is not supported on some platforms config['bfd'] = utils.parse_conf_cmd_arg(conf, 'bfd', 'enable', 'disable') or 'enable' config['echo'] = utils.parse_conf_cmd_arg(conf, 'bfd echo', 'enable', 'disable') or 'enable' return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r"^(\S+)", conf) intf = match.group(1) if get_interface_type(intf) == "unknown": return {} config["name"] = intf config["description"] = utils.parse_conf_arg(conf, "description") config["speed"] = utils.parse_conf_arg(conf, "speed") config["mtu"] = utils.parse_conf_arg(conf, "mtu") config["duplex"] = utils.parse_conf_arg(conf, "duplex") config["mode"] = utils.parse_conf_cmd_arg(conf, "switchport", "layer2", "layer3") config["enabled"] = utils.parse_conf_cmd_arg(conf, "shutdown", False, True) config["fabric_forwarding_anycast_gateway"] = utils.parse_conf_arg( conf, "fabric forwarding mode anycast-gateway") config["ip_forward"] = utils.parse_conf_arg(conf, "ip forward") interfaces_cfg = utils.remove_empties(config) return interfaces_cfg
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r"^(\S+)", conf) intf = match.group(1) if get_interface_type(intf) == "unknown": return {} config["name"] = intf config["mode"] = utils.parse_conf_arg(conf, "switchport mode") config["ip_forward"] = utils.parse_conf_arg(conf, "ip forward") config["access"]["vlan"] = utils.parse_conf_arg( conf, "switchport access vlan" ) config["trunk"]["allowed_vlans"] = utils.parse_conf_arg( conf, "switchport trunk allowed vlan" ) config["trunk"]["native_vlan"] = utils.parse_conf_arg( conf, "switchport trunk native vlan" ) return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r"^(\S+)", conf) intf = match.group(1) if get_interface_type(intf) == "unknown": return {} config["name"] = intf config["dot1q"] = utils.parse_conf_arg(conf, "encapsulation dot1[qQ]") config["redirects"] = utils.parse_conf_cmd_arg(conf, "no ip redirects", False, True) config["unreachables"] = utils.parse_conf_cmd_arg( conf, "ip unreachables", True, False) ipv4_match = re.compile(r"\n ip address (.*)") matches = ipv4_match.findall(conf) if matches: if matches[0]: config["ipv4"] = [] for m in matches: ipv4_conf = m.split() addr = ipv4_conf[0] if addr: config_dict = {"address": addr} if len(ipv4_conf) > 1: d = ipv4_conf[1] if d == "secondary": config_dict.update({"secondary": True}) if len(ipv4_conf) == 4: if ipv4_conf[2] == "tag": config_dict.update( {"tag": int(ipv4_conf[-1])}) elif d == "tag": config_dict.update({"tag": int(ipv4_conf[-1])}) config["ipv4"].append(config_dict) ipv6_match = re.compile(r"\n ipv6 address (.*)") matches = ipv6_match.findall(conf) if matches: if matches[0]: config["ipv6"] = [] for m in matches: ipv6_conf = m.split() addr = ipv6_conf[0] if addr: config_dict = {"address": addr} if len(ipv6_conf) > 1: d = ipv6_conf[1] if d == "tag": config_dict.update({"tag": int(ipv6_conf[-1])}) config["ipv6"].append(config_dict) return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf config['dot1q'] = utils.parse_conf_arg(conf, 'encapsulation dot1[qQ]') config['redirects'] = utils.parse_conf_cmd_arg(conf, 'no ip redirects', False, True) config['unreachables'] = utils.parse_conf_cmd_arg( conf, 'ip unreachables', True, False) ipv4_match = re.compile(r'\n ip address (.*)') matches = ipv4_match.findall(conf) if matches: if matches[0]: config['ipv4'] = [] for m in matches: ipv4_conf = m.split() addr = ipv4_conf[0] if addr: config_dict = {'address': addr} if len(ipv4_conf) > 1: d = ipv4_conf[1] if d == 'secondary': config_dict.update({'secondary': True}) if len(ipv4_conf) == 4: if ipv4_conf[2] == 'tag': config_dict.update( {'tag': int(ipv4_conf[-1])}) elif d == 'tag': config_dict.update({'tag': int(ipv4_conf[-1])}) config['ipv4'].append(config_dict) ipv6_match = re.compile(r'\n ipv6 address (.*)') matches = ipv6_match.findall(conf) if matches: if matches[0]: config['ipv6'] = [] for m in matches: ipv6_conf = m.split() addr = ipv6_conf[0] if addr: config_dict = {'address': addr} if len(ipv6_conf) > 1: d = ipv6_conf[1] if d == 'tag': config_dict.update({'tag': int(ipv6_conf[-1])}) config['ipv6'].append(config_dict) return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf ipv4_match = re.compile(r'\n ip address (.*)') matches = ipv4_match.findall(conf) if matches: if validate_ipv4_addr(matches[0]): config['ipv4'] = [] for m in matches: ipv4_conf = m.split() addr = ipv4_conf[0] ipv4_addr = addr if validate_ipv4_addr(addr) else None if ipv4_addr: config_dict = {'address': ipv4_addr} if len(ipv4_conf) > 1: d = ipv4_conf[1] if d == 'secondary': config_dict.update({'secondary': True}) if len(ipv4_conf) == 4: if ipv4_conf[2] == 'tag': config_dict.update( {'tag': int(ipv4_conf[-1])}) elif d == 'tag': config_dict.update({'tag': int(ipv4_conf[-1])}) config['ipv4'].append(config_dict) ipv6_match = re.compile(r'\n ipv6 address (.*)') matches = ipv6_match.findall(conf) if matches: if validate_ipv6_addr(matches[0]): config['ipv6'] = [] for m in matches: ipv6_conf = m.split() addr = ipv6_conf[0] ipv6_addr = addr if validate_ipv6_addr(addr) else None if ipv6_addr: config_dict = {'address': ipv6_addr} if len(ipv6_conf) > 1: d = ipv6_conf[1] if d == 'tag': config_dict.update({'tag': int(ipv6_conf[-1])}) config['ipv6'].append(config_dict) return utils.remove_empties(config)
def parse_interfaces(self, data): objects = list() for line in data.split('\n'): if len(line) == 0: continue elif line.startswith('admin') or line[0] == ' ': continue else: match = re.match(r'^(\S+)', line) if match: intf = match.group(1) if get_interface_type(intf) != 'unknown': objects.append(intf) return objects
def parse_interfaces(self, data): objects = list() for line in data.split("\n"): if len(line) == 0: continue elif line.startswith("admin") or line[0] == " ": continue else: match = re.match(r"^(\S+)", line) if match: intf = match.group(1) if get_interface_type(intf) != "unknown": objects.append(intf) return objects
def get_members(self, id, connection): """ Returns members associated with a channel-group :param name: The channel group :rtype: list :returns: Members """ members = [] data = connection.get("show port-channel summary") match = re.search(r"{0} (.+)(|\n)".format(id), data) if match: interfaces = re.search(r"Eth\d(.+)$", match.group()) if interfaces: for i in interfaces.group().split(): if get_interface_type(i[:-3]) != "unknown": members.append(normalize_interface(i[:-3])) return members
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf config['bfd'] = utils.parse_conf_cmd_arg(conf, 'hsrp bfd', 'enable', 'disable') return utils.remove_empties(config)
def set_config(self, existing_acl_interfaces_facts): """ Collect the configuration from the args passed to the module, collect the current configuration (as a dict from facts) :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ config = self._module.params['config'] want = [] if config: for w in config: if get_interface_type(w['name']) == 'loopback': self._module.fail_json( msg='This module works with ethernet, management or port-channe') w.update({'name': normalize_interface(w['name'])}) want.append(remove_empties(w)) have = existing_acl_interfaces_facts resp = self.set_state(want, have) return to_list(resp)
def set_config(self, existing_l3_interfaces_facts): """ Collect the configuration from the args passed to the module, collect the current configuration (as a dict from facts) :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ config = self._module.params.get('config') want = [] if config: for w in config: w.update({'name': normalize_interface(w['name'])}) if get_interface_type(w['name']) == 'management': self._module.fail_json( msg= "The 'management' interface is not allowed to be managed by this module" ) want.append(remove_empties(w)) have = existing_l3_interfaces_facts resp = self.set_state(want, have) return to_list(resp)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r'^(\S+)', conf) intf = match.group(1) if get_interface_type(intf) == 'unknown': return {} config['name'] = intf config['port_priority'] = utils.parse_conf_arg(conf, 'lacp port-priority') config['rate'] = utils.parse_conf_arg(conf, 'lacp rate') config['mode'] = utils.parse_conf_arg(conf, 'mode') suspend_individual = re.search(r'no lacp suspend-individual', conf) if suspend_individual: config['suspend_individual'] = False max_links = utils.parse_conf_arg(conf, 'lacp max-bundle') if max_links: config['links']['max'] = max_links min_links = utils.parse_conf_arg(conf, 'lacp min-links') if min_links: config['links']['min'] = min_links graceful = re.search(r'no lacp graceful-convergence', conf) if graceful: config['convergence']['gracefule'] = False vpc = re.search(r'lacp vpc-convergence', conf) if vpc: config['convergence']['vpc'] = True return utils.remove_empties(config)
def render_config(self, spec, conf): """ Render config as dictionary structure and delete keys from spec for null values :param spec: The facts tree, generated from the argspec :param conf: The configuration :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) match = re.search(r"^(\S+)", conf) intf = match.group(1) if get_interface_type(intf) == "unknown": return {} config["name"] = intf config["port_priority"] = utils.parse_conf_arg(conf, "lacp port-priority") config["rate"] = utils.parse_conf_arg(conf, "lacp rate") config["mode"] = utils.parse_conf_arg(conf, "lacp mode") suspend_individual = re.search(r"no lacp suspend-individual", conf) if suspend_individual: config["suspend_individual"] = False max_links = utils.parse_conf_arg(conf, "lacp max-bundle") if max_links: config["links"]["max"] = max_links min_links = utils.parse_conf_arg(conf, "lacp min-links") if min_links: config["links"]["min"] = min_links graceful = re.search(r"no lacp graceful-convergence", conf) if graceful: config["convergence"]["graceful"] = False vpc = re.search(r"lacp vpc-convergence", conf) if vpc: config["convergence"]["vpc"] = True return utils.remove_empties(config)