def set_config(self, existing_l2_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"])}) self.expand_trunk_allowed_vlans(w) want.append(remove_empties(w)) have = existing_l2_interfaces_facts for h in have: self.expand_trunk_allowed_vlans(h) resp = self.set_state(want, have) return to_list(resp)
def populate_neighbors(self, data): objects = dict() # if there are no neighbors the show command returns # ERROR: No neighbour information if data.startswith("ERROR"): return dict() regex = re.compile(r"(\S+)\s+(\S+)\s+\d+\s+\w+\s+(\S+)") for item in data.split("\n")[4:-1]: match = regex.match(item) if match: nbor = dict() nbor["host"] = nbor["sysname"] = match.group(1) nbor["port"] = match.group(3) local_intf = normalize_interface(match.group(2)) if local_intf not in objects: objects[local_intf] = [] objects[local_intf].append(nbor) return objects
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 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) name = conf[0].strip() config["name"] = normalize_interface(name) config["access_groups"] = [] v4 = {"afi": "ipv4", "acls": []} v6 = {"afi": "ipv6", "acls": []} for c in conf[1:]: if c: acl4 = re.search(r"ip( port)? access-group (\w*) (\w*)", c) acl6 = re.search(r"ipv6( port)? traffic-filter (\w*) (\w*)", c) if acl4: acl = { "name": acl4.group(2).strip(), "direction": acl4.group(3).strip(), } if acl4.group(1): acl.update({"port": True}) v4["acls"].append(acl) elif acl6: acl = {"name": acl6.group(2), "direction": acl6.group(3)} if acl6.group(1): acl.update({"port": True}) v6["acls"].append(acl) if len(v4["acls"]) > 0: config["access_groups"].append(v4) if len(v6["acls"]) > 0: config["access_groups"].append(v6) return utils.remove_empties(config)
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 set_config(self, existing_lldp_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']) not in ('management', 'ethernet'): self._module.fail_json( msg= 'This module works with either management or ethernet') w.update({'name': normalize_interface(w['name'])}) want.append(remove_empties(w)) have = existing_lldp_interfaces_facts resp = self.set_state(want, have) return to_list(resp)
def set_config(self, existing_interfaces_facts, default_intf_list=[]): """ 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"])}) want.append(remove_empties(w)) have = deepcopy(existing_interfaces_facts) for i in want: # 'have' does not include objects from the default_interfaces list. # Add any 'want' names from default_interfaces to the 'have' list. if i["name"] in default_intf_list: have.append({"name": i["name"]}) resp = self.set_state(want, have) return to_list(resp)
def set_commands(self, want, have): commands = [] h1 = h2 = h3 = {} want = remove_empties(want) vrf_list = [] if have: vrf_list = [h["vrf"] for h in have] if want["vrf"] in vrf_list and have != [{"vrf": "default"}]: for x in have: if x["vrf"] == want["vrf"]: h1 = x # this has the 'have' dict with same vrf as want if "address_families" in h1.keys(): afi_list = [h["afi"] for h in h1["address_families"]] if "address_families" in want.keys(): for af in want["address_families"]: if af["afi"] in afi_list: for x in h1["address_families"]: if x["afi"] == af["afi"]: h2 = ( x ) # this has the have dict with same vrf and afi as want dest_list = [h["dest"] for h in h2["routes"]] if "routes" in af.keys(): for ro in af["routes"]: if ro["dest"] in dest_list: for x in h2["routes"]: if x["dest"] == ro["dest"]: h3 = ( x ) # this has the have dict with same vrf, afi and dest as want next_hop_list = list(h3["next_hops"]) if "next_hops" in ro.keys(): for nh in ro["next_hops"]: if "interface" in nh.keys(): nh["interface"] = normalize_interface( nh["interface"]) if nh not in next_hop_list: # no match for next hop in have commands = self.set_next_hop( want, h2, nh, ro, commands, ) vrf_list.append( want["vrf"]) else: # no match for dest if "next_hops" in ro.keys(): for nh in ro["next_hops"]: commands = self.set_next_hop( want, h2, nh, ro, commands) else: # no match for afi if "routes" in af.keys(): for ro in af["routes"]: for nh in ro["next_hops"]: commands = self.set_next_hop( want, af, nh, ro, commands) else: # no match for vrf vrf_list.append(want["vrf"]) for af in want["address_families"]: for ro in af["routes"]: for nh in ro["next_hops"]: commands = self.set_next_hop(want, af, nh, ro, commands) return commands