def _clear_config(self, want, have): # Delete the interface config based on the want and have config commands = [] if want.get("name"): interface = "interface " + want["name"] else: interface = "interface " + have["name"] os_version = get_os_version(self._module) if os_version and LooseVersion(os_version) < LooseVersion("7.0.0"): if have.get("native_vlan"): remove_command_from_config_list(interface, "dot1q native vlan", commands) if have.get("q_vlan"): remove_command_from_config_list(interface, "encapsulation dot1q", commands) else: if have.get("encapsulation"): remove_command_from_config_list(interface, "encapsulation dot1q", commands) if have.get("l2protocol") and (want.get("l2protocol") is None or want.get("propagate") is None): if "no l2transport" not in commands: remove_command_from_config_list(interface, "l2transport", commands) elif have.get("l2transport" ) and have.get("l2transport") != want.get("l2transport"): if "no l2transport" not in commands: remove_command_from_config_list(interface, "l2transport", commands) return commands
def _compute_commands(self, key, value, remove=False): """The method generates LAG commands based on the key, value passed. When remove is set to True, the command is negated. :rtype: str :returns: a command based on the `key`, `value` pair passed and the value of `remove` """ if key == "mode": cmd = "lacp mode {0}".format(value) elif key == "load_balancing_hash": os_version = get_os_version(self._module) if os_version and Version(os_version) < Version("7.0.0"): cmd = "bundle load-balancing hash {0}".format(value) elif key == "max_active": cmd = "bundle maximum-active links {0}".format(value) elif key == "min_active": cmd = "bundle minimum-active links {0}".format(value) if remove: cmd = "no {0}".format(cmd) return cmd
def main(): """ main entry point for module execution """ element_spec = dict( dest=dict( type="str", choices=["host", "console", "monitor", "buffered", "file"], ), name=dict(type="str"), size=dict(type="int"), path=dict(type="str"), vrf=dict(type="str", default="default"), facility=dict(type="str", default="local7"), hostnameprefix=dict(type="str"), level=dict( type="str", default="debugging", aliases=["severity"], choices=[ "emergencies", "alerts", "critical", "errors", "warning", "notifications", "informational", "debugging", ], ), state=dict(default="present", choices=["present", "absent"]), ) aggregate_spec = deepcopy(element_spec) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) mutually_exclusive = [("dest", "facility", "hostnameprefix")] required_if = [ ("dest", "host", ["name"]), ("dest", "file", ["name"]), ("dest", "buffered", ["size"]), ("dest", "console", ["level"]), ("dest", "monitor", ["level"]), ] argument_spec = dict(aggregate=dict( type="list", elements="dict", options=aggregate_spec, mutually_exclusive=mutually_exclusive, required_if=required_if, )) argument_spec.update(element_spec) argument_spec.update(iosxr_argument_spec) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, required_if=required_if, supports_check_mode=True, ) config_object = None if is_cliconf(module): config_object = CliConfiguration(module) os_version = get_os_version(module) elif is_netconf(module): config_object = NCConfiguration(module) os_version = (get_capabilities(module).get("device_info").get( "network_os_version")) if config_object: result = config_object.run(os_version) module.exit_json(**result)
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 match.group(1).lower() == "preconfigure": match = re.search(r"^(\S+) (.*)", conf) if match: intf = match.group(2) if get_interface_type(intf) == "unknown": return {} if intf.lower().startswith("gi"): config["name"] = intf # populate the facts from the configuration native_vlan = re.search(r"dot1q native vlan (\d+)", conf) if native_vlan: config["native_vlan"] = int(native_vlan.group(1)) dot1q = utils.parse_conf_arg(conf, "encapsulation dot1q") os_version = get_os_version(self._module) if os_version and Version(os_version) > Version("7.0.0"): encapsulation = re.search( r"encapsulation dot1q\s(\d+)\s*(second-dot1q\s\d+)?", conf, ) if encapsulation: config["encapsulation"]["dot1q"] = int( encapsulation.group(1), ) if encapsulation.group(2): config["encapsulation"]["second_dot1q"] = int( encapsulation.group(2).split("second-dot1q ")[1], ) else: config["q_vlan"] = [] if dot1q: config["q_vlan"].append(int(dot1q.split(" ")[0])) if len(dot1q.split(" ")) > 1: config["q_vlan"].append(int(dot1q.split(" ")[2])) if utils.parse_conf_cmd_arg(conf, "l2transport", True): config["l2transport"] = True if utils.parse_conf_arg(conf, "propagate"): config["propagate"] = True config["l2protocol"] = [] cdp = utils.parse_conf_arg(conf, "l2protocol cdp") pvst = utils.parse_conf_arg(conf, "l2protocol pvst") stp = utils.parse_conf_arg(conf, "l2protocol stp") vtp = utils.parse_conf_arg(conf, "l2protocol vtp") cpsv = utils.parse_conf_arg(conf, "l2protocol cpsv") if cdp: config["l2protocol"].append({"cdp": cdp}) if pvst: config["l2protocol"].append({"pvst": pvst}) if stp: config["l2protocol"].append({"stp": stp}) if vtp: config["l2protocol"].append({"vtp": vtp}) if cpsv: config["l2protocol"].append({"cpsv": cpsv}) return utils.remove_empties(config)
def _set_config(self, want, have, module): # Set the interface config based on the want and have config commands = [] interface = "interface " + want["name"] l2_protocol_bool = False # Get the diff b/w want and have diff = dict_diff(have, want) if diff: # For merging with already configured l2protocol if have.get("l2protocol") and len(have.get("l2protocol")) > 1: l2_protocol_diff = [] for each in want.get("l2protocol"): for every in have.get("l2protocol"): if every == each: break if each not in have.get("l2protocol"): l2_protocol_diff.append(each) l2_protocol_bool = True l2protocol = tuple(l2_protocol_diff) else: l2protocol = {} wants_native = diff.get("native_vlan") l2transport = diff.get("l2transport") q_vlan = diff.get("q_vlan") encapsulation = diff.get("encapsulation") propagate = diff.get("propagate") if l2_protocol_bool is False: l2protocol = diff.get("l2protocol") os_version = get_os_version(self._module) if os_version and LooseVersion(os_version) < LooseVersion("7.0.0"): if wants_native: cmd = "dot1q native vlan {0}".format(wants_native) add_command_to_config_list(interface, cmd, commands) if l2transport or l2protocol: for each in l2protocol: each = dict(each) if (isinstance(each, dict) and list(each.keys())[0] != "cpsv"): cmd = "l2transport l2protocol {0} {1}".format( list(each.keys())[0], list(each.values())[0]) add_command_to_config_list(interface, cmd, commands) if q_vlan and "." in interface: q_vlans = " ".join(map(str, want.get("q_vlan"))) if q_vlans != have.get("q_vlan"): cmd = "dot1q vlan {0}".format(q_vlans) add_command_to_config_list(interface, cmd, commands) else: if l2transport or l2protocol: for each in l2protocol: each = dict(each) if (isinstance(each, dict) and list(each.keys())[0] == "cpsv"): cmd = "l2transport l2protocol {0} {1}".format( list(each.keys())[0], list(each.values())[0]) add_command_to_config_list(interface, cmd, commands) break if encapsulation: encapsulation = dict(encapsulation) if encapsulation.get("dot1q"): if encapsulation.get("second_dot1q"): cmd = "encapsulation dot1q {0} second-dot1q {1}".format( encapsulation.get("dot1q"), encapsulation.get("second_dot1q"), ) else: cmd = "encapsulation dot1q {0}".format( encapsulation.get("dot1q")) add_command_to_config_list(interface, cmd, commands) if l2transport or l2protocol: if propagate and not have.get("propagate"): cmd = "l2transport propagate remote-status" add_command_to_config_list(interface, cmd, commands) elif want.get("l2transport") is False and ( want.get("l2protocol") or want.get("propagate")): module.fail_json( msg= "L2transport L2protocol or Propagate can only be configured when " "L2transport set to True!") return commands