diff --git a/plugins/module_utils/network/vyos/config/bgp_global/bgp_global.py b/plugins/module_utils/network/vyos/config/bgp_global/bgp_global.py index 46dfafcf..f20b95ac 100644 --- a/plugins/module_utils/network/vyos/config/bgp_global/bgp_global.py +++ b/plugins/module_utils/network/vyos/config/bgp_global/bgp_global.py @@ -1,502 +1,503 @@ # # -*- coding: utf-8 -*- # Copyright 2021 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # from __future__ import absolute_import, division, print_function import re __metaclass__ = type """ The vyos_bgp_global config file. It is in this file where the current configuration (as dict) is compared to the provided configuration (as dict) and the command set necessary to bring the current configuration to its desired end-state is created. """ from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import ( ResourceModule, ) from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( dict_merge, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.facts import Facts from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.bgp_global import ( Bgp_globalTemplate, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.bgp_global_14 import ( Bgp_globalTemplate14, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.utils.version import ( LooseVersion, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import get_os_version class Bgp_global(ResourceModule): """ The vyos_bgp_global config class """ def __init__(self, module, vrf=None): super(Bgp_global, self).__init__( empty_fact_val={}, facts_module=Facts(module), module=module, resource="bgp_global", tmplt=Bgp_globalTemplate(), ) self.parsers = [] # the VRF whose BGP instance this is (vyos_vrf), None for the global instance self.vrf = vrf def _validate_template(self): version = get_os_version(self._module) if LooseVersion(version) >= LooseVersion("1.4"): self._tmplt = Bgp_globalTemplate14() else: self._tmplt = Bgp_globalTemplate() def parse(self): """override parse to check template""" self._validate_template() return super().parse() def get_parser(self, name): """get_parsers""" self._validate_template() return super().get_parser(name) def execute_module(self): """Execute the module :rtype: A dictionary :returns: The result from module execution """ self._validate_template() if self.state not in ["parsed", "gathered"]: self.generate_commands() self.run_commands() return self.result def generate_commands(self): """Generate configuration commands to send based on want, have and desired state. """ wantd = {} haved = {} # set here, not in execute_module: vyos_vrf calls generate_commands directly if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): self._asn_mod = "" else: self._asn_mod = " " + str(self.have.get("as_number")) # remote_as is an AS number or "external"/"internal"; numbers are compared as int for neighbor in self.want.get("neighbor", []): if "remote_as" in neighbor: neighbor["remote_as"] = self._remote_as(neighbor["address"], neighbor["remote_as"]) v6only = neighbor.get("interface", {}).get("v6only", {}) if "remote_as" in v6only: v6only["remote_as"] = self._remote_as(neighbor["address"], v6only["remote_as"]) # Options without a CLI path on VyOS 1.4 and later are dropped from want, with a # warning. The version is only checked when one of them is set (no device access # otherwise, e.g. for state rendered). default = self.want.get("bgp_params", {}).get("default", {}) neighbors = [n for n in self.want.get("neighbor", []) if "default_originate" in n] if ("no_ipv4_unicast" in default or neighbors) and LooseVersion( get_os_version(self._module), ) >= LooseVersion("1.4"): # no "parameters default no-ipv4-unicast": FRR always has it if default.pop("no_ipv4_unicast", None) is not None: self._module.warn( "no_ipv4_unicast is ignored: VyOS 1.4 and later always apply it " "(no bgp default ipv4-unicast)", ) if not default: self.want["bgp_params"].pop("default") # default-originate only exists per address family for neighbor in neighbors: neighbor.pop("default_originate") self._module.warn( "neighbor {0}: default_originate is ignored: VyOS 1.4 and later only " "support it per address family, see vyos_bgp_address_family".format( neighbor["address"], ), ) if self.want and self.want.get("as_number") is None and self.state != "deleted": self._module.fail_json( msg="as_number is required: VyOS needs a system-as for every BGP " "instance, including the BGP instance of each VRF", ) if ( self.want.get("as_number") == self.have.get("as_number") or not self.have or LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4") ): if self.want: wantd = {self.want["as_number"]: self.want} if self.have: haved = {self.have["as_number"]: self.have} else: self._module.fail_json(msg="Only one bgp instance is allowed per device") # turn all lists of dicts into dicts prior to merge for entry in wantd, haved: self._bgp_global_list_to_dict(entry) # if state is merged, merge want onto have and then compare if self.state == "merged": wantd = dict_merge(haved, wantd) # if state is deleted, empty out wantd and set haved to wantd if self.state == "purged": h_del = {} for k, v in haved.items(): if k in wantd or not wantd: h_del.update({k: v}) for num, entry in h_del.items(): self.commands.append(self._tmplt.render({"as_number": num}, "router", True)) wantd = {} if self.state == "deleted": self._compare(want={}, have=self.have) wantd = {} for k, want in wantd.items(): self._compare(want=want, have=haved.pop(k, {})) def _compare(self, want, have): """Leverages the base class `compare()` method and populates the list of commands to be run by comparing the `want` and `have` data with the `parsers` defined for the Bgp_global network resource. """ if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): self._compare_asn(want, have) parsers = ["maximum_paths", "timers"] self._compare_neighbor(want, have) self._compare_bgp_params(want, have) for name, entry in want.items(): if name != "as_number": self.compare( parsers=parsers, want={"as_number": want["as_number"], name: entry}, have={ "as_number": want["as_number"], name: have.pop(name, {}), }, ) for name, entry in have.items(): if name != "as_number": self.compare( parsers=parsers, want={}, have={"as_number": have["as_number"], name: entry}, ) # Do the negation first command_set = [] for cmd in self.commands: if cmd not in command_set: if "delete" in cmd: command_set.insert(0, cmd) else: command_set.append(cmd) self.commands = command_set def _compare_neighbor(self, want, have): parsers = [ "neighbor.advertisement_interval", "neighbor.allowas_in", "neighbor.as_override", "neighbor.attribute_unchanged.as_path", "neighbor.attribute_unchanged.med", "neighbor.attribute_unchanged.next_hop", "neighbor.capability_dynamic", "neighbor.capability_orf", "neighbor.default_originate", "neighbor.description", "neighbor.interface.v6only.peer_group", "neighbor.interface.v6only.remote_as", "neighbor.disable_capability_negotiation", "neighbor.disable_connected_check", "neighbor.disable_send_community", "neighbor.distribute_list", "neighbor.ebgp_multihop", "neighbor.filter_list", "neighbor.local_as", "neighbor.maximum_prefix", "neighbor.nexthop_self", "neighbor.override_capability", "neighbor.passive", "neighbor.password", "neighbor.peer_group", "neighbor.peer_group_name", "neighbor.port", "neighbor.prefix_list", "neighbor.remote_as", "neighbor.remove_private_as", "neighbor.route_map", "neighbor.route_reflector_client", "neighbor.route_server_client", "neighbor.shutdown", "neighbor.soft_reconfiguration", "neighbor.strict_capability_match", "neighbor.unsuppress_map", "neighbor.update_source", "neighbor.weight", "neighbor.ttl_security", "neighbor.timers", ] wneigh = want.pop("neighbor", {}) hneigh = have.pop("neighbor", {}) # neighbors and peer groups are keyed by (peer_group, name): see _bgp_global_list_to_dict for key, entry in wneigh.items(): first = len(self.commands) hentry = hneigh.get(key, {}) for k, v in entry.items(): if k == "address": continue # the bare "peer-group " node only for a group without other settings if k == "peer_group" and len(entry) > 2: hentry.pop(k, None) continue if hentry: h = {"address": key[1], k: hentry.pop(k, {})} else: h = {} self.compare( parsers=parsers, want={ "as_number": want["as_number"], "neighbor": {"address": key[1], k: v}, }, have={"as_number": want["as_number"], "neighbor": h}, ) self._rename_peer_group(key, first) for key, entry in hneigh.items(): first = len(self.commands) if key not in wneigh: if self._check_af(key): if key[0]: # vyos_bgp_address_family only manages neighbor address families msg = ( "Remove the address-family settings of peer-group {0} with " "vyos_config before replacing/deleting the peer group: " "vyos_bgp_address_family does not manage peer groups.".format(key[1]) ) else: - msg = "Use the _bgp_address_family module to delete the address_family under neighbor {0}, before replacing/deleting the neighbor.".format( - key[1], + msg = ( + "Use the _bgp_address_family module to delete the address_family under neighbor {0}, " + "before replacing/deleting the neighbor.".format(key[1]) ) self._module.fail_json(msg=msg) self.commands.append( "delete protocols bgp" + self._asn_mod + " neighbor " + key[1], ) else: for k, v in entry.items(): if k == "peer_group": # the group itself, not one of its settings continue self.compare( parsers=parsers, want={}, have={ "as_number": have["as_number"], "neighbor": {"address": key[1], k: v}, }, ) self._rename_peer_group(key, first) def _rename_peer_group(self, key, first): """Peer-group definitions are compared with the neighbor parsers: rename the commands generated for one, from index first on.""" is_peer_group, name = key if not is_peer_group: return neighbor = re.compile(r"^(\S+ protocols bgp )neighbor " + re.escape(name) + r"(?= |$)") self.commands[first:] = [ neighbor.sub(r"\1peer-group " + name, cmd) for cmd in self.commands[first:] ] def _compare_bgp_params(self, want, have): parsers = [ "bgp_params.always_compare_med", "bgp_params.bestpath.as_path", "bgp_params.bestpath.compare_routerid", "bgp_params.bestpath.med", "bgp_params.cluster_id", "bgp_params.confederation", "bgp_params.dampening_half_life", "bgp_params.dampening_max_suppress_time", "bgp_params.dampening_re_use", "bgp_params.dampening_start_suppress_time", "bgp_params.default", "bgp_params.deterministic_med", "bgp_params.disbale_network_import_check", "bgp_params.enforce_first_as", "bgp_params.graceful_restart", "bgp_params.log_neighbor_changes", "bgp_params.no_client_to_client_reflection", "bgp_params.no_fast_external_failover", "bgp_params.routerid", "bgp_params.scan_time", ] wbgp = want.pop("bgp_params", {}) hbgp = have.pop("bgp_params", {}) # The as-path flags (confed, ignore, multipath-relax) are separate leaves in VyOS: # changing as_path has to delete the flag it replaces. w_as_path = wbgp.get("bestpath", {}).get("as_path") h_as_path = hbgp.get("bestpath", {}).get("as_path") if w_as_path and h_as_path and w_as_path != h_as_path: self.addcmd( { "as_number": want["as_number"], "bgp_params": {"bestpath": {"as_path": h_as_path}}, }, "bgp_params.bestpath.as_path", True, ) for name, entry in wbgp.items(): if name == "confederation": if entry != hbgp.pop(name, {}): self.addcmd( { "as_number": want["as_number"], "bgp_params": {name: entry}, }, "bgp_params.confederation", False, ) elif name == "distance": if entry != hbgp.pop(name, {}): distance_parsers = [ "bgp_params.distance.global", "bgp_params.distance.prefix", ] for distance_type in entry: self.compare( parsers=distance_parsers, want={ "as_number": want["as_number"], "bgp_params": {name: distance_type}, }, have={ "as_number": want["as_number"], "bgp_params": {name: hbgp.pop(name, {})}, }, ) else: self.compare( parsers=parsers, want={ "as_number": want["as_number"], "bgp_params": {name: entry}, }, have={ "as_number": want["as_number"], "bgp_params": {name: hbgp.pop(name, {})}, }, ) if not wbgp and hbgp: self.commands.append("delete protocols bgp" + self._asn_mod + " parameters") hbgp = {} for name, entry in hbgp.items(): if name == "confederation": self.commands.append( "delete protocols bgp" + self._asn_mod + " parameters confederation", ) elif name == "distance": distance_parsers = [ "bgp_params.distance.global", "bgp_params.distance.prefix", ] self.compare( parsers=distance_parsers, want={}, have={ "as_number": have["as_number"], "bgp_params": {name: entry[0]}, }, ) else: self.compare( parsers=parsers, want={}, have={ "as_number": have["as_number"], "bgp_params": {name: entry}, }, ) def _bgp_global_list_to_dict(self, entry): for name, proc in entry.items(): if "neighbor" in proc: neigh_dict = {} for entry in proc.get("neighbor", []): # a peer group and a neighbor may have the same name key = (bool(entry.get("peer_group")), entry["address"]) neigh_dict.update({key: entry}) proc["neighbor"] = neigh_dict if "network" in proc: network_dict = {} for entry in proc.get("network", []): network_dict.update({entry["address"]: entry}) proc["network"] = network_dict if "aggregate_address" in proc: agg_dict = {} for entry in proc.get("aggregate_address", []): agg_dict.update({entry["prefix"]: entry}) proc["aggregate_address"] = agg_dict if "redistribute" in proc: redis_dict = {} for entry in proc.get("redistribute", []): redis_dict.update({entry["protocol"]: entry}) proc["redistribute"] = redis_dict def _compare_asn(self, want, have): if want.get("as_number") and not have.get("as_number"): self.commands.append( "set protocols bgp " + "system-as" + " " + str(want.get("as_number")), ) def _check_af(self, key): if not self._connection: return False is_peer_group, name = key scope = "vrf name {0} ".format(self.vrf) if self.vrf else "" af_line = re.compile( r"^set " + re.escape(scope) + r"protocols bgp (?:\d+ )?" + ("peer-group " if is_peer_group else "neighbor ") + re.escape(name) + r" address-family\b", ) return any(af_line.match(line) for line in self._get_config(self._connection).splitlines()) def _get_config(self, connection): return connection.get('show configuration commands | match "protocols bgp"') def _remote_as(self, address, value): if str(value) in ("external", "internal"): return str(value) if str(value).isdigit() and 1 <= int(value) <= 4294967294: return int(value) self._module.fail_json( msg="neighbor {0}: remote_as must be an AS number (1-4294967294), " "'external' or 'internal', got {1!r}".format(address, value), )