def main(): argument_spec = dict( vrf=dict(required=True), afi=dict(required=True, choices=['ipv4', 'ipv6']), route_target_both_auto_evpn=dict(required=False, type='bool'), state=dict(choices=['present', 'absent'], default='present'), safi=dict(choices=['unicast', 'multicast'], removed_in_version="2.4"), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False, 'warnings': warnings} config_text = get_config(module) config = NetworkConfig(indent=2, contents=config_text) path = ['vrf context %s' % module.params['vrf'], 'address-family %s unicast' % module.params['afi']] try: current = config.get_block_config(path) except ValueError: current = None commands = list() if current and module.params['state'] == 'absent': commands.append('no address-family %s unicast' % module.params['afi']) elif module.params['state'] == 'present': if current: have = 'route-target both auto evpn' in current if module.params['route_target_both_auto_evpn'] is not None: want = bool(module.params['route_target_both_auto_evpn']) if want and not have: commands.append('address-family %s unicast' % module.params['afi']) commands.append('route-target both auto evpn') elif have and not want: commands.append('address-family %s unicast' % module.params['afi']) commands.append('no route-target both auto evpn') else: commands.append('address-family %s unicast' % module.params['afi']) if module.params['route_target_both_auto_evpn']: commands.append('route-target both auto evpn') if commands: commands.insert(0, 'vrf context %s' % module.params['vrf']) if not module.check_mode: load_config(module, commands) result['changed'] = True result['commands'] = commands module.exit_json(**result)
def get_config_context(self, config, path, indent=2): if config is not None: netcfg = NetworkConfig(indent=indent, contents=config) try: config = netcfg.get_block_config(to_list(path)) except ValueError: config = None return config
def get_section(self, config, section): if config is not None: netcfg = NetworkConfig(indent=1, contents=config) try: config = netcfg.get_block_config(to_list(section)) except ValueError: config = None return config
def map_config_to_obj(module): config = get_config(module) configobj = NetworkConfig(indent=2, contents=config) vrf_config = {} vrfs = re.findall(r'^vrf context (\S+)$', config, re.M) for vrf in vrfs: config_data = configobj.get_block_config(path=['vrf context %s' % vrf]) vrf_config[vrf] = config_data return { 'hostname': parse_hostname(config), 'domain_lookup': 'no ip domain-lookup' not in config, 'domain_name': parse_domain_name(config, vrf_config), 'domain_search': parse_domain_search(config, vrf_config), 'name_servers': parse_name_servers(config, vrf_config, vrfs), 'system_mtu': parse_system_mtu(config) }
def map_config_to_obj(module): config = get_config(module) configobj = NetworkConfig(indent=2, contents=config) vrf_config = {} vrfs = re.findall(r"^vrf context (\S+)$", config, re.M) for vrf in vrfs: config_data = configobj.get_block_config(path=["vrf context %s" % vrf]) vrf_config[vrf] = config_data return { "hostname": parse_hostname(config), "domain_lookup": "no ip domain-lookup" not in config, "domain_name": parse_domain_name(config, vrf_config), "domain_search": parse_domain_search(config, vrf_config), "name_servers": parse_name_servers(config, vrf_config, vrfs), "system_mtu": parse_system_mtu(config), }
def main(): argument_spec = dict( vrf=dict(required=True), afi=dict(required=True, choices=["ipv4", "ipv6"]), route_target_both_auto_evpn=dict(required=False, type="bool"), state=dict(choices=["present", "absent"], default="present"), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() result = {"changed": False, "warnings": warnings} config_text = get_config(module) config = NetworkConfig(indent=2, contents=config_text) path = [ "vrf context %s" % module.params["vrf"], "address-family %s unicast" % module.params["afi"], ] try: current = config.get_block_config(path) except ValueError: current = None commands = list() if current and module.params["state"] == "absent": commands.append("no address-family %s unicast" % module.params["afi"]) elif module.params["state"] == "present": if current: have = "route-target both auto evpn" in current if module.params["route_target_both_auto_evpn"] is not None: want = bool(module.params["route_target_both_auto_evpn"]) if want and not have: commands.append("address-family %s unicast" % module.params["afi"]) commands.append("route-target both auto evpn") elif have and not want: commands.append("address-family %s unicast" % module.params["afi"]) commands.append("no route-target both auto evpn") else: commands.append("address-family %s unicast" % module.params["afi"]) if module.params["route_target_both_auto_evpn"]: commands.append("route-target both auto evpn") if commands: commands.insert(0, "vrf context %s" % module.params["vrf"]) if not module.check_mode: load_config(module, commands) result["changed"] = True result["commands"] = commands module.exit_json(**result)
def main(): argument_spec = dict( vrf=dict(required=True), afi=dict(required=True, choices=['ipv4', 'ipv6']), route_target_both_auto_evpn=dict(required=False, type='bool'), state=dict(choices=['present', 'absent'], default='present'), route_targets=dict(type='list', elements='dict', options=dict( rt=dict(type='str'), direction=dict( choices=['import', 'export', 'both'], default='both'), state=dict(choices=['present', 'absent'], default='present'), )), ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() result = {'changed': False, 'warnings': warnings} config_text = get_config(module) config = NetworkConfig(indent=2, contents=config_text) path = [ 'vrf context %s' % module.params['vrf'], 'address-family %s unicast' % module.params['afi'] ] try: current = config.get_block_config(path) except ValueError: current = None commands = list() if current and module.params['state'] == 'absent': commands.append('no address-family %s unicast' % module.params['afi']) elif module.params['state'] == 'present': rt_commands = list() if not current: commands.append('address-family %s unicast' % module.params['afi']) current = '' have_auto_evpn = 'route-target both auto evpn' in current if module.params['route_target_both_auto_evpn'] is not None: want_auto_evpn = bool(module.params['route_target_both_auto_evpn']) if want_auto_evpn and not have_auto_evpn: commands.append('route-target both auto evpn') elif have_auto_evpn and not want_auto_evpn: commands.append('no route-target both auto evpn') if module.params['route_targets'] is not None: for rt in module.params['route_targets']: if rt.get('direction') == 'both' or not rt.get('direction'): rt_commands = match_current_rt(rt, 'import', current, rt_commands) rt_commands = match_current_rt(rt, 'export', current, rt_commands) else: rt_commands = match_current_rt(rt, rt.get('direction'), current, rt_commands) if rt_commands: commands.extend(rt_commands) if commands and current: commands.insert(0, 'address-family %s unicast' % module.params['afi']) if commands: commands.insert(0, 'vrf context %s' % module.params['vrf']) if not module.check_mode: load_config(module, commands) result['changed'] = True result['commands'] = commands module.exit_json(**result)