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> <chassis> <aggregated-devices> <ethernet> <lacp> </lacp> </ethernet> </aggregated-devices> </chassis> </configuration> """ data = get_resource_config(connection, config_filter=config_filter) if isinstance(data, string_types): data = etree.fromstring( to_bytes(data, errors="surrogate_then_replace")) facts = {} config = deepcopy(self.generated_spec) resources = data.xpath( "configuration/chassis/aggregated-devices/ethernet/lacp") if resources: lacp_root = resources[0] config["system_priority"] = utils.get_xml_conf_arg( lacp_root, "system-priority") if utils.get_xml_conf_arg(lacp_root, "link-protection/non-revertive", data="tag"): config["link_protection"] = "non-revertive" elif utils.get_xml_conf_arg(lacp_root, "link-protection"): config["link_protection"] = "revertive" params = utils.validate_config( self.argument_spec, {"config": utils.remove_empties(config)}) facts["lacp"] = {} facts["lacp"].update(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 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> <protocols> <lldp> </lldp> </protocols> </configuration> """ data = get_resource_config(connection, config_filter=config_filter) if isinstance(data, string_types): data = etree.fromstring( to_bytes(data, errors="surrogate_then_replace") ) facts = {} config = deepcopy(self.generated_spec) resources = data.xpath("configuration/protocols/lldp") if resources: lldp_root = resources[0] config["address"] = utils.get_xml_conf_arg( lldp_root, "management-address" ) config["interval"] = utils.get_xml_conf_arg( lldp_root, "advertisement-interval" ) config["transmit_delay"] = utils.get_xml_conf_arg( lldp_root, "transmit-delay" ) config["hold_multiplier"] = utils.get_xml_conf_arg( lldp_root, "hold-multiplier" ) if utils.get_xml_conf_arg(lldp_root, "disable", data="tag"): config["enable"] = False params = utils.validate_config( self.argument_spec, {"config": utils.remove_empties(config)} ) facts["lldp_global"] = utils.remove_empties(params["config"]) ansible_facts["ansible_network_resources"].update(facts) return ansible_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 ElementTree instance of configuration object :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) config["name"] = utils.get_xml_conf_arg(conf, "name") config["description"] = utils.get_xml_conf_arg(conf, "description") mtu = utils.get_xml_conf_arg(conf, "mtu") config["mtu"] = int(mtu) if mtu else None config["speed"] = utils.get_xml_conf_arg(conf, "speed") config["duplex"] = utils.get_xml_conf_arg(conf, "link-mode") config["hold_time"]["down"] = utils.get_xml_conf_arg( conf, "hold-time/down") config["hold_time"]["up"] = utils.get_xml_conf_arg( conf, "hold-time/up") disable = utils.get_xml_conf_arg(conf, "disable", data="tag") if disable: config["enabled"] = False else: config["enabled"] = 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 ElementTree instance of configuration object :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) config["name"] = utils.get_xml_conf_arg(conf, "name") if utils.get_xml_conf_arg(conf, "disable", data="tag"): config["enabled"] = False 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 ElementTree instance of configuration object :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) config['name'] = utils.get_xml_conf_arg(conf, 'name') config['description'] = utils.get_xml_conf_arg(conf, 'description') mtu = utils.get_xml_conf_arg(conf, 'mtu') config['mtu'] = int(mtu) if mtu else None config['speed'] = utils.get_xml_conf_arg(conf, 'speed') config['duplex'] = utils.get_xml_conf_arg(conf, 'link-mode') config['hold_time']['down'] = utils.get_xml_conf_arg( conf, 'hold-time/down') config['hold_time']['up'] = utils.get_xml_conf_arg( conf, 'hold-time/up') disable = utils.get_xml_conf_arg(conf, 'disable', data='tag') if disable: config['enabled'] = False else: config['enabled'] = 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) config['name'] = utils.get_xml_conf_arg(conf, 'name') config['vlan_id'] = utils.get_xml_conf_arg(conf, 'vlan-id') config['description'] = utils.get_xml_conf_arg(conf, 'description') 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) config["name"] = utils.get_xml_conf_arg(conf, "name") config["vlan_id"] = utils.get_xml_conf_arg(conf, "vlan-id") config["description"] = utils.get_xml_conf_arg(conf, "description") config["l3_interface"] = utils.get_xml_conf_arg(conf, "l3-interface") 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 ElementTree instance of configuration object :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) config["name"] = utils.get_xml_conf_arg(conf, "name") config["period"] = utils.get_xml_conf_arg( conf, "aggregated-ether-options/lacp/periodic") config["sync_reset"] = utils.get_xml_conf_arg( conf, "aggregated-ether-options/lacp/sync-reset") force_up = utils.get_xml_conf_arg( conf, "ether-options/ieee-802.3ad/lacp/force-up", data="tag") if force_up: config["force_up"] = True config["port_priority"] = utils.get_xml_conf_arg( conf, "ether-options/ieee-802.3ad/lacp/port-priority") config["system"]["priority"] = utils.get_xml_conf_arg( conf, "aggregated-ether-options/lacp/system-priority") address = utils.get_xml_conf_arg( conf, "aggregated-ether-options/lacp/system-id") if address: config["system"].update({"mac": {"address": address}}) lacp_intf_cfg = utils.remove_empties(config) # if lacp config is not present for interface return empty dict if len(lacp_intf_cfg) == 1: return {} else: return lacp_intf_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 ElementTree instance of configuration object :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) config['name'] = utils.get_xml_conf_arg(conf, 'name') config['period'] = utils.get_xml_conf_arg( conf, 'aggregated-ether-options/lacp/periodic') config['sync_reset'] = utils.get_xml_conf_arg( conf, 'aggregated-ether-options/lacp/sync-reset') force_up = utils.get_xml_conf_arg( conf, 'ether-options/ieee-802.3ad/lacp/force-up', data='tag') if force_up: config['force_up'] = True config['port_priority'] = utils.get_xml_conf_arg( conf, 'ether-options/ieee-802.3ad/lacp/port-priority') config['system']['priority'] = utils.get_xml_conf_arg( conf, 'aggregated-ether-options/lacp/system-priority') address = utils.get_xml_conf_arg( conf, 'aggregated-ether-options/lacp/system-id') if address: config['system'].update({'mac': {'address': address}}) lacp_intf_cfg = utils.remove_empties(config) # if lacp config is not present for interface return empty dict if len(lacp_intf_cfg) == 1: return {} else: return lacp_intf_cfg
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> <protocols> <lldp> </lldp> </protocols> </configuration> """ data = get_resource_config(connection, config_filter=config_filter) if isinstance(data, string_types): data = etree.fromstring(to_bytes(data, errors='surrogate_then_replace')) facts = {} config = deepcopy(self.generated_spec) resources = data.xpath('configuration/protocols/lldp') if resources: lldp_root = resources[0] config['address'] = utils.get_xml_conf_arg(lldp_root, 'management-address') config['interval'] = utils.get_xml_conf_arg(lldp_root, 'advertisement-interval') config['transmit_delay'] = utils.get_xml_conf_arg(lldp_root, 'transmit-delay') config['hold_multiplier'] = utils.get_xml_conf_arg(lldp_root, 'hold-multiplier') if utils.get_xml_conf_arg(lldp_root, 'disable', data='tag'): config['enable'] = False params = utils.validate_config(self.argument_spec, {'config': utils.remove_empties(config)}) facts['lldp_global'] = utils.remove_empties(params['config']) ansible_facts['ansible_network_resources'].update(facts) return ansible_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 ElementTree instance of configuration object :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) config["name"] = utils.get_xml_conf_arg(conf, "name") config["description"] = utils.get_xml_conf_arg(conf, "description") mtu = utils.get_xml_conf_arg(conf, "mtu") config["mtu"] = int(mtu) if mtu else None config["speed"] = utils.get_xml_conf_arg(conf, "speed") config["duplex"] = utils.get_xml_conf_arg(conf, "link-mode") config["hold_time"]["down"] = utils.get_xml_conf_arg( conf, "hold-time/down" ) config["hold_time"]["up"] = utils.get_xml_conf_arg( conf, "hold-time/up" ) disable = utils.get_xml_conf_arg(conf, "disable", data="tag") if disable: config["enabled"] = False else: config["enabled"] = True cfg = self._get_xml_dict(conf) unit_cfg = cfg.get("interface") if "unit" in unit_cfg.keys(): units = unit_cfg.get("unit") unit_lst = [] unit_dict = {} if isinstance(units, dict): if "description" in units.keys(): unit_dict["name"] = units["name"] unit_dict["description"] = units["description"] unit_lst.append(unit_dict) else: for unit in units: if "description" in unit.keys(): unit_dict["name"] = unit["name"] unit_dict["description"] = unit["description"] unit_lst.append(unit_dict) unit_dict = {} config["units"] = unit_lst 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 ElementTree instance of configuration object :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) enhanced_layer = True mode = utils.get_xml_conf_arg( conf, "unit/family/ethernet-switching/interface-mode" ) if mode is None: mode = utils.get_xml_conf_arg( conf, "unit/family/ethernet-switching/port-mode" ) enhanced_layer = False # Layer 2 is configured on interface if mode: config["name"] = utils.get_xml_conf_arg(conf, "name") unit = utils.get_xml_conf_arg(conf, "unit/name") config["unit"] = unit if unit else 0 config["enhanced_layer"] = enhanced_layer if mode == "access": config["access"] = {} config["access"]["vlan"] = utils.get_xml_conf_arg( conf, "unit/family/ethernet-switching/vlan/members" ) elif mode == "trunk": config["trunk"] = {} vlan_members = conf.xpath( "unit/family/ethernet-switching/vlan/members" ) if vlan_members: config["trunk"]["allowed_vlans"] = [] for vlan_member in vlan_members: config["trunk"]["allowed_vlans"].append( vlan_member.text ) config["trunk"]["native_vlan"] = utils.get_xml_conf_arg( conf, "native-vlan-id" ) 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 ElementTree instance of configuration object :rtype: dictionary :returns: The generated config """ config = deepcopy(spec) intf_name = utils.get_xml_conf_arg(conf, 'name') if intf_name.startswith('ae'): config['name'] = intf_name config['members'] = [] for interface_obj in self._resources: lag_interface_member = utils.get_xml_conf_arg(interface_obj, "ether-options/ieee-802.3ad[bundle='%s']/../../name" % intf_name) if lag_interface_member: member_config = {} member_config['member'] = lag_interface_member if utils.get_xml_conf_arg(interface_obj, "ether-options/ieee-802.3ad/primary", data='tag'): member_config['link_type'] = "primary" elif utils.get_xml_conf_arg(interface_obj, "ether-options/ieee-802.3ad/backup", data='tag'): member_config['link_type'] = "backup" if member_config: config['members'].append(member_config) for m in ['active', 'passive']: if utils.get_xml_conf_arg(conf, "aggregated-ether-options/lacp/%s" % m, data='tag'): config['mode'] = m break link_protection = utils.get_xml_conf_arg(conf, "aggregated-ether-options/link-protection", data='tag') if link_protection: config['link_protection'] = True lag_intf_cfg = utils.remove_empties(config) # if lag interfaces config is not present return empty dict if len(lag_intf_cfg) == 1: return {} else: return lag_intf_cfg