def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for interfaces :param connection: the device connection :param data: previously collected configuration as lxml ElementTree root instance or valid xml sting :rtype: dictionary :returns: facts """ if not HAS_LXML: self._module.fail_json(msg="lxml is not installed.") if not data: config_filter = """ <configuration> <interfaces/> </configuration> """ data = self.get_config(connection, config_filter=config_filter) if isinstance(data, string_types): data = etree.fromstring( to_bytes(data, errors="surrogate_then_replace")) self._resources = data.xpath("configuration/interfaces/interface") objs = [] for resource in self._resources: if resource is not None: obj = self.render_config(self.generated_spec, resource) if obj: objs.append(obj) facts = {} if objs: facts["l2_interfaces"] = [] params = utils.validate_config(self.argument_spec, {"config": objs}) for cfg in params["config"]: facts["l2_interfaces"].append(utils.remove_empties(cfg)) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """Populate the facts for ospfv3 :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = self.get_device_data(connection) # typically data is populated from the current device configuration # data = connection.get('show running-config | section ^interface') # using mock data instead objs = {} ospfv3 = findall(r"^set protocols ospfv3 (.+)", data, M) if ospfv3: objs = self.render_config(ospfv3) facts = {} params = utils.validate_config(self.argument_spec, {"config": objs}) facts["ospfv3"] = utils.remove_empties(params["config"]) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for firewall_global :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: # typically data is populated from the current device configuration # data = connection.get('show running-config | section ^interface') # using mock data instead data = self.get_device_data(connection) objs = {} firewalls = findall(r'^set firewall .*$', data, M) if firewalls: objs = self.render_config(firewalls) facts = {} params = utils.validate_config(self.argument_spec, {'config': objs}) facts['firewall_global'] = utils.remove_empties(params['config']) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for l3_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get_config() # operate on a collection of resource x objs = [] interface_names = re.findall( r"set interfaces (?:ethernet|bonding|vti|vxlan) (?:\'*)(\S+)(?:\'*)", data, re.M, ) if interface_names: for interface in set(interface_names): intf_regex = r" %s .+$" % interface cfg = re.findall(intf_regex, data, re.M) obj = self.render_config(cfg) obj["name"] = interface.strip("'") if obj: objs.append(obj) ansible_facts["ansible_network_resources"].pop("l3_interfaces", None) facts = {} if objs: facts["l3_interfaces"] = [] params = utils.validate_config(self.argument_spec, {"config": objs}) for cfg in params["config"]: facts["l3_interfaces"].append(utils.remove_empties(cfg)) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """Populate the facts for acl_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = self.get_device_data(connection) # split the config into instances of the resource resource_delim = "interface" find_pattern = r"(?:^|\n)%s.*?(?=(?:^|\n)%s|$)" % ( resource_delim, resource_delim, ) resources = [ p.strip() for p in re.findall(find_pattern, data, re.DOTALL) ] objs = [] for resource in resources: if resource: obj = self.render_config(self.generated_spec, resource) if obj: objs.append(obj) ansible_facts["ansible_network_resources"].pop("acl_interfaces", None) facts = {} if objs: params = utils.validate_config(self.argument_spec, {"config": objs}) facts["acl_interfaces"] = [ utils.remove_empties(cfg) for cfg in params["config"] ] ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for Interfaces network resource :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ facts = {} objs = [] if not data: # typically data is populated from the current device configuration # data = connection.get('show running-config | section ^interface') # using mock data instead data = ("resource rsrc_a\n" " a_bool\n" " a_string choice_b\n" "resource rscrc_b\n" " key is property01 value is value end\n" " an_int 10\n") # parse native config using the Interfaces template interfaces_parser = InterfacesTemplate(lines=data.splitlines()) objs = list(interfaces_parser.parse().values()) ansible_facts['ansible_network_resources'].pop('interfaces', None) params = utils.remove_empties( utils.validate_config(self.argument_spec, {"config": objs}) ) facts['interfaces'] = params['config'] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for acl_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = self.get_device_data(connection) data = data.split('interface') resources = [] for i in range(len(data)): intf = data[i].split('\n') for l in range(1, len(intf)): if not re.search('ip(v6)?( port)? (access-group|traffic-filter)', intf[l]): intf[l] = '' intf = list(filter(None, intf)) resources.append(intf) objs = [] for resource in resources: if resource: obj = self.render_config(self.generated_spec, resource) if obj: objs.append(obj) ansible_facts['ansible_network_resources'].pop('acl_interfaces', None) facts = {} if objs: params = utils.validate_config( self.argument_spec, {'config': objs}) params = utils.remove_empties(params) facts['acl_interfaces'] = params['config'] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for Ospf_interfaces network resource :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ facts = {} objs = [] if not data: data = self.get_config(connection) # parse native config using the Ospf_interfaces template ospf_interfaces_parser = Ospf_interfacesTemplate( lines=data.splitlines() ) objs = list(ospf_interfaces_parser.parse().values()) if objs: for item in objs: item["address_family"] = list(item["address_family"].values()) for af in item["address_family"]: if af.get("processes"): af["processes"] = list(af["processes"].values()) ansible_facts["ansible_network_resources"].pop("ospf_interfaces", None) params = utils.remove_empties( utils.validate_config(self.argument_spec, {"config": objs}) ) facts["ospf_interfaces"] = params.get("config", []) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for guest_virtual_machines :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: requests = [{ "path": "/rest/restconf/data/extreme-virtual-service:virtual-services-config/", "method": "GET" }, { "path": "/rest/restconf/data/extreme-virtual-service:virtual-services-state/", "method": "GET" }] data = send_requests(self._module, requests=requests) objs = [] if data: obj = self.render_config(self.generated_spec, data) if obj: objs.extend(obj) ansible_facts['ansible_network_resources'].pop( 'guest_virtual_machines', None) facts = {} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['guest_virtual_machines'] = params['config'] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for BGP :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = list() if connection: # just for linting purposes, remove pass if not data: data = get_bgp_af_data(self._module, self.af_params_map) vrf_list = [e_bgp_af['vrf_name'] for e_bgp_af in data] self.normalize_af_advertise_prefix(data) self.update_max_paths(data) bgp_redis_data = get_all_bgp_af_redistribute( self._module, vrf_list, self.af_redis_params_map) self.update_redis_data(data, bgp_redis_data) self.update_afis(data) # operate on a collection of resource x for conf in data: if conf: obj = self.render_config(self.generated_spec, conf) # split the config into instances of the resource if obj: objs.append(obj) ansible_facts['ansible_network_resources'].pop('bgp_af', None) facts = {} if objs: params = utils.validate_config( self.argument_spec, {'config': remove_empties_from_list(objs)}) facts['bgp_af'] = params['config'] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """Populate the facts for lag_interfaces :param connection: the device connection :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = [] if not data: data = connection.get("show running-config | section ^interface") objs = self.render_config(self.generated_spec, data, connection) ansible_facts["ansible_network_resources"].pop("lag_interfaces", None) facts = {} if objs: facts["lag_interfaces"] = [] params = utils.validate_config(self.argument_spec, {"config": objs}) for cfg in params["config"]: facts["lag_interfaces"].append(utils.remove_empties(cfg)) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for static_routes :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = [] resources = self.get_device_data(connection, data) objs = self.render_config(self.generated_spec, resources) ansible_facts["ansible_network_resources"].pop("static_routes", None) facts = {} if objs: params = utils.validate_config(self.argument_spec, {"config": objs}) params = utils.remove_empties(params) for c in params["config"]: if c == {"vrf": "default"}: params["config"].remove(c) facts["static_routes"] = params["config"] ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lldp_global :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get('show running-config | include lldp') objs = {} objs = self.render_config(self.generated_spec, data) ansible_facts['ansible_network_resources'].pop('lldp_global', None) facts = {} if objs: params = utils.validate_config( self.argument_spec, {'config': objs}) facts['lldp_global'] = params['config'] facts = utils.remove_empties(facts) ansible_facts['ansible_network_resources'].update((facts)) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lag_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = self.get_run_conf(connection) int_list = self.get_int_brief(connection) # split the config into instances of the resource objs = [] config = data.split("!") for conf in config: obj = self.render_config(self.generated_spec, conf, int_list) if obj: objs.append(obj) # merge list of dictionaries as a dictionary indexed by name merge_dict = {} for obj in objs: if obj["name"] not in merge_dict: merge_dict[obj["name"]] = obj else: merge_dict[obj["name"]]["members"].extend(obj["members"]) objs = list(merge_dict.values()) ansible_facts['ansible_network_resources'].pop('lag_interfaces', None) facts = {} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['lag_interfaces'] = params['config'] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lacp_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected configuration :rtype: dictionary :returns: facts """ if not data: data = connection.get('show running-config | section lacp') # split the config into instances of the resource resource_delim = 'interface' find_pattern = r'(?:^|\n)%s.*?(?=(?:^|\n)%s|$)' % (resource_delim, resource_delim) resources = [ p.strip() for p in re.findall(find_pattern, data, re.DOTALL) ] objs = [] for resource in resources: if resource: obj = self.render_config(self.generated_spec, resource) if obj: objs.append(obj) ansible_facts['ansible_network_resources'].pop('lacp_interfaces', None) facts = {} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['lacp_interfaces'] = [ utils.remove_empties(cfg) for cfg in params['config'] ] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for Bgp_address_family network resource :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ facts = {} objs = [] if not data: data = self.get_config(connection) nb_data = self._flatten_config(data, "neighbor") data = self._flatten_config(nb_data, "vrf") # parse native config using the Bgp_global template bgp_global_parser = Bgp_address_familyTemplate(lines=data.splitlines()) objs = bgp_global_parser.parse() af = objs.get("address_family") if af: self._post_parse(objs) else: objs["address_family"] = [] ansible_facts["ansible_network_resources"].pop( "bgp_address_family", None ) params = utils.remove_empties( utils.validate_config(self.argument_spec, {"config": objs}) ) facts["bgp_address_family"] = params.get("config", {}) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for bfd_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = [] if not data: data = connection.get( "show running-config | section '^interface|^feature bfd'") # Some of the bfd attributes if 'feature bfd' in data.split('\n'): resources = data.split('interface ') resources.pop(0) else: resources = [] for resource in resources: if resource: obj = self.render_config(self.generated_spec, resource) if obj and len(obj.keys()) > 1: objs.append(obj) ansible_facts['ansible_network_resources'].pop('bfd_interfaces', None) facts = {} if objs: facts['bfd_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['bfd_interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for l2_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if connection: # just for linting purposes, remove pass if not data: # typically data is populated from the current device configuration # data = connection.get('show running-config | section ^interface') # using mock data instead data = self.get_all_l2_interfaces() objs = list() for conf in data: if conf: obj = self.render_config(self.generated_spec, conf) # split the config into instances of the resource if obj: objs.append(obj) ansible_facts['ansible_network_resources'].pop('l2_interfaces', None) facts = {} if objs: facts['l2_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['l2_interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for l3_interface :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get( 'show running-config interface l3 | details | nomore | display json' ) objs = [] try: data_dict = json.loads(data)['data'] data_list = data_dict['dmos-base:config']['interface'][ 'dmos-ip-application:l3'] except (ValueError, KeyError): pass else: data_list = data_list if isinstance(data_list, list) else [data_list] for each in data_list: obj = self.render_config(self.generated_spec, each) if obj: objs.append(obj) facts = {} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['l3_interface'] = params['config'] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for Bgp_address_family network resource :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ facts = {} objs = [] if not data: data = self.get_bgp_address_family_data(connection) # parse native config using the Bgp_address_family template bgp_af_parser = Bgp_AddressFamilyTemplate(lines=data.splitlines()) objs = bgp_af_parser.parse() objs = utils.remove_empties(objs) temp_af = [] if objs.get("address_family"): for k, v in iteritems(objs["address_family"]): if k == "__": continue temp_dict = {} temp = [every for every in k.split("_") if every != ""] temp_dict["afi"] = temp.pop(0) if len(temp) > 1: temp_dict["vrf"] = [ each.split(" ")[1] for each in temp if "vrf" in each ][0] temp_dict["safi"] = [ each for each in temp if "vrf" not in each ][0] elif len(temp) == 1: if "vrf" in temp[0]: temp_dict["vrf"] = temp[0].split("vrf ")[1] else: temp_dict["safi"] = temp[0] neighbor = v.get("neighbor") if neighbor: neighbor_list = [] temp_slow_peer = [] temp = {} neighbor_identifier = None for each in neighbor: if ( each.get("address") or each.get("ipv6_address") or each.get("tag") ) != neighbor_identifier: if temp: if temp_slow_peer: temp.update({"slow_peer": temp_slow_peer}) neighbor_list.append(temp) temp = {} neighbor_identifier = ( each.get("address") or each.get("ipv6_address") or each.get("tag") ) if "address" in each: temp["address"] = neighbor_identifier elif "ipv6_address" in each: temp["ipv6_address"] = neighbor_identifier else: temp["tag"] = neighbor_identifier for every in ["address", "ipv6_address", "tag"]: if every in each: each.pop(every) temp.update(each) slow_peer_val = each.get("slow_peer") if slow_peer_val: temp_slow_peer.append(slow_peer_val[0]) if temp: temp.update({"slow_peer": temp_slow_peer}) neighbor_list.append(temp) temp = {} v["neighbor"] = neighbor_list v.update(temp_dict) temp_af.append(v) objs["address_family"] = temp_af objs["address_family"] = sorted( objs["address_family"], key=lambda k, sk="afi": k[sk] ) if objs: ansible_facts["ansible_network_resources"].pop( "bgp_address_family", None ) params = utils.remove_empties( utils.validate_config(self.argument_spec, {"config": objs}) ) facts["bgp_address_family"] = params["config"] ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for Route_maps network resource :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ facts = {} objs = [] if not data: data = self.get_config(connection) resource_delim = "route-map" find_pattern = r"(?:^|\n)%s.*?(?=(?:^|\n)%s|$)" % ( resource_delim, resource_delim, ) resources = [ p.strip() for p in re.findall(find_pattern, data, re.DOTALL) ] # parse native config using the Ospf_interfaces template route_maps_facts = [] # parse native config using the Route_maps template for resource in resources: route_maps_parser = Route_mapsTemplate(lines=resource.splitlines()) objs = route_maps_parser.parse() if objs: dict_update = {} for k, v in iteritems(objs): if k == "entries": e_list = [] match_dict = {} set_dict = {} for el in v: for entry_k, entry_v in iteritems(el): if entry_k == "match": match_dict.update(entry_v) elif entry_k == "set": set_dict.update(entry_v) else: dict_update.update(el) dict_update.update({ "match": match_dict, "set": set_dict }) e_list.append(dict_update) objs.update({"entries": e_list}) route_maps_facts.append(objs) maps = [] r_facts = [] for r_map in route_maps_facts: if r_map["route_map"] in maps: for r_f in r_facts: if r_f["route_map"] == r_map["route_map"]: r_f["entries"].extend(r_map["entries"]) else: maps.append(r_map["route_map"]) r_facts.append(r_map) ansible_facts["ansible_network_resources"].pop("route_maps", None) facts = {"route_maps": []} params = utils.remove_empties( utils.validate_config(self.argument_spec, {"config": r_facts})) if params.get("config"): for cfg in params["config"]: facts["route_maps"].append(utils.remove_empties(cfg)) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for vlans :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = [] mtu_objs = [] remote_objs = [] final_objs = [] if not data: data = self.get_vlans_data(connection) # operate on a collection of resource x config = data.split("\n") # Get individual vlan configs separately vlan_info = "" temp = "" vlan_name = True for conf in config: if len(list(filter(None, conf.split(" ")))) <= 2 and vlan_name: temp = temp + conf if len(list(filter(None, temp.split(" ")))) <= 2: continue if "VLAN Name" in conf: vlan_info = "Name" elif "VLAN Type" in conf: vlan_info = "Type" vlan_name = False elif "Remote SPAN" in conf: vlan_info = "Remote" vlan_name = False elif "VLAN AREHops" in conf or "STEHops" in conf: vlan_info = "Hops" vlan_name = False elif "Primary Secondary" in conf: vlan_info = "Primary" vlan_name = False if temp: conf = temp temp = "" if (conf and " " not in filter(None, conf.split("-")) and not conf.split(" ")[0] == ""): obj = self.render_config(self.generated_spec, conf, vlan_info) if "mtu" in obj: mtu_objs.append(obj) elif "remote_span" in obj: remote_objs = obj elif obj: objs.append(obj) # Appending MTU value to the retrieved dictionary for o, m in zip(objs, mtu_objs): o.update(m) final_objs.append(o) # Appending Remote Span value to related VLAN: if remote_objs: if remote_objs.get("remote_span"): for each in remote_objs.get("remote_span"): for every in final_objs: if each == every.get("vlan_id"): every.update({"remote_span": True}) break facts = {} if final_objs: facts["vlans"] = [] params = utils.validate_config(self.argument_spec, {"config": objs}) for cfg in params["config"]: facts["vlans"].append(utils.remove_empties(cfg)) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for acls :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = self.get_device_data(connection) objs = [] acl_lines = data.splitlines() # We iterate through the data and create a list of ACLs # where each ACL is a dictionary in the following format: # {'afi': 'ipv4', 'name': 'acl_1', 'aces': ['10 permit 172.16.0.0 0.0.255.255', '20 deny 192.168.34.0 0.0.0.255']} if acl_lines: acl, acls = {}, [] for line in acl_lines: if line.startswith('ip'): if acl: acls.append(acl) acl = {'aces': []} acl['afi'], acl['name'] = line.split()[0], line.split()[2] else: acl['aces'].append(line.strip()) acls.append(acl) # Here we group the ACLs based on AFI # { # 'ipv6': [{'aces': ['10 permit ipv6 2000::/12 any'], 'name': 'acl_2'}], # 'ipv4': [{'aces': ['10 permit 172.16.0.0 0.0.255.255', '20 deny 192.168.34.0 0.0.0.255'], 'name': 'acl_1'}, # {'aces': ['20 deny 10.0.0.0/8 log'], 'name': 'acl_3'}] # } grouped_acls = {'ipv4': [], 'ipv6': []} for acl in acls: acl_copy = deepcopy(acl) del acl_copy['afi'] grouped_acls[acl['afi']].append(acl_copy) # Now that we have the ACLs in a fairly structured format, # we pass it on to render_config to convert it to model spec for key, value in iteritems(grouped_acls): obj = self.render_config(self.generated_spec, value) if obj: obj['afi'] = key objs.append(obj) ansible_facts['ansible_network_resources'].pop('acls', None) facts = {} facts['acls'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['acls'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for acls :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = self.get_acl_data(connection) rmmod = NetworkTemplate(lines=data.splitlines(), tmplt=AclsTemplate()) current = rmmod.parse() temp_v4 = [] temp_v6 = [] if current.get("acls"): for k, v in iteritems(current.get("acls")): if v.get("afi") == "ipv4": del v["afi"] temp_v4.append(v) elif v.get("afi") == "ipv6": del v["afi"] temp_v6.append(v) temp_v4 = sorted(temp_v4, key=lambda i: str(i["name"])) temp_v6 = sorted(temp_v6, key=lambda i: str(i["name"])) for each in temp_v4: for each_ace in each.get("aces"): if each["acl_type"] == "standard": each_ace["source"] = each_ace.pop("std_source") if each_ace.get("icmp_igmp_tcp_protocol"): each_ace["protocol_options"] = { each_ace["protocol"]: { each_ace.pop("icmp_igmp_tcp_protocol").replace( "-", "_"): True } } if each_ace.get("std_source") == {}: del each_ace["std_source"] for each in temp_v6: for each_ace in each.get("aces"): if each_ace.get("std_source") == {}: del each_ace["std_source"] if each_ace.get("icmp_igmp_tcp_protocol"): each_ace["protocol_options"] = { each_ace["protocol"]: { each_ace.pop("icmp_igmp_tcp_protocol").replace( "-", "_"): True } } objs = [] if temp_v4: objs.append({"afi": "ipv4", "acls": temp_v4}) if temp_v6: objs.append({"afi": "ipv6", "acls": temp_v6}) # objs['ipv6'] = {'acls': temp_v6} facts = {} if objs: facts["acls"] = [] params = utils.validate_config(self.argument_spec, {"config": objs}) for cfg in params["config"]: facts["acls"].append(utils.remove_empties(cfg)) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def configure_module_api(argspec, module, deepsec_request): if module.params.get("config"): config = {} before = [] after = [] changed = False temp_name = [] for each in module.params["config"]: search_by_name = search_for_imr_by_name(deepsec_request, each["name"]) if search_by_name.get(api_return): each_result = search_by_name[api_return] temp = copy.deepcopy(each_result) for every in temp: every = map_obj_to_params(every, key_transform, api_return) if every["name"] == each["name"]: diff = utils.dict_diff(every, each) if diff: diff = remove_get_keys_from_payload_dict( diff, get_supported_keys) if diff: if each["name"] not in temp_name: after.extend(before) before.append(every) # Check for actual modification and if present fire # the request over that IPR ID each = utils.remove_empties( utils.dict_merge(every, each)) each = remove_get_keys_from_payload_dict( each, get_supported_keys) changed = True utils.validate_config(argspec, {"config": [each]}) payload = map_params_to_obj(each, key_transform) api_request = deepsec_request.post( "{0}/{1}".format(api_object, every["id"]), data=payload, ) if api_request.get("errors"): module.fail_json(msg=api_request["errors"]) elif api_request.get("message"): module.fail_json(msg=api_request["message"]) after.append( map_obj_to_params(api_request, key_transform, api_return)) else: before.append(every) temp_name.append(every["name"]) else: before.append(every) else: changed = True each = remove_get_keys_from_payload_dict( each, get_supported_keys) utils.validate_config(argspec, {"config": [each]}) payload = map_params_to_obj(each, key_transform) api_request = deepsec_request.post("{0}".format(api_object), data=payload) if api_request.get("errors"): module.fail_json(msg=api_request["errors"]) elif api_request.get("message"): module.fail_json(msg=api_request["message"]) after.append( map_obj_to_params(api_request, key_transform, api_return)) config.update({"before": before, "after": after}) module.exit_json(integrity_monitoringrules=config, changed=changed)
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for Prefix_lists network resource :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ facts = {} objs = [] if not data: data = self.get_prefix_list_data(connection) # parse native config using the Prefix_lists template prefix_lists_parser = Prefix_listsTemplate(lines=data.splitlines()) objs = prefix_lists_parser.parse() final_objs = [] temp = {} temp["afi"] = None temp["prefix_lists"] = [] if objs: for k, v in iteritems(objs): temp_prefix_list = {} temp_prefix_list["entries"] = [] if not temp["afi"] or v["afi"] != temp["afi"]: if temp and temp["afi"]: temp["prefix_lists"] = sorted( temp["prefix_lists"], key=lambda k, sk="name": str(k[sk]), ) # additional check for py3.5 if len(final_objs) == 2: for each in final_objs: if v["afi"] == each["afi"]: each["prefix_lists"].extend( temp["prefix_lists"]) else: final_objs.append(copy(temp)) temp["prefix_lists"] = [] temp["afi"] = v["afi"] for each in v["prefix_lists"]: if not temp_prefix_list.get("name"): temp_prefix_list["name"] = each["name"] temp_prefix_list["entries"].append(each["entries"]) temp["prefix_lists"].append(temp_prefix_list) if temp and temp["afi"]: temp["prefix_lists"] = sorted( temp["prefix_lists"], key=lambda k, sk="name": str(k[sk])) # additional check for py3.5 if len(final_objs) == 2: for each in final_objs: if v["afi"] == each["afi"]: each["prefix_lists"].extend(temp["prefix_lists"]) else: final_objs.append(copy(temp)) final_objs = sorted(final_objs, key=lambda k, sk="afi": k[sk]) ansible_facts["ansible_network_resources"].pop( "prefix_lists", None) params = utils.remove_empties( utils.validate_config(self.argument_spec, {"config": final_objs})) facts["prefix_lists"] = params["config"] ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def set_commands(self, want, have): commands = [] have_afi = search_obj_in_list(want["afi"], have, "afi") ip = "" if "v6" in want["afi"]: ip = "ipv6 " else: ip = "ip " if have_afi: if want.get("acls"): for w_acl in want["acls"]: have_acl = search_obj_in_list(w_acl["name"], have_afi["acls"], "name") name = w_acl["name"] flag = 0 ace_commands = [] if have_acl != w_acl: if have_acl: ace_list = [] if w_acl.get("aces") and have_acl.get("aces"): # case 1 --> sequence number not given in want --> new ace # case 2 --> new sequence number in want --> new ace # case 3 --> existing sequence number given --> update rule (only for merged state. # For replaced and overridden, rule is deleted in the state's config) ace_list = [ item for item in w_acl["aces"] if "sequence" not in item.keys() ] # case 1 want_seq = [ item["sequence"] for item in w_acl["aces"] if "sequence" in item.keys() ] have_seq = [ item["sequence"] for item in have_acl["aces"] ] new_seq = list(set(want_seq) - set(have_seq)) common_seq = list( set(want_seq).intersection(set(have_seq))) temp_list = [ item for item in w_acl["aces"] if "sequence" in item.keys() and item["sequence"] in new_seq ] # case 2 ace_list.extend(temp_list) for w in w_acl["aces"]: self.argument_spec = AclsArgs.argument_spec params = utils.validate_config( self.argument_spec, { "config": [{ "afi": want["afi"], "acls": [{ "name": name, "aces": ace_list, }], }] }, ) if ("sequence" in w.keys() and w["sequence"] in common_seq): temp_obj = search_obj_in_list( w["sequence"], have_acl["aces"], "sequence", ) # case 3 if temp_obj != w: for key, val in w.items(): temp_obj[key] = val ace_list.append(temp_obj) if (self._module.params["state"] == "merged"): ace_commands.append( "no " + str(w["sequence"])) # remove existing rule to update it elif w_acl.get("aces"): # 'have' has ACL defined without any ACE ace_list = list(w_acl["aces"]) for w_ace in ace_list: ace_commands.append( self.process_ace(w_ace).strip()) flag = 1 if flag: ace_commands.insert(0, ip + "access-list " + name) else: commands.append(ip + "access-list " + name) if "aces" in w_acl.keys(): for w_ace in w_acl["aces"]: commands.append( self.process_ace(w_ace).strip()) commands.extend(ace_commands) else: if want.get("acls"): for w_acl in want["acls"]: name = w_acl["name"] commands.append(ip + "access-list " + name) if "aces" in w_acl.keys(): for w_ace in w_acl["aces"]: commands.append(self.process_ace(w_ace).strip()) return commands
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = self.get_ospfv2_data(connection) end_flag, end_mark, count, v_read = 0, 0, 0, False areas, config_commands = [], [] area_str, process, curr_process = "", "", "" data = data.splitlines() for line in data: if ( line.startswith("router ospf") and curr_process != "" and curr_process != line ): end_mark, count, end_flag, area_str = 0, 0, 0, "" if end_mark == 0 and count == 0 and line.startswith("router ospf"): curr_process = line process = re.sub("\n", "", line) count += 1 config_commands.append(process) else: if line.startswith(" area") or line.startswith(" vrf"): area_str = process + re.sub("\n", "", line) config_commands.append(area_str.replace(" ", " ")) end_flag += 1 elif line.startswith(" virtual-link"): virtual_str = area_str + re.sub("\n", "", line) config_commands.append(virtual_str.replace(" ", " ")) v_read = True elif v_read: if "!" not in line: command = virtual_str.replace(" ", " ") + re.sub( "\n", "", line ) config_commands.append(command.replace(" ", " ")) else: v_read = False elif end_flag > 0 and "!" not in line: command = area_str + re.sub("\n", "", line) config_commands.append(command.replace(" ", " ")) elif "!" in line: end_flag = 0 end_mark += 1 if end_mark == 3: end_mark, count = 0, 0 area_str = "" else: command = process + line command.replace(" ", " ") config_commands.append(re.sub("\n", "", command)) areas.append(re.sub("\n", "", command)) data = config_commands ipv4 = {"processes": []} rmmod = NetworkTemplate(lines=data, tmplt=Ospfv2Template()) current = rmmod.parse() # convert some of the dicts to lists for key, sortv in [("processes", "process_id")]: if key in current and current[key]: current[key] = current[key].values() current[key] = sorted( current[key], key=lambda k, sk=sortv: k[sk] ) for process in current.get("processes", []): if "areas" in process: process["areas"] = list(process["areas"].values()) process["areas"] = sorted( process["areas"], key=lambda k, sk="area_id": k[sk] ) for area in process["areas"]: if "ranges" in area: area["ranges"] = sorted( area["ranges"], key=lambda k, s="ranges": k[s] ) if "virtual_link" in area: area["virtual_link"] = list( area["virtual_link"].values() ) area["virtual_link"] = sorted( area["virtual_link"], key=lambda k, sk="id": k[sk] ) ipv4["processes"].append(process) ansible_facts["ansible_network_resources"].pop("ospfv2", None) facts = {} if current: params = utils.validate_config( self.argument_spec, {"config": ipv4} ) params = utils.remove_empties(params) facts["ospfv2"] = params["config"] ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """Populate the facts for Bgp_address_family network resource :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ facts = {} objs = [] if not data: data = self.get_config(connection) data = self._flatten_config(data) # parse native config using the Bgp_address_family template bgp_address_family_parser = Bgp_address_familyTemplate( lines=data.splitlines()) objs = bgp_address_family_parser.parse() if objs: nbr = [] if "address_family" in objs: # remove neighbor AF entries for k, v in iteritems(objs["address_family"]): if not k.startswith("nbr_"): nbr.append(k) for x in nbr: del objs["address_family"][x] objs["address_family"] = list(objs["address_family"].values()) # sort list of dictionaries for x in objs["address_family"]: if "aggregate_address" in x: x["aggregate_address"] = sorted( x["aggregate_address"], key=lambda k, s="prefix": k[s], ) if "networks" in x: x["networks"] = sorted(x["networks"], key=lambda k, s="prefix": k[s]) if "redistribute" in x: x["redistribute"] = sorted( x["redistribute"], key=lambda k: (k.get("id", -1), k["protocol"]), ) objs["address_family"] = sorted( objs["address_family"], key=lambda k: ( k.get("afi", ""), k.get("safi", ""), k.get("vrf", ""), ), ) ansible_facts["ansible_network_resources"].pop("bgp_address_family", None) params = utils.remove_empties( utils.validate_config(self.argument_spec, {"config": objs})) facts["bgp_address_family"] = params.get("config", {}) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts
def set_commands(self, want, have): commands = [] have_afi = search_obj_in_list(want['afi'], have, 'afi') ip = '' if 'v6' in want['afi']: ip = 'ipv6 ' else: ip = 'ip ' if have_afi: if want.get('acls'): for w_acl in want['acls']: have_acl = search_obj_in_list(w_acl['name'], have_afi['acls'], 'name') name = w_acl['name'] flag = 0 ace_commands = [] if have_acl != w_acl: if have_acl: ace_list = [] if w_acl.get('aces') and have_acl.get('aces'): # case 1 --> sequence number not given in want --> new ace # case 2 --> new sequence number in want --> new ace # case 3 --> existing sequence number given --> update rule (only for merged state. # For replaced and overridden, rule is deleted in the state's config) ace_list = [ item for item in w_acl['aces'] if 'sequence' not in item.keys() ] # case 1 want_seq = [ item['sequence'] for item in w_acl['aces'] if 'sequence' in item.keys() ] have_seq = [ item['sequence'] for item in have_acl['aces'] ] new_seq = list(set(want_seq) - set(have_seq)) common_seq = list( set(want_seq).intersection(set(have_seq))) temp_list = [ item for item in w_acl['aces'] if 'sequence' in item.keys() and item['sequence'] in new_seq ] # case 2 ace_list.extend(temp_list) for w in w_acl['aces']: self.argument_spec = AclsArgs.argument_spec params = utils.validate_config( self.argument_spec, { 'config': [{ 'afi': want['afi'], 'acls': [{ 'name': name, 'aces': ace_list }] }] }) if 'sequence' in w.keys( ) and w['sequence'] in common_seq: temp_obj = search_obj_in_list( w['sequence'], have_acl['aces'], 'sequence') # case 3 if temp_obj != w: for key, val in w.items(): temp_obj[key] = val ace_list.append(temp_obj) if self._module.params[ 'state'] == 'merged': ace_commands.append( 'no ' + str(w['sequence'])) # remove existing rule to update it elif w_acl.get('aces'): # 'have' has ACL defined without any ACE ace_list = [item for item in w_acl['aces']] for w_ace in ace_list: ace_commands.append( self.process_ace(w_ace).strip()) flag = 1 if flag: ace_commands.insert(0, ip + 'access-list ' + name) else: commands.append(ip + 'access-list ' + name) if 'aces' in w_acl.keys(): for w_ace in w_acl['aces']: commands.append( self.process_ace(w_ace).strip()) commands.extend(ace_commands) else: if want.get('acls'): for w_acl in want['acls']: name = w_acl['name'] commands.append(ip + 'access-list ' + name) if 'aces' in w_acl.keys(): for w_ace in w_acl['aces']: commands.append(self.process_ace(w_ace).strip()) return commands