diff --git a/changelogs/fragments/t9431_bgp_address_family_fixes.yml b/changelogs/fragments/t9431_bgp_address_family_fixes.yml new file mode 100644 index 00000000..f9068cf6 --- /dev/null +++ b/changelogs/fragments/t9431_bgp_address_family_fixes.yml @@ -0,0 +1,19 @@ +--- +minor_changes: + - vyos_bgp_address_family - neighbor address families accept ipv4-vpn and ipv6-vpn next to ipv4 + and ipv6 (protocols bgp neighbor
address-family ipv4-vpn|ipv6-vpn), for example for + route-reflector-client and nexthop-self on an L3VPN route reflector. They need VyOS 1.4 or later; + older versions fail with a clear message. +bugfixes: + - vyos_bgp_address_family - a neighbor address family other than unicast or VPN on the device, + such as ipv4-labeled-unicast, no longer makes the module fail; before, it was read as + afi ipv4-labeled, which the argument spec rejected. + - vyos_bgp_address_family - a neighbor address family without options (afi only) activates the + neighbor in that family (protocols bgp neighbor
address-family ); before, it + rendered nothing, so on VyOS 1.4 and later, where FRR always has "no bgp default + ipv4-unicast", such a neighbor carried no routes. With state replaced or overridden, a family + without options on the device that the configuration no longer lists is now deleted. + - vyos_bgp_address_family - neighbor default_originate accepts true for a plain default-originate, + next to a route-map name for default-originate route-map ; before, only the route-map form + existed, true rendered "default-originate route-map True", and a plain default-originate on the + device was not read back. Changing from a route map to the plain form now removes the route map. diff --git a/docs/vyos.vyos.vyos_bgp_address_family_module.rst b/docs/vyos.vyos.vyos_bgp_address_family_module.rst index b82d29cf..5094dd35 100644 --- a/docs/vyos.vyos.vyos_bgp_address_family_module.rst +++ b/docs/vyos.vyos.vyos_bgp_address_family_module.rst @@ -1,2212 +1,2216 @@ .. _vyos.vyos.vyos_bgp_address_family_module: ********************************* vyos.vyos.vyos_bgp_address_family ********************************* **BGP Address Family resource module** Version added: 1.0.0 .. contents:: :local: :depth: 1 Synopsis -------- - This module manages BGP address family configuration of interfaces on devices running VYOS. - Tested against VyOS 1.3.8, 1.4.2, the upcoming 1.5, and the rolling release of spring 2025 - The provided examples of commands are valid for VyOS 1.4+ Parameters ---------- .. raw:: html
Parameter Choices/Defaults Comments
config
dictionary
A dict of BGP global configuration for interfaces.
address_family
list / elements=dictionary
BGP address-family parameters.
afi
string
    Choices:
  • ipv4
  • ipv6
BGP address family settings.
aggregate_address
list / elements=dictionary
BGP aggregate network.
as_set
boolean
    Choices:
  • no
  • yes
Generate AS-set path information for this aggregate address.
prefix
string
BGP aggregate network.
summary_only
boolean
    Choices:
  • no
  • yes
Announce the aggregate summary network only.
networks
list / elements=dictionary
BGP network
backdoor
boolean
    Choices:
  • no
  • yes
Network as a backdoor route.
path_limit
integer
AS path hop count limit
prefix
string
BGP network address
route_map
string
Route-map to modify route attributes
redistribute
list / elements=dictionary
Redistribute routes from other protocols into BGP
metric
integer
Metric for redistributed routes.
protocol
string
    Choices:
  • connected
  • kernel
  • ospf
  • ospfv3
  • rip
  • ripng
  • static
types of routes to be redistributed.
route_map
string
Route map to filter redistributed routes
table
string
Redistribute non-main Kernel Routing Table.
as_number
integer
AS number
neighbors
list / elements=dictionary
BGP neighbor
address_family
list / elements=dictionary
address family.
afi
string
    Choices:
  • ipv4
  • ipv6
  • +
  • ipv4-vpn
  • +
  • ipv6-vpn
-
BGP neighbor parameters.
+
Address family of the neighbor.
+
ipv4 and ipv6 are the unicast address families (ipv4-unicast, ipv6-unicast), ipv4-vpn and ipv6-vpn the L3VPN ones.
allowas_in
integer
Number of occurrences of AS number.
as_override
boolean
    Choices:
  • no
  • yes
AS for routes sent to this neighbor to be the local AS.
attribute_unchanged
dictionary
BGP attributes are sent unchanged.
as_path
boolean
    Choices:
  • no
  • yes
as_path attribute
med
boolean
    Choices:
  • no
  • yes
med attribute
next_hop
boolean
    Choices:
  • no
  • yes
next_hop attribute
capability
dictionary
Advertise capabilities to this neighbor.
dynamic
boolean
    Choices:
  • no
  • yes
Advertise dynamic capability to this neighbor.
orf
string
    Choices:
  • send
  • receive
Advertise ORF capability to this neighbor.
default_originate
- string + raw
-
Send default route to this neighbor
+
Send a default route to this neighbor.
+
true for default-originate, or a route-map name for default-originate route-map <name>, which only originates the default route when the route map matches.
distribute_list
list / elements=dictionary
Access-list to filter route updates to/from this neighbor.
acl
integer
Access-list number.
action
string
    Choices:
  • export
  • import
Access-list to filter outgoing/incoming route updates to this neighbor
filter_list
list / elements=dictionary
As-path-list to filter route updates to/from this neighbor.
action
string
    Choices:
  • export
  • import
filter outgoing/incoming route updates
path_list
string
As-path-list to filter
maximum_prefix
integer
Maximum number of prefixes to accept from this neighbor nexthop-self Nexthop for routes sent to this neighbor to be the local router.
nexthop_local
boolean
    Choices:
  • no
  • yes
Nexthop attributes.
nexthop_self
boolean
    Choices:
  • no
  • yes
Nexthop for routes sent to this neighbor to be the local router.
peer_group
string
IPv4 peer group for this peer
prefix_list
list / elements=dictionary
Prefix-list to filter route updates to/from this neighbor.
action
string
    Choices:
  • export
  • import
filter outgoing/incoming route updates
prefix_list
string
Prefix-list to filter
remove_private_as
boolean
    Choices:
  • no
  • yes
Remove private AS numbers from AS path in outbound route updates
route_map
list / elements=dictionary
Route-map to filter route updates to/from this neighbor.
action
string
    Choices:
  • export
  • import
filter outgoing/incoming route updates
route_map
string
route-map to filter
route_reflector_client
boolean
    Choices:
  • no
  • yes
Neighbor as a route reflector client
route_server_client
boolean
    Choices:
  • no
  • yes
Neighbor is route server client
soft_reconfiguration
boolean
    Choices:
  • no
  • yes
Soft reconfiguration for neighbor
unsupress_map
string
Route-map to selectively unsuppress suppressed routes
weight
integer
Default weight for routes from this neighbor
neighbor_address
string
BGP neighbor address (v4/v6).
running_config
string
This option is used only with state parsed.
The value of this option should be the output received from the VYOS device by executing the command show configuration command | match bgp.
The state parsed reads the configuration from running_config option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the parsed key within the result.
state
string
    Choices:
  • merged ←
  • replaced
  • deleted
  • gathered
  • parsed
  • rendered
  • purged
  • overridden
The state the configuration should be left in.

Examples -------- .. code-block:: yaml # Using merged # Before state # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # vyos@vyos:~$ - name: Merge provided configuration with device configuration vyos.vyos.vyos_bgp_address_family: config: as_number: "100" address_family: - afi: "ipv4" redistribute: - protocol: "static" metric: 50 neighbors: - neighbor_address: "20.33.1.1/24" address_family: - afi: "ipv4" allowas_in: 4 as_override: true attribute_unchanged: med: true - afi: "ipv6" default_originate: "map01" distribute_list: - action: "export" acl: 10 - neighbor_address: "100.11.34.12" address_family: - afi: "ipv4" maximum_prefix: 45 nexthop_self: true route_map: - action: "export" route_map: "map01" - action: "import" route_map: "map01" weight: 50 # After State: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export '10' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight '50' # vyos@vyos:~$ # # Module Execution: # # "after": { # "address_family": [ # { # "afi": "ipv4", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "export", # "route_map": "map01" # }, # { # "action": "import", # "route_map": "map01" # } # ], # "weight": 50 # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "before": {}, # "changed": true, # "commands": [ # "set protocols bgp address-family ipv4-unicast redistribute static metric 50", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number 4", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map map01", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export 10", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix 45", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight 50" # ], # # Using replaced: # Before state: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export '10' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight '50' # vyos@vyos:~$ - name: Replace provided configuration with device configuration vyos.vyos.vyos_bgp_address_family: config: as_number: "100" neighbors: - neighbor_address: "100.11.34.12" address_family: - afi: "ipv4" allowas_in: 4 as_override: true attribute_unchanged: med: true - afi: "ipv6" default_originate: "map01" distribute_list: - action: "export" acl: 10 - neighbor_address: "20.33.1.1/24" address_family: - afi: "ipv6" maximum_prefix: 45 nexthop_self: true state: replaced # After State: # # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast maximum-prefix '45' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast as-override # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast distribute-list export '10' # vyos@vyos:~$ # # # # Module Execution: # "after": { # "address_family": [ # { # "afi": "ipv4", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4" # }, # { # "afi": "ipv6", # "maximum_prefix": 45, # "nexthop_self": true # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "before": { # "address_family": [ # { # "afi": "ipv4", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "export", # "route_map": "map01" # }, # { # "action": "import", # "route_map": "map01" # } # ], # "weight": 50 # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "changed": true, # "commands": [ # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast allowas-in number 4", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast as-override", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast attribute-unchanged med", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast default-originate route-map map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast distribute-list export 10", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast maximum-prefix 45", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast nexthop-self" # ], # Using overridden # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast network 35.1.1.0/24 backdoor # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp address-family ipv6-unicast aggregate-address 6601:1:1:1::/64 summary-only # set protocols bgp address-family ipv6-unicast network 5001:1:1:1::/64 route-map 'map01' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast maximum-prefix '45' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast as-override # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast distribute-list export '10' # vyos@vyos:~$ - name: Override vyos.vyos.vyos_bgp_address_family: config: as_number: "100" neighbors: - neighbor_address: "100.11.34.12" address_family: - afi: "ipv6" maximum_prefix: 45 nexthop_self: true route_map: - action: "import" route_map: "map01" address_family: - afi: "ipv4" aggregate_address: - prefix: "60.9.2.0/24" summary_only: true - afi: "ipv6" redistribute: - protocol: "static" metric: 50 state: overridden # After State # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast aggregate-address 60.9.2.0/24 summary-only # set protocols bgp address-family ipv6-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast route-map import 'map01' # vyos@vyos:~$ # Module Execution: # "after": { # "address_family": [ # { # "afi": "ipv4", # "aggregate_address": [ # { # "prefix": "60.9.2.0/24", # "summary_only": true # } # ] # }, # { # "afi": "ipv6", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4" # }, # { # "afi": "ipv6", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "import", # "route_map": "map01" # } # ] # } # ], # "neighbor_address": "100.11.34.12" # } # ] # }, # "before": { # "address_family": [ # { # "afi": "ipv4", # "networks": [ # { # "backdoor": true, # "prefix": "35.1.1.0/24" # } # ], # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # }, # { # "afi": "ipv6", # "aggregate_address": [ # { # "prefix": "6601:1:1:1::/64", # "summary_only": true # } # ], # "networks": [ # { # "prefix": "5001:1:1:1::/64", # "route_map": "map01" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4" # }, # { # "afi": "ipv6", # "maximum_prefix": 45, # "nexthop_self": true # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "changed": true, # "commands": [ # "delete protocols bgp neighbor 20.33.1.1/24 address-family", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast distribute-list", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast default-originate", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast attribute-unchanged", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast as-override", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast allowas-in", # "delete protocols bgp address-family ipv6 aggregate-address", # "delete protocols bgp address-family ipv6 network", # "delete protocols bgp address-family ipv4 network", # "delete protocols bgp address-family ipv4 redistribute", # "set protocols bgp address-family ipv4-unicast aggregate-address 60.9.2.0/24 summary-only", # "set protocols bgp address-family ipv6-unicast redistribute static metric 50", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast maximum-prefix 45", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast nexthop-self", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast route-map import map01" # ], # # Using deleted: # Before State: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast aggregate-address 60.9.2.0/24 summary-only # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp address-family ipv6-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export '10' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight '50' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast route-map import 'map01' # vyos@vyos:~$ - name: Delete vyos.vyos.vyos_bgp_address_family: config: as_number: "100" neighbors: - neighbor_address: "20.33.1.1/24" address_family: - afi: "ipv6" - neighbor_address: "100.11.34.12" address_family: - afi: "ipv4" state: deleted # After State: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv6-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 100.11.34.12 # vyos@vyos:~$ # # # Module Execution: # # "after": { # "address_family": [ # { # "afi": "ipv6", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "before": { # "address_family": [ # { # "afi": "ipv4", # "aggregate_address": [ # { # "prefix": "60.9.2.0/24", # "summary_only": true # } # ], # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # }, # { # "afi": "ipv6", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "export", # "route_map": "map01" # }, # { # "action": "import", # "route_map": "map01" # } # ], # "weight": 50 # }, # { # "afi": "ipv6", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "import", # "route_map": "map01" # } # ] # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "changed": true, # "commands": [ # "delete protocols bgp address-family ipv4-unicast", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast", # "delete protocols bgp neighbor 100.11.34.12 address-family" # ], # # using parsed: # parsed.cfg # set protocols bgp 65536 address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set # set protocols bgp 65536 address-family ipv4-unicast network 192.1.13.0/24 route-map 'map01' # set protocols bgp 65536 address-family ipv4-unicast network 192.2.13.0/24 backdoor # set protocols bgp 65536 address-family ipv6-unicast redistribute ripng metric '20' # set protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast route-map export 'map01' # set protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound # set protocols bgp 65536 neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop - name: parse configs vyos.vyos.vyos_bgp_address_family: running_config: "{{ lookup('file', './parsed.cfg') }}" state: parsed # Module execution result: # # "parsed": { # "address_family": [ # { # "afi": "ipv4", # "aggregate_address": [ # { # "as_set": true, # "prefix": "192.0.2.0/24" # } # ], # "networks": [ # { # "prefix": "192.1.13.0/24", # "route_map": "map01" # }, # { # "backdoor": true, # "prefix": "192.2.13.0/24" # } # ] # }, # { # "afi": "ipv6", # "redistribute": [ # { # "metric": 20, # "protocol": "ripng" # } # ] # } # ], # "as_number": 65536, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "route_map": [ # { # "action": "export", # "route_map": "map01" # } # ], # "soft_reconfiguration": true # } # ], # "neighbor_address": "192.0.2.25" # }, # { # "address_family": [ # { # "afi": "ipv6", # "attribute_unchanged": { # "next_hop": true # } # } # ], # "neighbor_address": "203.0.113.5" # } # ] # # Using gathered: # Native config: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast network 35.1.1.0/24 backdoor # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp address-family ipv6-unicast aggregate-address 6601:1:1:1::/64 summary-only # set protocols bgp address-family ipv6-unicast network 5001:1:1:1::/64 route-map 'map01' # set protocols bgp address-family ipv6-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 100.11.34.12 - name: gather configs vyos.vyos.vyos_bgp_address_family: state: gathered # Module execution result: # # "gathered": { # "address_family": [ # { # "afi": "ipv4", # "networks": [ # { # "backdoor": true, # "prefix": "35.1.1.0/24" # } # ], # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # }, # { # "afi": "ipv6", # "aggregate_address": [ # { # "prefix": "6601:1:1:1::/64", # "summary_only": true # } # ], # "networks": [ # { # "prefix": "5001:1:1:1::/64", # "route_map": "map01" # } # ], # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # Using rendered: - name: Render vyos.vyos.vyos_bgp_address_family: config: as_number: "100" address_family: - afi: "ipv4" redistribute: - protocol: "static" metric: 50 neighbors: - neighbor_address: "20.33.1.1/24" address_family: - afi: "ipv4" allowas_in: 4 as_override: true attribute_unchanged: med: true - afi: "ipv6" default_originate: "map01" distribute_list: - action: "export" acl: 10 - neighbor_address: "100.11.34.12" address_family: - afi: "ipv4" maximum_prefix: 45 nexthop_self: true route_map: - action: "export" route_map: "map01" - action: "import" route_map: "map01" weight: 50 state: rendered # Module Execution: # "rendered": [ # "set protocols bgp address-family ipv4-unicast redistribute static metric 50", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number 4", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map map01", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export 10", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix 45", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight 50" # ] Return Values ------------- Common return values are documented `here `_, the following are the fields unique to this module: .. raw:: html
Key Returned Description
after
dictionary
when changed
The resulting configuration after module execution.

Sample:
This output will always be in the same format as the module argspec.
before
dictionary
when state is merged, replaced, overridden, deleted or purged
The configuration prior to the module execution.

Sample:
This output will always be in the same format as the module argspec.
commands
list
when state is merged, replaced, overridden, deleted or purged
The set of commands pushed to the remote device.

Sample:
['sample command 1', 'sample command 2', 'sample command 3']
gathered
list
when state is gathered
Facts about the network resource gathered from the remote device as structured data.

Sample:
This output will always be in the same format as the module argspec.
parsed
list
when state is parsed
The device native config provided in running_config option parsed into structured data as per module argspec.

Sample:
This output will always be in the same format as the module argspec.
rendered
list
when state is rendered
The provided configuration in the task rendered in device-native format (offline).

Sample:
['sample command 1', 'sample command 2', 'sample command 3']


Status ------ Authors ~~~~~~~ - Gomathi Selvi Srinivasan (@GomathiselviS) diff --git a/plugins/module_utils/network/vyos/argspec/bgp_address_family/bgp_address_family.py b/plugins/module_utils/network/vyos/argspec/bgp_address_family/bgp_address_family.py index 946f6861..6d8943da 100644 --- a/plugins/module_utils/network/vyos/argspec/bgp_address_family/bgp_address_family.py +++ b/plugins/module_utils/network/vyos/argspec/bgp_address_family/bgp_address_family.py @@ -1,194 +1,195 @@ # -*- coding: utf-8 -*- # Copyright 2024 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 + __metaclass__ = type ############################################# # WARNING # ############################################# # # This file is auto generated by the # cli_rm_builder. # # Manually editing this file is not advised. # # To update the argspec make the desired changes # in the module docstring and re-run # cli_rm_builder. # ############################################# """ The arg spec for the vyos_bgp_address_family module """ class Bgp_address_familyArgs(object): # pylint: disable=R0903 """The arg spec for the vyos_bgp_address_family module""" argument_spec = { "config": { "type": "dict", "options": { "as_number": {"type": "int"}, "address_family": { "type": "list", "elements": "dict", "options": { "afi": {"type": "str", "choices": ["ipv4", "ipv6"]}, "aggregate_address": { "type": "list", "elements": "dict", "options": { "prefix": {"type": "str"}, "as_set": {"type": "bool"}, "summary_only": {"type": "bool"}, }, }, "networks": { "type": "list", "elements": "dict", "options": { "prefix": {"type": "str"}, "path_limit": {"type": "int"}, "backdoor": {"type": "bool"}, "route_map": {"type": "str"}, }, }, "redistribute": { "type": "list", "elements": "dict", "options": { "protocol": { "type": "str", "choices": [ "connected", "kernel", "ospf", "ospfv3", "rip", "ripng", "static", ], }, "table": {"type": "str"}, "route_map": {"type": "str"}, "metric": {"type": "int"}, }, }, }, }, "neighbors": { "type": "list", "elements": "dict", "options": { "neighbor_address": {"type": "str"}, "address_family": { "type": "list", "elements": "dict", "options": { "afi": { "type": "str", - "choices": ["ipv4", "ipv6"], + "choices": ["ipv4", "ipv6", "ipv4-vpn", "ipv6-vpn"], }, "allowas_in": {"type": "int"}, "as_override": {"type": "bool"}, "attribute_unchanged": { "type": "dict", "options": { "as_path": {"type": "bool"}, "med": {"type": "bool"}, "next_hop": {"type": "bool"}, }, }, "capability": { "type": "dict", "options": { "dynamic": {"type": "bool"}, "orf": { "type": "str", "choices": ["send", "receive"], }, }, }, - "default_originate": {"type": "str"}, + "default_originate": {"type": "raw"}, "distribute_list": { "type": "list", "elements": "dict", "options": { "action": { "type": "str", "choices": ["export", "import"], }, "acl": {"type": "int"}, }, }, "filter_list": { "type": "list", "elements": "dict", "options": { "action": { "type": "str", "choices": ["export", "import"], }, "path_list": {"type": "str"}, }, }, "maximum_prefix": {"type": "int"}, "nexthop_local": {"type": "bool"}, "nexthop_self": {"type": "bool"}, "peer_group": {"type": "str"}, "prefix_list": { "type": "list", "elements": "dict", "options": { "action": { "type": "str", "choices": ["export", "import"], }, "prefix_list": {"type": "str"}, }, }, "remove_private_as": {"type": "bool"}, "route_map": { "type": "list", "elements": "dict", "options": { "action": { "type": "str", "choices": ["export", "import"], }, "route_map": {"type": "str"}, }, }, "route_reflector_client": {"type": "bool"}, "route_server_client": {"type": "bool"}, "soft_reconfiguration": {"type": "bool"}, "unsupress_map": {"type": "str"}, "weight": {"type": "int"}, }, }, }, }, }, }, "running_config": {"type": "str"}, "state": { "type": "str", "choices": [ "merged", "replaced", "deleted", "gathered", "parsed", "rendered", "purged", "overridden", ], "default": "merged", }, } # pylint: disable=C0301 diff --git a/plugins/module_utils/network/vyos/config/bgp_address_family/bgp_address_family.py b/plugins/module_utils/network/vyos/config/bgp_address_family/bgp_address_family.py index f1494698..07c86fcc 100644 --- a/plugins/module_utils/network/vyos/config/bgp_address_family/bgp_address_family.py +++ b/plugins/module_utils/network/vyos/config/bgp_address_family/bgp_address_family.py @@ -1,404 +1,468 @@ # # -*- 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 + __metaclass__ = type """ The vyos_bgp_address_family 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. """ import re 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_address_family import ( Bgp_address_familyTemplate, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.bgp_address_family_14 import ( Bgp_address_familyTemplate14, ) 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_address_family(ResourceModule): """ The vyos_bgp_address_family config class """ def __init__(self, module): super(Bgp_address_family, self).__init__( empty_fact_val={}, facts_module=Facts(module), module=module, resource="bgp_address_family", tmplt=Bgp_address_familyTemplate(), ) self.parsers = [] def _validate_template(self): version = get_os_version(self._module) if LooseVersion(version) >= LooseVersion("1.4"): self._tmplt = Bgp_address_familyTemplate14() else: self._tmplt = Bgp_address_familyTemplate() 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 = {} + # The VPN address families (ipv4-vpn, ipv6-vpn) only exist on VyOS 1.4 and later. + # The version is only checked when one of them is used. + # default_originate is true (default-originate) or a route-map name + # (default-originate route-map ); false means not configured. + for neighbor in self.want.get("neighbors", []): + for af in neighbor.get("address_family", []): + value = af.get("default_originate") + if value is False: + af.pop("default_originate") + elif ( + value is not None + and value is not True + and not (isinstance(value, str) and value) + ): + self._module.fail_json( + msg="neighbor {0}: default_originate must be true or a route-map name, " + "got {1!r}".format(neighbor["neighbor_address"], value), + ) + vpn = sorted( + { + af["afi"] + for neighbor in self.want.get("neighbors", []) + for af in neighbor.get("address_family", []) + if af.get("afi", "").endswith("-vpn") + }, + ) + if vpn and LooseVersion(get_os_version(self._module)) < LooseVersion("1.4"): + self._module.fail_json( + msg="address family {0} needs VyOS 1.4 or later".format(", ".join(vpn)), + ) + 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_af_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 == "deleted": for k, have in haved.items(): self._delete_af(wantd, have) wantd = {} if self.state == "overridden": for k, have in haved.items(): if k not in wantd: self._compare(want={}, have=have) 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_address_family network resource. """ if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): self._compare_asn(want, have) self._compare_af(want, have) self._compare_neighbors(want, have) # 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_af(self, want, have): waf = want.get("address_family", {}) haf = have.get("address_family", {}) for name, entry in waf.items(): self._compare_lists( entry, have=haf.get(name, {}), as_number=want["as_number"], afi=name, ) for name, entry in haf.items(): if name not in waf.keys() and self.state == "replaced": continue self._compare_lists({}, entry, as_number=have["as_number"], afi=name) def _delete_af(self, want, have): for as_num, entry in want.items(): for afi, af_entry in entry.get("address_family", {}).items(): if have.get("address_family"): for hafi, hentry in have["address_family"].items(): if hafi == afi: self.commands.append( self._tmplt.render( { "as_number": as_num, "address_family": {"afi": afi}, }, "address_family", True, ), ) for neigh, neigh_entry in entry.get("neighbors", {}).items(): if have.get("neighbors"): for hneigh, hnentry in have["neighbors"].items(): if hneigh == neigh: if not neigh_entry.get("address_family"): self.commands.append( self._tmplt.render( { "as_number": as_num, "neighbors": {"neighbor_address": neigh}, }, "neighbors", True, ), ) else: for k in neigh_entry["address_family"].keys(): if ( hnentry.get("address_family") and k in hnentry["address_family"].keys() ): self.commands.append( self._tmplt.render( { "as_number": as_num, "neighbors": { "neighbor_address": neigh, "address_family": {"afi": k}, }, }, "neighbors.address_family", True, ), ) def _compare_neighbors(self, want, have): parsers = [ "neighbors.allowas_in", "neighbors.as_override", "neighbors.attribute_unchanged.as_path", "neighbors.attribute_unchanged.med", "neighbors.attribute_unchanged.next_hop", "neighbors.capability_dynamic", "neighbors.capability_orf", "neighbors.default_originate", "neighbors.distribute_list", "neighbors.prefix_list", "neighbors.filter_list", "neighbors.maximum_prefix", "neighbors.nexthop_local", "neighbors.nexthop_self", "neighbors.peer_group", "neighbors.remove_private_as", "neighbors.route_map", "neighbors.route_reflector_client", "neighbors.route_server_client", "neighbors.soft_reconfiguration", "neighbors.unsuppress_map", "neighbors.weight", ] wneigh = want.get("neighbors", {}) hneigh = have.get("neighbors", {}) for name, entry in wneigh.items(): for afi, af_entry in entry.get("address_family").items(): + if set(af_entry) == {"afi"} and not hneigh.get(name, {}).get( + "address_family", + {}, + ).get(afi): + self.addcmd( + { + "as_number": want["as_number"], + "neighbors": {"neighbor_address": name, "address_family": {"afi": afi}}, + }, + "neighbors.address_family", + False, + ) for k, val in af_entry.items(): w = { "as_number": want["as_number"], "neighbors": { "neighbor_address": name, "address_family": {"afi": afi, k: val}, }, } h = {} if hneigh.get(name): if hneigh[name]["address_family"].get(afi): if hneigh[name]["address_family"][afi].get(k): h = { "as_number": want["as_number"], "neighbors": { "neighbor_address": name, "address_family": { "afi": afi, k: hneigh[name]["address_family"][afi].pop(k, {}), }, }, } + # from default-originate route-map to plain default-originate: + # the route map has to be deleted, setting the plain form keeps it + if ( + k == "default_originate" + and val is True + and isinstance(h.get("neighbors", {}).get("address_family", {}).get(k), str) + ): + self.addcmd(h, "neighbors.default_originate", True) + self.compare( parsers=parsers, want=w, have=h, ) for name, entry in hneigh.items(): if name not in wneigh.keys(): # remove surplus config for overridden and replaced if self.state != "replaced": self.commands.append( self._tmplt.render( { "as_number": have["as_number"], "neighbors": {"neighbor_address": name}, }, "neighbors", True, ), ) continue for hafi, haf_entry in entry.get("address_family").items(): - # remove surplus configs for given neighbor - replace and overridden + if set(haf_entry) == {"afi"} and hafi not in wneigh[name].get("address_family", {}): + self.addcmd( + { + "as_number": have["as_number"], + "neighbors": { + "neighbor_address": name, + "address_family": {"afi": hafi}, + }, + }, + "neighbors.address_family", + True, + ) for k, val in haf_entry.items(): h = { "as_number": have["as_number"], "neighbors": { "neighbor_address": name, "address_family": {"afi": hafi, k: val}, }, } self.compare(parsers=parsers, want={}, have=h) def _compare_lists(self, want, have, as_number, afi): parsers = [ "aggregate_address", "network", "network.backdoor", "network.path_limit", "network.route_map", "redistribute", "redistribute.metric", "redistribute.route_map", "redistribute.table", ] if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): delete_asn = "" else: delete_asn = " " + str(as_number) for attrib in ["redistribute", "networks", "aggregate_address"]: wdict = want.pop(attrib, {}) hdict = have.pop(attrib, {}) for key, entry in wdict.items(): if entry != hdict.get(key, {}): self.compare( parsers=parsers, want={ "as_number": as_number, "address_family": {"afi": afi, attrib: entry}, }, have={ "as_number": as_number, "address_family": { "afi": afi, attrib: hdict.pop(key, {}), }, }, ) hdict.pop(key, {}) # remove remaining items in have for replaced if not wdict and hdict: attrib = re.sub("_", "-", attrib) attrib = re.sub("networks", "network", attrib) self.commands.append( "delete protocols bgp" + delete_asn + " " + "address-family " + afi + " " + attrib, ) hdict = {} for key, entry in hdict.items(): self.compare( parsers=parsers, want={}, have={ "as_number": as_number, "address_family": {"afi": afi, attrib: entry}, }, ) # de-duplicate child commands if parent command is present for val in self.commands: for val2 in self.commands: if val != val2 and val2.startswith(val): self.commands.remove(val2) 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 _bgp_af_list_to_dict(self, entry): for name, proc in entry.items(): if "address_family" in proc: af_dict = {} for entry in proc.get("address_family"): if "networks" in entry: network_dict = {} for n_entry in entry.get("networks", []): network_dict.update({n_entry["prefix"]: n_entry}) entry["networks"] = network_dict if "aggregate_address" in entry: agg_dict = {} for a_entry in entry.get("aggregate_address", []): agg_dict.update({a_entry["prefix"]: a_entry}) entry["aggregate_address"] = agg_dict if "redistribute" in entry: redis_dict = {} for r_entry in entry.get("redistribute", []): proto_key = r_entry.get("protocol", "table") redis_dict.update({proto_key: r_entry}) entry["redistribute"] = redis_dict for af in proc.get("address_family"): af_dict.update({af["afi"]: af}) proc["address_family"] = af_dict if "neighbors" in proc: neigh_dict = {} for entry in proc.get("neighbors", []): neigh_dict.update({entry["neighbor_address"]: entry}) proc["neighbors"] = neigh_dict self._bgp_af_list_to_dict(proc["neighbors"]) diff --git a/plugins/module_utils/network/vyos/rm_templates/bgp_address_family.py b/plugins/module_utils/network/vyos/rm_templates/bgp_address_family.py index fe7889aa..6857a3c5 100644 --- a/plugins/module_utils/network/vyos/rm_templates/bgp_address_family.py +++ b/plugins/module_utils/network/vyos/rm_templates/bgp_address_family.py @@ -1,1434 +1,1436 @@ # -*- 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 + __metaclass__ = type """ The Bgp_address_family parser templates file. This contains a list of parser definitions and associated functions that facilitates both facts gathering and native command generation for the given network resource. """ import re from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.network_template import ( NetworkTemplate, ) def _tmplt_bgp_af_aggregate_address(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp {as_number} address-family ".format(**config_data) config_data = config_data["address_family"] if config_data["aggregate_address"].get("as_set"): command += afi + " aggregate-address {prefix} as-set".format( **config_data["aggregate_address"], ) if config_data["aggregate_address"].get("summary_only"): command += afi + " aggregate-address {prefix} summary-only".format( **config_data["aggregate_address"], ) return command def _tmplt_bgp_af_redistribute(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp {as_number} address-family ".format(**config_data) config_data = config_data["address_family"]["redistribute"] command += afi + " redistribute {protocol}".format(**config_data) if config_data.get("metric"): command += " metric {metric}".format(**config_data) elif config_data.get("route_map"): command += " route-map {route_map}".format(**config_data) elif config_data.get("table"): command += " table {table}".format(**config_data) return command def _tmplt_bgp_af_redistribute_delete(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp {as_number} address-family ".format(**config_data) config_data = config_data["address_family"]["redistribute"] command += afi + " redistribute {protocol}".format(**config_data) if config_data.get("metric"): command += " metric" elif config_data.get("route_map"): command += " route-map" elif config_data.get("table"): command += " table" return command def _tmplt_bgp_af_neighbor_distribute_list(config_data): command = [] afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" cmd = "protocols bgp {as_number} neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for list_el in config_data["distribute_list"]: command.append( cmd + afi + " distribute-list " + list_el["action"] + " " + str(list_el["acl"]), ) return command def _tmplt_bgp_af_neighbor_route_map(config_data): command = [] afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" cmd = "protocols bgp {as_number} neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for list_el in config_data["route_map"]: command.append( cmd + afi + " route-map " + list_el["action"] + " " + str(list_el["route_map"]), ) return command def _tmplt_bgp_af_neighbor_prefix_list(config_data): command = [] afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" cmd = "protocols bgp {as_number} neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for list_el in config_data["prefix_list"]: command.append( cmd + afi + " prefix-list " + list_el["action"] + " " + str(list_el["prefix_list"]), ) return command def _tmplt_bgp_af_neighbor_filter_list(config_data): command = [] afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" cmd = "protocols bgp {as_number} neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for list_el in config_data["filter_list"]: command.append( cmd + afi + " filter-list " + list_el["action"] + " " + str(list_el["path_list"]), ) return command def _tmplt_bgp_af_neighbor_attribute(config_data): command = [] afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" cmd = "protocols bgp {as_number} neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for k in config_data["attribute_unchanged"].keys(): if config_data["attribute_unchanged"][k]: k = re.sub("_", "-", k) c = cmd + afi + " attribute-unchanged " + k command.append(c) return command def _tmplt_bgp_af_neighbor_delete(config_data): afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" command = "protocols bgp {as_number} ".format(**config_data) command += ( "neighbor {neighbor_address} address-family ".format(**config_data["neighbors"]) + afi ) config_data = config_data["neighbors"]["address_family"] if config_data.get("allowas_in"): command += " allowas-in" elif config_data.get("as_override"): command += " as-override" elif config_data.get("attribute_unchanged"): command += " attribute-unchanged" elif config_data.get("capability"): command += " capability" elif config_data.get("default_originate"): + if config_data["default_originate"] is not True: + command += " route-map {default_originate}".format(**config_data) command += " default-originate" elif config_data.get("maximum_prefix"): command += " maximum-prefix" elif config_data.get("nexthop_local"): command += " nexthop-local" elif config_data.get("nexthop_self"): command += " nexthop-self" elif config_data.get("peer_group"): command += " peer-group" elif config_data.get("remote_private_as"): command += " remote-private-as" elif config_data.get("route_reflector_client"): command += " route-reflector-client" elif config_data.get("route_server_client"): command += " route-server-client" elif config_data.get("soft_reconfiguration"): command += " soft-reconfiguration" elif config_data.get("unsuppress_map"): command += " unsuppress-map" elif config_data.get("weight"): command += " weight" elif config_data.get("filter_list"): command += " filter-list" elif config_data.get("prefix_list"): command += " prefix-list" elif config_data.get("distribute_list"): command += " distribute-list" elif config_data.get("route_map"): command += " route-map" return command def _tmplt_bgp_af_neighbor(config_data): afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" command = "protocols bgp {as_number} ".format(**config_data) command += ( "neighbor {neighbor_address} address-family ".format(**config_data["neighbors"]) + afi ) config_data = config_data["neighbors"]["address_family"] if config_data.get("allowas_in"): command += " allowas-in number {allowas_in}".format(**config_data) elif config_data.get("as_override"): command += " as-override" elif config_data.get("capability"): command += " capability " if config_data["capability"].get("dynamic"): command += "dynamic" elif config_data["capability"].get("orf"): command += " prefix-list {orf}".format(**config_data["capability"]) elif config_data.get("default_originate"): command += " default-originate route-map {default_originate}".format(**config_data) elif config_data.get("maximum_prefix"): command += " maximum-prefix {maximum_prefix}".format(**config_data) elif config_data.get("nexthop_local"): command += " nexthop-local" elif config_data.get("nexthop_self"): command += " nexthop-self" elif config_data.get("peer_group"): command += " peer-group {peer_group}".format(**config_data) elif config_data.get("remote_private_as"): command += " remote-private-as" elif config_data.get("route_reflector_client"): command += " route-reflector-client" elif config_data.get("route_server_client"): command += " route-server-client" elif config_data.get("soft_reconfiguration"): command += " soft-reconfiguration inbound" elif config_data.get("unsuppress_map"): command += " unsuppress-map {unsuppress_map}".format(**config_data) elif config_data.get("weight"): command += " weight {weight}".format(**config_data) return command def _tmplt_bgp_af_network(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp {as_number} address-family ".format(**config_data) config_data = config_data["address_family"]["networks"] command += afi + " network {prefix}".format(**config_data) if config_data.get("backdoor"): command += " backdoor" elif config_data.get("route_map"): command += " route-map {route_map}".format(**config_data) return command def _tmplt_bgp_af_network_delete(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp {as_number} address-family ".format(**config_data) config_data = config_data["address_family"]["networks"] command += afi + " network {prefix}".format(**config_data) if config_data.get("backdoor"): command += " backdoor" elif config_data.get("route_map"): command += " route_map" return command class Bgp_address_familyTemplate(NetworkTemplate): def __init__(self, lines=None, module=None): prefix = {"set": "set", "remove": "delete"} super(Bgp_address_familyTemplate, self).__init__( lines=lines, tmplt=self, prefix=prefix, module=module, ) # fmt: off PARSERS = [ { "name": "address_family", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast *$""", re.VERBOSE, ), "setval": "protocols bgp {{ as_number }} address-family {{ address_family.afi }}-unicast", "compval": "as_number", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", }, }, }, }, { "name": "aggregate_address", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast \s+aggregate-address \s+(?P
\S+) \s*(?Pas-set)? \s*(?Psummary-only)? $""", re.VERBOSE, ), "setval": _tmplt_bgp_af_aggregate_address, "remval": "protocols bgp {{ as_number }} address-family {{ address_family.afi }}-unicast aggregate-address" + " {{ address_family.aggregate_address.prefix }}", "compval": "address_family.aggregate_address", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "aggregate_address": [ { "prefix": "{{ address }}", "as_set": "{{ True if as_set is defined }}", "summary_only": "{{ True if summary_only is defined }}", }, ], }, }, }, }, { "name": "network", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast \s+network \s+(?P
\S+) $""", re.VERBOSE, ), "setval": _tmplt_bgp_af_network, "remval": "protocols bgp {{ as_number }} address-family {{ address_family.afi }}-unicast network {{ address_family.networks.prefix }}", "compval": "address_family.networks.prefix", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "networks": [ { "prefix": "{{ address }}", }, ], }, }, }, }, { "name": "network.backdoor", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast \s+network \s+(?P
\S+) \s+backdoor *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_network, "remval": _tmplt_bgp_af_network_delete, "compval": "address_family.networks.backdoor", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "networks": [ { "prefix": "{{ address }}", "backdoor": "{{ True }}", }, ], }, }, }, }, { "name": "network.path_limit", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast \s+network \s+(?P
\S+) \s+path-limit \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_network, "remval": _tmplt_bgp_af_network_delete, "compval": "address_family.networks.path_limit", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "networks": [ { "prefix": "{{ address }}", "path_limit": "{{ limit|int }}", }, ], }, }, }, }, { "name": "network.route_map", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast \s+network \s+(?P
\S+) \s+route-map \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_network, "remval": _tmplt_bgp_af_network_delete, "compval": "address_family.networks.route_map", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "networks": [ { "prefix": "{{ address }}", "route_map": "{{ map }}", }, ], }, }, }, }, { "name": "redistribute", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast \s+redistribute \s+(?P\S+) $""", re.VERBOSE, ), "setval": _tmplt_bgp_af_redistribute, "remval": "protocols bgp {{ as_number }} address-family {{ address_family.afi }}-unicast redistribute {{ address_family.redistribute.protocol }}", "compval": "address_family.redistribute.protocol", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "redistribute": [ { "protocol": "{{ proto }}", }, ], }, }, }, }, { "name": "redistribute.metric", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast \s+redistribute \s+(?P\S+) \s+metric \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_redistribute, "remval": _tmplt_bgp_af_redistribute_delete, "compval": "address_family.redistribute.metric", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "redistribute": [ { "protocol": "{{ proto }}", "metric": "{{ val|int }}", }, ], }, }, }, }, { "name": "redistribute.route_map", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast \s+redistribute \s+(?P\S+) \s+route-map \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_redistribute, "remval": _tmplt_bgp_af_redistribute_delete, "compval": "address_family.redistribute.route_map", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "redistribute": [ { "protocol": "{{ proto }}", "route_map": "{{ map }}", }, ], }, }, }, }, { "name": "redistribute.table", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+address-family \s+(?P\S+)-unicast \s+redistribute \s+table \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_redistribute, "remval": _tmplt_bgp_af_redistribute_delete, "compval": "address_family.redistribute.table", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "redistribute": [ { "table": "{{ tab }}", }, ], }, }, }, }, { "name": "neighbors", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family *$""", re.VERBOSE, ), "setval": "protocols bgp {{ as_number }} neighbor {{ neighbors.neighbor_address }} address-family", "compval": "neighbors", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", }, }, }, }, { "name": "neighbors.address_family", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast *$""", re.VERBOSE, ), "setval": "protocols bgp {{ as_number }} neighbor {{ neighbors.neighbor_address }} address-family {{ neighbors.address_family.afi }}-unicast", "compval": "neighbors", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", }, }, }, }, }, }, { "name": "neighbors.allowas_in", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+allowas-in \s+number \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.allowas_in", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "allowas_in": "{{ num }}", }, }, }, }, }, }, { "name": "neighbors.as_override", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+as-override *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.as_override", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "as_override": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.attribute_unchanged.as_path", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+attribute-unchanged \s+(?Pas-path) *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_attribute, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.attribute_unchanged.as_path", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "attribute_unchanged": { "as_path": "{{ True }}", }, }, }, }, }, }, }, { "name": "neighbors.attribute_unchanged.med", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+attribute-unchanged \s+(?Pmed) *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_attribute, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.attribute_unchanged.med", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "attribute_unchanged": { "med": "{{ True }}", }, }, }, }, }, }, }, { "name": "neighbors.attribute_unchanged.next_hop", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+attribute-unchanged \s+(?Pnext-hop) *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_attribute, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.attribute_unchanged.next_hop", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "attribute_unchanged": { "next_hop": "{{ True }}", }, }, }, }, }, }, }, { "name": "neighbors.capability_dynamic", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+capability \s+dynamic *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.capability.dynamic", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "capability": { "dynamic": "{{ true }}", }, }, }, }, }, }, }, { "name": "neighbors.capability_orf", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+capability \s+prefix-list \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.capability.orf", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "capability": { "orf": "{{ orf }}", }, }, }, }, }, }, }, { "name": "neighbors.default_originate", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+default-originate - \s+route-map - \s+(?P\S+)\s*$""", + (?:\s+route-map\s+(?P\S+))?\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.default_originate", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", - "default_originate": "{{ map }}", + "default_originate": "{{ map if map is defined else True }}", }, }, }, }, }, }, { "name": "neighbors.distribute_list", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+distribute-list \s+(?Pexport|import) \s+(?P\d+) *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_distribute_list, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.distribute_list", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "distribute_list": [ { "action": "{{ action }}", "acl": "{{ list }}", }, ], }, }, }, }, }, }, { "name": "neighbors.prefix_list", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+prefix-list \s+(?Pexport|import) \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_prefix_list, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.prefix_list", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "prefix_list": [ { "action": "{{ action }}", "prefix_list": "{{ list }}", }, ], }, }, }, }, }, }, { "name": "neighbors.filter_list", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+filter-list \s+(?Pexport|import) \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_filter_list, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.filter_list", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "filter_list": [ { "action": "{{ action }}", "path_list": "{{ list }}", }, ], }, }, }, }, }, }, { "name": "neighbors.maximum_prefix", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+maximum-prefix \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.maximum_prefix", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "maximum_prefix": "{{ num }}", }, }, }, }, }, }, { "name": "neighbors.nexthop_local", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+nexthop-local *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.nexthop_local", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "nexthop_local": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.nexthop_self", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+nexthop-self *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.nexthop_self", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "nexthop_self": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.peer_group", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+peer-group \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.peer_group", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "peer_group": "{{ name }}", }, }, }, }, }, }, { "name": "neighbors.remove_private_as", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+remove-private-as *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.remove_private_as", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "remove_private_as": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.route_map", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+route-map \s+(?Pexport|import) \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_route_map, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.route_map", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "route_map": [ { "action": "{{ action }}", "route_map": "{{ map }}", }, ], }, }, }, }, }, }, { "name": "neighbors.route_reflector_client", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+route-reflector-client *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.route_reflector_client", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "route_reflector_client": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.route_server_client", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+route-server-client *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.route_server_client", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "route_server_client": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.soft_reconfiguration", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+soft-reconfiguration \s+inbound *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.soft_reconfiguration", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "soft_reconfiguration": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.unsuppress_map", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+unsuppress-map \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.unsuppress_map", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "unsuppress_map": "{{ map }}", }, }, }, }, }, }, { "name": "neighbors.weight", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+(?P\d+) \s+neighbor \s+(?P
\S+) \s+address-family \s+(?P\S+)-unicast \s+weight \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.weight", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "weight": "{{ num }}", }, }, }, }, }, }, ] # fmt: on diff --git a/plugins/module_utils/network/vyos/rm_templates/bgp_address_family_14.py b/plugins/module_utils/network/vyos/rm_templates/bgp_address_family_14.py index 9936cf6d..70392e55 100644 --- a/plugins/module_utils/network/vyos/rm_templates/bgp_address_family_14.py +++ b/plugins/module_utils/network/vyos/rm_templates/bgp_address_family_14.py @@ -1,1417 +1,1430 @@ # -*- 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 + __metaclass__ = type """ The Bgp_address_family parser templates file. This contains a list of parser definitions and associated functions that facilitates both facts gathering and native command generation for the given network resource. """ import re from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.network_template import ( NetworkTemplate, ) +def _afi_cli(afi): + # ipv4/ipv6 are the unicast families; the VPN families are used as they are + return afi if afi.endswith("-vpn") else afi + "-unicast" + + +def _tmplt_bgp_af_neighbor_afi(config_data): + return "protocols bgp neighbor {neighbor_address} address-family ".format( + **config_data["neighbors"], + ) + _afi_cli(config_data["neighbors"]["address_family"]["afi"]) + + def _tmplt_bgp_af_aggregate_address(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp address-family ".format(**config_data) config_data = config_data["address_family"] if config_data["aggregate_address"].get("as_set"): command += afi + " aggregate-address {prefix} as-set".format( **config_data["aggregate_address"], ) if config_data["aggregate_address"].get("summary_only"): command += afi + " aggregate-address {prefix} summary-only".format( **config_data["aggregate_address"], ) return command def _tmplt_bgp_af_redistribute(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp address-family ".format(**config_data) config_data = config_data["address_family"]["redistribute"] command += afi + " redistribute {protocol}".format(**config_data) if config_data.get("metric"): command += " metric {metric}".format(**config_data) elif config_data.get("route_map"): command += " route-map {route_map}".format(**config_data) elif config_data.get("table"): command += " table {table}".format(**config_data) return command def _tmplt_bgp_af_redistribute_delete(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp address-family ".format(**config_data) config_data = config_data["address_family"]["redistribute"] command += afi + " redistribute {protocol}".format(**config_data) if config_data.get("metric"): command += " metric" elif config_data.get("route_map"): command += " route-map" elif config_data.get("table"): command += " table" return command def _tmplt_bgp_af_neighbor_distribute_list(config_data): command = [] - afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" + afi = _afi_cli(config_data["neighbors"]["address_family"]["afi"]) cmd = "protocols bgp neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for list_el in config_data["distribute_list"]: command.append( cmd + afi + " distribute-list " + list_el["action"] + " " + str(list_el["acl"]), ) return command def _tmplt_bgp_af_neighbor_route_map(config_data): command = [] - afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" + afi = _afi_cli(config_data["neighbors"]["address_family"]["afi"]) cmd = "protocols bgp neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for list_el in config_data["route_map"]: command.append( cmd + afi + " route-map " + list_el["action"] + " " + str(list_el["route_map"]), ) return command def _tmplt_bgp_af_neighbor_prefix_list(config_data): command = [] - afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" + afi = _afi_cli(config_data["neighbors"]["address_family"]["afi"]) cmd = "protocols bgp neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for list_el in config_data["prefix_list"]: command.append( cmd + afi + " prefix-list " + list_el["action"] + " " + str(list_el["prefix_list"]), ) return command def _tmplt_bgp_af_neighbor_filter_list(config_data): command = [] - afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" + afi = _afi_cli(config_data["neighbors"]["address_family"]["afi"]) cmd = "protocols bgp neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for list_el in config_data["filter_list"]: command.append( cmd + afi + " filter-list " + list_el["action"] + " " + str(list_el["path_list"]), ) return command def _tmplt_bgp_af_neighbor_attribute(config_data): command = [] - afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" + afi = _afi_cli(config_data["neighbors"]["address_family"]["afi"]) cmd = "protocols bgp neighbor ".format(**config_data) cmd += "{neighbor_address} address-family ".format(**config_data["neighbors"]) config_data = config_data["neighbors"]["address_family"] for k in config_data["attribute_unchanged"].keys(): if config_data["attribute_unchanged"][k]: k = re.sub("_", "-", k) c = cmd + afi + " attribute-unchanged " + k command.append(c) return command def _tmplt_bgp_af_neighbor_delete(config_data): - afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" + afi = _afi_cli(config_data["neighbors"]["address_family"]["afi"]) command = "protocols bgp ".format(**config_data) command += ( "neighbor {neighbor_address} address-family ".format(**config_data["neighbors"]) + afi ) config_data = config_data["neighbors"]["address_family"] if config_data.get("allowas_in"): command += " allowas-in" elif config_data.get("as_override"): command += " as-override" elif config_data.get("attribute_unchanged"): command += " attribute-unchanged" elif config_data.get("capability"): command += " capability" elif config_data.get("default_originate"): command += " default-originate" elif config_data.get("maximum_prefix"): command += " maximum-prefix" elif config_data.get("nexthop_local"): command += " nexthop-local" elif config_data.get("nexthop_self"): command += " nexthop-self" elif config_data.get("peer_group"): command += " peer-group" elif config_data.get("remote_private_as"): command += " remote-private-as" elif config_data.get("route_reflector_client"): command += " route-reflector-client" elif config_data.get("route_server_client"): command += " route-server-client" elif config_data.get("soft_reconfiguration"): command += " soft-reconfiguration" elif config_data.get("unsuppress_map"): command += " unsuppress-map" elif config_data.get("weight"): command += " weight" elif config_data.get("filter_list"): command += " filter-list" elif config_data.get("prefix_list"): command += " prefix-list" elif config_data.get("distribute_list"): command += " distribute-list" elif config_data.get("route_map"): command += " route-map" return command def _tmplt_bgp_af_neighbor(config_data): - afi = config_data["neighbors"]["address_family"]["afi"] + "-unicast" + afi = _afi_cli(config_data["neighbors"]["address_family"]["afi"]) command = "protocols bgp ".format(**config_data) command += ( "neighbor {neighbor_address} address-family ".format(**config_data["neighbors"]) + afi ) config_data = config_data["neighbors"]["address_family"] if config_data.get("allowas_in"): command += " allowas-in number {allowas_in}".format(**config_data) elif config_data.get("as_override"): command += " as-override" elif config_data.get("capability"): command += " capability " if config_data["capability"].get("dynamic"): command += "dynamic" elif config_data["capability"].get("orf"): command += " prefix-list {orf}".format(**config_data["capability"]) elif config_data.get("default_originate"): - command += " default-originate route-map {default_originate}".format(**config_data) + command += " default-originate" + if config_data["default_originate"] is not True: + command += " route-map {default_originate}".format(**config_data) elif config_data.get("maximum_prefix"): command += " maximum-prefix {maximum_prefix}".format(**config_data) elif config_data.get("nexthop_local"): command += " nexthop-local" elif config_data.get("nexthop_self"): command += " nexthop-self" elif config_data.get("peer_group"): command += " peer-group {peer_group}".format(**config_data) elif config_data.get("remote_private_as"): command += " remote-private-as" elif config_data.get("route_reflector_client"): command += " route-reflector-client" elif config_data.get("route_server_client"): command += " route-server-client" elif config_data.get("soft_reconfiguration"): command += " soft-reconfiguration inbound" elif config_data.get("unsuppress_map"): command += " unsuppress-map {unsuppress_map}".format(**config_data) elif config_data.get("weight"): command += " weight {weight}".format(**config_data) return command def _tmplt_bgp_af_network(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp address-family ".format(**config_data) config_data = config_data["address_family"]["networks"] command += afi + " network {prefix}".format(**config_data) if config_data.get("backdoor"): command += " backdoor" elif config_data.get("route_map"): command += " route-map {route_map}".format(**config_data) return command def _tmplt_bgp_af_network_delete(config_data): afi = config_data["address_family"]["afi"] + "-unicast" command = "protocols bgp address-family ".format(**config_data) config_data = config_data["address_family"]["networks"] command += afi + " network {prefix}".format(**config_data) if config_data.get("backdoor"): command += " backdoor" elif config_data.get("route_map"): command += " route_map" return command class Bgp_address_familyTemplate14(NetworkTemplate): def __init__(self, lines=None, module=None): prefix = {"set": "set", "remove": "delete"} super(Bgp_address_familyTemplate14, self).__init__( lines=lines, tmplt=self, prefix=prefix, module=module, ) # fmt: off PARSERS = [ { "name": "system_as", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+system-as \s+(?P\d+) *$""", re.VERBOSE, ), "setval": "protocols bgp system-as {{ as_number }}", "compval": "as_number", "result": { "as_number": "{{ as_num }}", }, }, { "name": "address_family", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast *$""", re.VERBOSE, ), "setval": "protocols bgp address-family {{ address_family.afi }}-unicast", "compval": "as_number", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", }, }, }, }, { "name": "aggregate_address", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast \s+aggregate-address \s+(?P
\S+) \s*(?Pas-set)? \s*(?Psummary-only)? $""", re.VERBOSE, ), "setval": _tmplt_bgp_af_aggregate_address, "remval": "protocols bgp address-family {{ address_family.afi }}-unicast aggregate-address" + " {{ address_family.aggregate_address.prefix }}", "compval": "address_family.aggregate_address", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "aggregate_address": [ { "prefix": "{{ address }}", "as_set": "{{ True if as_set is defined }}", "summary_only": "{{ True if summary_only is defined }}", }, ], }, }, }, }, { "name": "network", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast \s+network \s+(?P
\S+) $""", re.VERBOSE, ), "setval": _tmplt_bgp_af_network, "remval": "protocols bgp address-family {{ address_family.afi }}-unicast network {{ address_family.networks.prefix }}", "compval": "address_family.networks.prefix", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "networks": [ { "prefix": "{{ address }}", }, ], }, }, }, }, { "name": "network.backdoor", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast \s+network \s+(?P
\S+) \s+backdoor *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_network, "remval": _tmplt_bgp_af_network_delete, "compval": "address_family.networks.backdoor", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "networks": [ { "prefix": "{{ address }}", "backdoor": "{{ True }}", }, ], }, }, }, }, { "name": "network.path_limit", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast \s+network \s+(?P
\S+) \s+path-limit \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_network, "remval": _tmplt_bgp_af_network_delete, "compval": "address_family.networks.path_limit", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "networks": [ { "prefix": "{{ address }}", "path_limit": "{{ limit|int }}", }, ], }, }, }, }, { "name": "network.route_map", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast \s+network \s+(?P
\S+) \s+route-map \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_network, "remval": _tmplt_bgp_af_network_delete, "compval": "address_family.networks.route_map", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "networks": [ { "prefix": "{{ address }}", "route_map": "{{ map }}", }, ], }, }, }, }, { "name": "redistribute", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast \s+redistribute \s+(?P\S+) $""", re.VERBOSE, ), "setval": _tmplt_bgp_af_redistribute, "remval": "protocols bgp address-family {{ address_family.afi }}-unicast redistribute {{ address_family.redistribute.protocol }}", "compval": "address_family.redistribute.protocol", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "redistribute": [ { "protocol": "{{ proto }}", }, ], }, }, }, }, { "name": "redistribute.metric", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast \s+redistribute \s+(?P\S+) \s+metric\s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_redistribute, "remval": _tmplt_bgp_af_redistribute_delete, "compval": "address_family.redistribute.metric", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "redistribute": [ { "protocol": "{{ proto }}", "metric": "{{ val }}", }, ], }, }, }, }, { "name": "redistribute.route_map", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast \s+redistribute \s+(?P\S+) \s+route-map \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_redistribute, "remval": _tmplt_bgp_af_redistribute_delete, "compval": "address_family.redistribute.route_map", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "redistribute": [ { "protocol": "{{ proto }}", "route_map": "{{ map }}", }, ], }, }, }, }, { "name": "redistribute.table", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+address-family \s+(?P\S+)-unicast \s+redistribute \s+table \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_redistribute, "remval": _tmplt_bgp_af_redistribute_delete, "compval": "address_family.redistribute.table", "result": { "as_number": "{{ as_num }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "redistribute": [ { "table": "{{ tab }}", }, ], }, }, }, }, { "name": "neighbors", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family *$""", re.VERBOSE, ), "setval": "protocols bgp neighbor {{ neighbors.neighbor_address }} address-family", "compval": "neighbors", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", }, }, }, }, { "name": "neighbors.address_family", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast - *$""", + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? + \s*$""", re.VERBOSE, ), - "setval": "protocols bgp neighbor {{ neighbors.neighbor_address }} address-family {{ neighbors.address_family.afi }}-unicast", + "setval": _tmplt_bgp_af_neighbor_afi, "compval": "neighbors", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", }, }, }, }, }, }, { "name": "neighbors.allowas_in", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+allowas-in \s+number \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.allowas_in", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "allowas_in": "{{ num }}", }, }, }, }, }, }, { "name": "neighbors.as_override", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+as-override *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.as_override", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "as_override": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.attribute_unchanged.as_path", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+attribute-unchanged \s+(?Pas-path) *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_attribute, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.attribute_unchanged.as_path", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "attribute_unchanged": { "as_path": "{{ True }}", }, }, }, }, }, }, }, { "name": "neighbors.attribute_unchanged.med", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+attribute-unchanged \s+(?Pmed) *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_attribute, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.attribute_unchanged.med", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "attribute_unchanged": { "med": "{{ True }}", }, }, }, }, }, }, }, { "name": "neighbors.attribute_unchanged.next_hop", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+attribute-unchanged \s+(?Pnext-hop) *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_attribute, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.attribute_unchanged.next_hop", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "attribute_unchanged": { "next_hop": "{{ True }}", }, }, }, }, }, }, }, { "name": "neighbors.capability_dynamic", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+capability \s+dynamic *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.capability.dynamic", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "capability": { "dynamic": "{{ true }}", }, }, }, }, }, }, }, { "name": "neighbors.capability_orf", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+capability \s+prefix-list \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.capability.orf", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "capability": { "orf": "{{ orf }}", }, }, }, }, }, }, }, { "name": "neighbors.default_originate", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+default-originate - \s+route-map - \s+(?P\S+)\s*$""", + (?:\s+route-map\s+(?P\S+))?\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.default_originate", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", - "default_originate": "{{ map }}", + "default_originate": "{{ map if map is defined else True }}", }, }, }, }, }, }, { "name": "neighbors.distribute_list", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+distribute-list \s+(?Pexport|import) \s+(?P\d+) *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_distribute_list, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.distribute_list", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "distribute_list": [ { "action": "{{ action }}", "acl": "{{ list }}", }, ], }, }, }, }, }, }, { "name": "neighbors.prefix_list", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+prefix-list \s+(?Pexport|import) \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_prefix_list, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.prefix_list", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "prefix_list": [ { "action": "{{ action }}", "prefix_list": "{{ list }}", }, ], }, }, }, }, }, }, { "name": "neighbors.filter_list", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+filter-list \s+(?Pexport|import) \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_filter_list, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.filter_list", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "filter_list": [ { "action": "{{ action }}", "path_list": "{{ list }}", }, ], }, }, }, }, }, }, { "name": "neighbors.maximum_prefix", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+maximum-prefix \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.maximum_prefix", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "maximum_prefix": "{{ num }}", }, }, }, }, }, }, { "name": "neighbors.nexthop_local", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+nexthop-local *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.nexthop_local", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "nexthop_local": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.nexthop_self", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+nexthop-self *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.nexthop_self", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "nexthop_self": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.peer_group", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+peer-group \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.peer_group", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "peer_group": "{{ name }}", }, }, }, }, }, }, { "name": "neighbors.remove_private_as", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+remove-private-as *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.remove_private_as", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "remove_private_as": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.route_map", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+route-map \s+(?Pexport|import) \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor_route_map, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.route_map", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "route_map": [ { "action": "{{ action }}", "route_map": "{{ map }}", }, ], }, }, }, }, }, }, { "name": "neighbors.route_reflector_client", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+route-reflector-client *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.route_reflector_client", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "route_reflector_client": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.route_server_client", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+route-server-client *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.route_server_client", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "route_server_client": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.soft_reconfiguration", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+soft-reconfiguration \s+inbound *$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.soft_reconfiguration", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "soft_reconfiguration": "{{ True }}", }, }, }, }, }, }, { "name": "neighbors.unsuppress_map", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+unsuppress-map \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.unsuppress_map", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "unsuppress_map": "{{ map }}", }, }, }, }, }, }, { "name": "neighbors.weight", "getval": re.compile( r""" ^set \s+protocols \s+bgp \s+neighbor \s+(?P
\S+) \s+address-family - \s+(?P\S+)-unicast + \s+(?Pipv4-vpn|ipv6-vpn|ipv4|ipv6)(?:-unicast)? \s+weight \s+(?P\S+)\s*$""", re.VERBOSE, ), "setval": _tmplt_bgp_af_neighbor, "remval": _tmplt_bgp_af_neighbor_delete, "compval": "neighbors.address_family.weight", "result": { "as_number": "{{ as_num }}", "neighbors": { "{{ address }}": { "neighbor_address": "{{ address }}", "address_family": { "{{ afi }}": { "afi": "{{ afi }}", "weight": "{{ num }}", }, }, }, }, }, }, ] # fmt: on diff --git a/plugins/modules/vyos_bgp_address_family.py b/plugins/modules/vyos_bgp_address_family.py index 4172643e..7a6579ec 100644 --- a/plugins/modules/vyos_bgp_address_family.py +++ b/plugins/modules/vyos_bgp_address_family.py @@ -1,1237 +1,1245 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # Copyright 2024 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) """ The module file for vyos_bgp_address_family """ from __future__ import absolute_import, division, print_function + __metaclass__ = type DOCUMENTATION = """ module: vyos_bgp_address_family version_added: 1.0.0 short_description: BGP Address Family resource module description: - This module manages BGP address family configuration of interfaces on devices running VYOS. - Tested against VyOS 1.3.8, 1.4.2, the upcoming 1.5, and the rolling release of spring 2025 - The provided examples of commands are valid for VyOS 1.4+ author: Gomathi Selvi Srinivasan (@GomathiselviS) options: config: description: A dict of BGP global configuration for interfaces. type: dict suboptions: as_number: description: - AS number type: int address_family: description: BGP address-family parameters. type: list elements: dict suboptions: afi: description: BGP address family settings. type: str choices: ['ipv4', 'ipv6'] aggregate_address: description: - BGP aggregate network. type: list elements: dict suboptions: prefix: description: BGP aggregate network. type: str as_set: description: Generate AS-set path information for this aggregate address. type: bool summary_only: description: Announce the aggregate summary network only. type: bool networks: description: BGP network type: list elements: dict suboptions: prefix: description: BGP network address type: str path_limit: description: AS path hop count limit type: int backdoor: description: Network as a backdoor route. type: bool route_map: description: Route-map to modify route attributes type: str redistribute: description: Redistribute routes from other protocols into BGP type: list elements: dict suboptions: protocol: description: types of routes to be redistributed. type: str choices: ['connected', 'kernel', 'ospf', 'ospfv3', 'rip', 'ripng', 'static'] table: description: Redistribute non-main Kernel Routing Table. type: str route_map: description: Route map to filter redistributed routes type: str metric: description: Metric for redistributed routes. type: int neighbors: description: BGP neighbor type: list elements: dict suboptions: neighbor_address: description: BGP neighbor address (v4/v6). type: str address_family: description: address family. type: list elements: dict suboptions: afi: - description: BGP neighbor parameters. + description: + - Address family of the neighbor. + - C(ipv4) and C(ipv6) are the unicast address families (C(ipv4-unicast), C(ipv6-unicast)), + C(ipv4-vpn) and C(ipv6-vpn) the L3VPN ones. type: str - choices: ['ipv4', 'ipv6'] + choices: ['ipv4', 'ipv6', 'ipv4-vpn', 'ipv6-vpn'] allowas_in: description: Number of occurrences of AS number. type: int as_override: description: AS for routes sent to this neighbor to be the local AS. type: bool attribute_unchanged: description: BGP attributes are sent unchanged. type: dict suboptions: as_path: description: as_path attribute type: bool med: description: med attribute type: bool next_hop: description: next_hop attribute type: bool capability: description: Advertise capabilities to this neighbor. type: dict suboptions: dynamic: description: Advertise dynamic capability to this neighbor. type: bool orf: description: Advertise ORF capability to this neighbor. type: str choices: ['send', 'receive'] default_originate: - description: Send default route to this neighbor - type: str + description: + - Send a default route to this neighbor. + - C(true) for C(default-originate), or a route-map name for + C(default-originate route-map ), which only originates the default route + when the route map matches. + type: raw distribute_list: description: Access-list to filter route updates to/from this neighbor. type: list elements: dict suboptions: action: description: Access-list to filter outgoing/incoming route updates to this neighbor type: str choices: ['export', 'import'] acl: description: Access-list number. type: int filter_list: description: As-path-list to filter route updates to/from this neighbor. type: list elements: dict suboptions: action: description: filter outgoing/incoming route updates type: str choices: ['export', 'import'] path_list: description: As-path-list to filter type: str maximum_prefix: description: Maximum number of prefixes to accept from this neighbor nexthop-self Nexthop for routes sent to this neighbor to be the local router. type: int nexthop_local: description: Nexthop attributes. type: bool nexthop_self: description: Nexthop for routes sent to this neighbor to be the local router. type: bool peer_group: description: IPv4 peer group for this peer type: str prefix_list: description: Prefix-list to filter route updates to/from this neighbor. type: list elements: dict suboptions: action: description: filter outgoing/incoming route updates type: str choices: ['export', 'import'] prefix_list: description: Prefix-list to filter type: str remove_private_as: description: Remove private AS numbers from AS path in outbound route updates type: bool route_map: description: Route-map to filter route updates to/from this neighbor. type: list elements: dict suboptions: action: description: filter outgoing/incoming route updates type: str choices: ['export', 'import'] route_map: description: route-map to filter type: str route_reflector_client: description: Neighbor as a route reflector client type: bool route_server_client: description: Neighbor is route server client type: bool soft_reconfiguration: description: Soft reconfiguration for neighbor type: bool unsupress_map: description: Route-map to selectively unsuppress suppressed routes type: str weight: description: Default weight for routes from this neighbor type: int running_config: type: str description: - This option is used only with state I(parsed). - The value of this option should be the output received from the VYOS device by executing the command B(show configuration command | match bgp). - The state I(parsed) reads the configuration from C(running_config) option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the I(parsed) key within the result. state: description: - The state the configuration should be left in. type: str choices: - merged - replaced - deleted - gathered - parsed - rendered - purged - overridden default: merged """ EXAMPLES = """ # Using merged # Before state # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # vyos@vyos:~$ - name: Merge provided configuration with device configuration vyos.vyos.vyos_bgp_address_family: config: as_number: "100" address_family: - afi: "ipv4" redistribute: - protocol: "static" metric: 50 neighbors: - neighbor_address: "20.33.1.1/24" address_family: - afi: "ipv4" allowas_in: 4 as_override: true attribute_unchanged: med: true - afi: "ipv6" default_originate: "map01" distribute_list: - action: "export" acl: 10 - neighbor_address: "100.11.34.12" address_family: - afi: "ipv4" maximum_prefix: 45 nexthop_self: true route_map: - action: "export" route_map: "map01" - action: "import" route_map: "map01" weight: 50 # After State: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export '10' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight '50' # vyos@vyos:~$ # # Module Execution: # # "after": { # "address_family": [ # { # "afi": "ipv4", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "export", # "route_map": "map01" # }, # { # "action": "import", # "route_map": "map01" # } # ], # "weight": 50 # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "before": {}, # "changed": true, # "commands": [ # "set protocols bgp address-family ipv4-unicast redistribute static metric 50", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number 4", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map map01", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export 10", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix 45", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight 50" # ], # # Using replaced: # Before state: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export '10' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight '50' # vyos@vyos:~$ - name: Replace provided configuration with device configuration vyos.vyos.vyos_bgp_address_family: config: as_number: "100" neighbors: - neighbor_address: "100.11.34.12" address_family: - afi: "ipv4" allowas_in: 4 as_override: true attribute_unchanged: med: true - afi: "ipv6" default_originate: "map01" distribute_list: - action: "export" acl: 10 - neighbor_address: "20.33.1.1/24" address_family: - afi: "ipv6" maximum_prefix: 45 nexthop_self: true state: replaced # After State: # # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast maximum-prefix '45' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast as-override # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast distribute-list export '10' # vyos@vyos:~$ # # # # Module Execution: # "after": { # "address_family": [ # { # "afi": "ipv4", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4" # }, # { # "afi": "ipv6", # "maximum_prefix": 45, # "nexthop_self": true # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "before": { # "address_family": [ # { # "afi": "ipv4", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "export", # "route_map": "map01" # }, # { # "action": "import", # "route_map": "map01" # } # ], # "weight": 50 # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "changed": true, # "commands": [ # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast allowas-in number 4", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast as-override", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast attribute-unchanged med", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast default-originate route-map map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast distribute-list export 10", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast maximum-prefix 45", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast nexthop-self" # ], # Using overridden # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast network 35.1.1.0/24 backdoor # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp address-family ipv6-unicast aggregate-address 6601:1:1:1::/64 summary-only # set protocols bgp address-family ipv6-unicast network 5001:1:1:1::/64 route-map 'map01' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast maximum-prefix '45' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast as-override # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast distribute-list export '10' # vyos@vyos:~$ - name: Override vyos.vyos.vyos_bgp_address_family: config: as_number: "100" neighbors: - neighbor_address: "100.11.34.12" address_family: - afi: "ipv6" maximum_prefix: 45 nexthop_self: true route_map: - action: "import" route_map: "map01" address_family: - afi: "ipv4" aggregate_address: - prefix: "60.9.2.0/24" summary_only: true - afi: "ipv6" redistribute: - protocol: "static" metric: 50 state: overridden # After State # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast aggregate-address 60.9.2.0/24 summary-only # set protocols bgp address-family ipv6-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast route-map import 'map01' # vyos@vyos:~$ # Module Execution: # "after": { # "address_family": [ # { # "afi": "ipv4", # "aggregate_address": [ # { # "prefix": "60.9.2.0/24", # "summary_only": true # } # ] # }, # { # "afi": "ipv6", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4" # }, # { # "afi": "ipv6", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "import", # "route_map": "map01" # } # ] # } # ], # "neighbor_address": "100.11.34.12" # } # ] # }, # "before": { # "address_family": [ # { # "afi": "ipv4", # "networks": [ # { # "backdoor": true, # "prefix": "35.1.1.0/24" # } # ], # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # }, # { # "afi": "ipv6", # "aggregate_address": [ # { # "prefix": "6601:1:1:1::/64", # "summary_only": true # } # ], # "networks": [ # { # "prefix": "5001:1:1:1::/64", # "route_map": "map01" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4" # }, # { # "afi": "ipv6", # "maximum_prefix": 45, # "nexthop_self": true # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "changed": true, # "commands": [ # "delete protocols bgp neighbor 20.33.1.1/24 address-family", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast distribute-list", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast default-originate", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast attribute-unchanged", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast as-override", # "delete protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast allowas-in", # "delete protocols bgp address-family ipv6 aggregate-address", # "delete protocols bgp address-family ipv6 network", # "delete protocols bgp address-family ipv4 network", # "delete protocols bgp address-family ipv4 redistribute", # "set protocols bgp address-family ipv4-unicast aggregate-address 60.9.2.0/24 summary-only", # "set protocols bgp address-family ipv6-unicast redistribute static metric 50", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast maximum-prefix 45", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast nexthop-self", # "set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast route-map import map01" # ], # # Using deleted: # Before State: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast aggregate-address 60.9.2.0/24 summary-only # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp address-family ipv6-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map 'map01' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export '10' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import 'map01' # set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight '50' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast maximum-prefix '45' # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast nexthop-self # set protocols bgp neighbor 100.11.34.12 address-family ipv6-unicast route-map import 'map01' # vyos@vyos:~$ - name: Delete vyos.vyos.vyos_bgp_address_family: config: as_number: "100" neighbors: - neighbor_address: "20.33.1.1/24" address_family: - afi: "ipv6" - neighbor_address: "100.11.34.12" address_family: - afi: "ipv4" state: deleted # After State: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv6-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 100.11.34.12 # vyos@vyos:~$ # # # Module Execution: # # "after": { # "address_family": [ # { # "afi": "ipv6", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "before": { # "address_family": [ # { # "afi": "ipv4", # "aggregate_address": [ # { # "prefix": "60.9.2.0/24", # "summary_only": true # } # ], # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # }, # { # "afi": "ipv6", # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "export", # "route_map": "map01" # }, # { # "action": "import", # "route_map": "map01" # } # ], # "weight": 50 # }, # { # "afi": "ipv6", # "maximum_prefix": 45, # "nexthop_self": true, # "route_map": [ # { # "action": "import", # "route_map": "map01" # } # ] # } # ], # "neighbor_address": "100.11.34.12" # }, # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # }, # { # "afi": "ipv6", # "default_originate": "map01", # "distribute_list": [ # { # "acl": 10, # "action": "export" # } # ] # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # }, # "changed": true, # "commands": [ # "delete protocols bgp address-family ipv4-unicast", # "delete protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast", # "delete protocols bgp neighbor 100.11.34.12 address-family" # ], # # using parsed: # parsed.cfg # set protocols bgp 65536 address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set # set protocols bgp 65536 address-family ipv4-unicast network 192.1.13.0/24 route-map 'map01' # set protocols bgp 65536 address-family ipv4-unicast network 192.2.13.0/24 backdoor # set protocols bgp 65536 address-family ipv6-unicast redistribute ripng metric '20' # set protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast route-map export 'map01' # set protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound # set protocols bgp 65536 neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop - name: parse configs vyos.vyos.vyos_bgp_address_family: running_config: "{{ lookup('file', './parsed.cfg') }}" state: parsed # Module execution result: # # "parsed": { # "address_family": [ # { # "afi": "ipv4", # "aggregate_address": [ # { # "as_set": true, # "prefix": "192.0.2.0/24" # } # ], # "networks": [ # { # "prefix": "192.1.13.0/24", # "route_map": "map01" # }, # { # "backdoor": true, # "prefix": "192.2.13.0/24" # } # ] # }, # { # "afi": "ipv6", # "redistribute": [ # { # "metric": 20, # "protocol": "ripng" # } # ] # } # ], # "as_number": 65536, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "route_map": [ # { # "action": "export", # "route_map": "map01" # } # ], # "soft_reconfiguration": true # } # ], # "neighbor_address": "192.0.2.25" # }, # { # "address_family": [ # { # "afi": "ipv6", # "attribute_unchanged": { # "next_hop": true # } # } # ], # "neighbor_address": "203.0.113.5" # } # ] # # Using gathered: # Native config: # vyos@vyos:~$ show configuration commands | match "set protocols bgp" # set protocols bgp system-as 100 # set protocols bgp address-family ipv4-unicast network 35.1.1.0/24 backdoor # set protocols bgp address-family ipv4-unicast redistribute static metric '50' # set protocols bgp address-family ipv6-unicast aggregate-address 6601:1:1:1::/64 summary-only # set protocols bgp address-family ipv6-unicast network 5001:1:1:1::/64 route-map 'map01' # set protocols bgp address-family ipv6-unicast redistribute static metric '50' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number '4' # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override # set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med # set protocols bgp neighbor 100.11.34.12 - name: gather configs vyos.vyos.vyos_bgp_address_family: state: gathered # Module execution result: # # "gathered": { # "address_family": [ # { # "afi": "ipv4", # "networks": [ # { # "backdoor": true, # "prefix": "35.1.1.0/24" # } # ], # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # }, # { # "afi": "ipv6", # "aggregate_address": [ # { # "prefix": "6601:1:1:1::/64", # "summary_only": true # } # ], # "networks": [ # { # "prefix": "5001:1:1:1::/64", # "route_map": "map01" # } # ], # "redistribute": [ # { # "metric": 50, # "protocol": "static" # } # ] # } # ], # "as_number": 100, # "neighbors": [ # { # "address_family": [ # { # "afi": "ipv4", # "allowas_in": 4, # "as_override": true, # "attribute_unchanged": { # "med": true # } # } # ], # "neighbor_address": "20.33.1.1/24" # } # ] # Using rendered: - name: Render vyos.vyos.vyos_bgp_address_family: config: as_number: "100" address_family: - afi: "ipv4" redistribute: - protocol: "static" metric: 50 neighbors: - neighbor_address: "20.33.1.1/24" address_family: - afi: "ipv4" allowas_in: 4 as_override: true attribute_unchanged: med: true - afi: "ipv6" default_originate: "map01" distribute_list: - action: "export" acl: 10 - neighbor_address: "100.11.34.12" address_family: - afi: "ipv4" maximum_prefix: 45 nexthop_self: true route_map: - action: "export" route_map: "map01" - action: "import" route_map: "map01" weight: 50 state: rendered # Module Execution: # "rendered": [ # "set protocols bgp address-family ipv4-unicast redistribute static metric 50", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast allowas-in number 4", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast as-override", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv4-unicast attribute-unchanged med", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast default-originate route-map map01", # "set protocols bgp neighbor 20.33.1.1/24 address-family ipv6-unicast distribute-list export 10", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast maximum-prefix 45", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast nexthop-self", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map export map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast route-map import map01", # "set protocols bgp neighbor 100.11.34.12 address-family ipv4-unicast weight 50" # ] """ RETURN = """ before: description: The configuration prior to the module execution. returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged) type: dict sample: > This output will always be in the same format as the module argspec. after: description: The resulting configuration after module execution. returned: when changed type: dict sample: > This output will always be in the same format as the module argspec. commands: description: The set of commands pushed to the remote device. returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged) type: list sample: - sample command 1 - sample command 2 - sample command 3 rendered: description: The provided configuration in the task rendered in device-native format (offline). returned: when I(state) is C(rendered) type: list sample: - sample command 1 - sample command 2 - sample command 3 gathered: description: Facts about the network resource gathered from the remote device as structured data. returned: when I(state) is C(gathered) type: list sample: > This output will always be in the same format as the module argspec. parsed: description: The device native config provided in I(running_config) option parsed into structured data as per module argspec. returned: when I(state) is C(parsed) type: list sample: > This output will always be in the same format as the module argspec. """ from ansible.module_utils.basic import AnsibleModule from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.bgp_address_family.bgp_address_family import ( Bgp_address_familyArgs, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.bgp_address_family.bgp_address_family import ( Bgp_address_family, ) def main(): """ Main entry point for module execution :returns: the result form module invocation """ module = AnsibleModule( argument_spec=Bgp_address_familyArgs.argument_spec, mutually_exclusive=[["config", "running_config"]], required_if=[ ["state", "merged", ["config"]], ["state", "replaced", ["config"]], ["state", "overridden", ["config"]], ["state", "rendered", ["config"]], ["state", "parsed", ["running_config"]], ], supports_check_mode=True, ) result = Bgp_address_family(module).execute_module() module.exit_json(**result) if __name__ == "__main__": main() diff --git a/tests/integration/targets/vyos_bgp_address_family/tests/cli/merged.yaml b/tests/integration/targets/vyos_bgp_address_family/tests/cli/merged.yaml index acfae74d..fba6ccb0 100644 --- a/tests/integration/targets/vyos_bgp_address_family/tests/cli/merged.yaml +++ b/tests/integration/targets/vyos_bgp_address_family/tests/cli/merged.yaml @@ -1,65 +1,203 @@ --- - debug: msg: START vyos_bgp_address_family merged integration tests on connection={{ ansible_connection }} - include_tasks: _remove_config.yaml - include_tasks: _preconfig.yaml - block: - name: Merge the provided configuration with the existing running configuration register: result vyos.vyos.vyos_bgp_address_family: &id001 config: as_number: "65536" address_family: - afi: ipv6 redistribute: - protocol: static metric: 50 neighbors: - neighbor_address: 203.0.113.5 address_family: - afi: ipv6 allowas_in: 4 attribute_unchanged: med: true default_originate: map01 distribute_list: - action: export acl: 10 - neighbor_address: 192.0.2.25 address_family: - afi: ipv6 maximum_prefix: 45 nexthop_self: true route_map: - action: export route_map: map01 - action: import route_map: map01 state: merged - vyos.vyos.vyos_facts: gather_network_resources: bgp_address_family - assert: that: - result.commands|length == merged.commands|length - result.changed == true - result.commands|symmetric_difference(merged.commands) == [] - result.after == ansible_facts['network_resources']['bgp_address_family'] - result.before == merged.before - result.after == merged.after - name: Merge the provided configuration with the existing running configuration (IDEMPOTENT) register: result vyos.vyos.vyos_bgp_address_family: *id001 - name: Assert that the previous task was idempotent assert: that: - result['changed'] == false always: - include_tasks: _remove_config.yaml + +- include_tasks: _get_version.yaml + +- block: + - name: setup an iBGP neighbor + vyos.vyos.vyos_config: + lines: + - set protocols bgp system-as 65536 + - set protocols bgp neighbor 192.0.2.50 remote-as internal + vars: + ansible_connection: ansible.netcommon.network_cli + + - name: Merge VPN address families for a route-reflector client + register: result + vyos.vyos.vyos_bgp_address_family: &id010 + config: + as_number: 65536 + neighbors: + - neighbor_address: 192.0.2.50 + address_family: + - afi: ipv4-vpn + route_reflector_client: true + nexthop_self: true + - afi: ipv6-vpn + route_reflector_client: true + state: merged + + - vyos.vyos.vyos_facts: + gather_network_resources: bgp_address_family + + - assert: + that: + - result.changed == true + - result.commands | symmetric_difference(expected) == [] + - result.after == ansible_facts['network_resources']['bgp_address_family'] + vars: + expected: + - set protocols bgp neighbor 192.0.2.50 address-family ipv4-vpn route-reflector-client + - set protocols bgp neighbor 192.0.2.50 address-family ipv4-vpn nexthop-self + - set protocols bgp neighbor 192.0.2.50 address-family ipv6-vpn route-reflector-client + + - name: Merge VPN address families for a route-reflector client (IDEMPOTENT) + register: result + vyos.vyos.vyos_bgp_address_family: *id010 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + when: vyos_version is version('1.4.0', '>=', version_type='semver') + always: + - include_tasks: _remove_config.yaml + +- block: + - name: setup a neighbor + vyos.vyos.vyos_config: + lines: + - set protocols bgp system-as 65536 + - set protocols bgp neighbor 192.0.2.60 remote-as 65002 + vars: + ansible_connection: ansible.netcommon.network_cli + + - name: Merge an address family without options (activate the neighbor) + register: result + vyos.vyos.vyos_bgp_address_family: &id011 + config: + as_number: 65536 + neighbors: + - neighbor_address: 192.0.2.60 + address_family: + - afi: ipv4 + state: merged + + - vyos.vyos.vyos_facts: + gather_network_resources: bgp_address_family + + - assert: + that: + - result.changed == true + - result.commands == ['set protocols bgp neighbor 192.0.2.60 address-family ipv4-unicast'] + - result.after == ansible_facts['network_resources']['bgp_address_family'] + + - name: Merge an address family without options (IDEMPOTENT) + register: result + vyos.vyos.vyos_bgp_address_family: *id011 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + when: vyos_version is version('1.4.0', '>=', version_type='semver') + always: + - include_tasks: _remove_config.yaml + +- block: + - name: setup a neighbor + vyos.vyos.vyos_config: + lines: + - set protocols bgp system-as 65536 + - set protocols bgp neighbor 192.0.2.70 remote-as 65002 + vars: + ansible_connection: ansible.netcommon.network_cli + + - name: Merge default-originate without a route map + register: result + vyos.vyos.vyos_bgp_address_family: &id012 + config: + as_number: 65536 + neighbors: + - neighbor_address: 192.0.2.70 + address_family: + - afi: ipv4 + default_originate: true + state: merged + + - vyos.vyos.vyos_facts: + gather_network_resources: bgp_address_family + + - assert: + that: + - result.changed == true + - result.commands == ['set protocols bgp neighbor 192.0.2.70 address-family ipv4-unicast default-originate'] + - result.after == ansible_facts['network_resources']['bgp_address_family'] + + - name: Merge default-originate without a route map (IDEMPOTENT) + register: result + vyos.vyos.vyos_bgp_address_family: *id012 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + when: vyos_version is version('1.4.0', '>=', version_type='semver') + always: + - include_tasks: _remove_config.yaml diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_bgp_address_family_bare_config_14.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_bgp_address_family_bare_config_14.cfg new file mode 100644 index 00000000..e3e1fada --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_bgp_address_family_bare_config_14.cfg @@ -0,0 +1,2 @@ +set protocols bgp system-as '65536' +set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_bgp_address_family_default_originate_config_14.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_bgp_address_family_default_originate_config_14.cfg new file mode 100644 index 00000000..cd174499 --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_bgp_address_family_default_originate_config_14.cfg @@ -0,0 +1,2 @@ +set protocols bgp system-as '65536' +set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast default-originate route-map 'RM-DEF' diff --git a/tests/unit/modules/network/vyos/test_vyos_bgp_address_family.py b/tests/unit/modules/network/vyos/test_vyos_bgp_address_family.py index 9d6f6297..5491209a 100644 --- a/tests/unit/modules/network/vyos/test_vyos_bgp_address_family.py +++ b/tests/unit/modules/network/vyos/test_vyos_bgp_address_family.py @@ -1,789 +1,810 @@ # (c) 2016 Red Hat Inc. # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . # Make coding more python3-ish from __future__ import absolute_import, division, print_function + __metaclass__ = type from unittest.mock import patch from ansible_collections.vyos.vyos.plugins.modules import vyos_bgp_address_family from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture class TestVyosBgpafModule13(TestVyosModule): module = vyos_bgp_address_family def setUp(self): super(TestVyosBgpafModule13, self).setUp() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", ) self.get_resource_connection_config = self.mock_get_resource_connection_config.start() self.mock_execute_show_command = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts." + "bgp_address_family.bgp_address_family.Bgp_address_familyFacts.get_device_data", ) self.execute_show_command = self.mock_execute_show_command.start() self.mock_get_os_version = patch( - "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.bgp_address_family.bgp_address_family.get_os_version" + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.bgp_address_family.bgp_address_family.get_os_version", ) self.test_version = "1.2" self.get_os_version = self.mock_get_os_version.start() self.get_os_version.return_value = self.test_version self.mock_facts_get_os_version = patch( - "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_address_family.bgp_address_family.get_os_version" + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_address_family.bgp_address_family.get_os_version", ) self.get_facts_os_version = self.mock_facts_get_os_version.start() self.get_facts_os_version.return_value = self.test_version self.maxDiff = None def tearDown(self): super(TestVyosBgpafModule13, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_execute_show_command.stop() self.mock_get_os_version.stop() self.mock_facts_get_os_version.stop() def load_fixtures(self, commands=None, filename=None): if filename is None: filename = "vyos_bgp_address_family_config.cfg" def load_from_file(*args, **kwargs): output = load_fixture(filename) return output self.execute_show_command.side_effect = load_from_file def test_vyos_bgp_address_family_merged_idempotent(self): set_module_args( dict( config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_merged(self): set_module_args( dict( config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", summary_only=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv4", filter_list=[ dict(action="export", path_list="list01"), ], capability=dict(orf="send"), ), ], ), ], ), ), ) commands = [ "set protocols bgp 65536 address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-setipv4-unicast aggregate-address 192.0.2.0/24 summary-only", "set protocols bgp 65536 address-family ipv6-unicast redistribute ospfv3 metric 20", "set protocols bgp 65536 neighbor 203.0.113.5 address-family ipv4-unicast filter-list export list01", "set protocols bgp 65536 neighbor 203.0.113.5 address-family ipv4-unicast capability prefix-list send", "set protocols bgp 65536 neighbor 192.10.21.25 address-family ipv6-unicast distribute-list export 10", "set protocols bgp 65536 neighbor 192.10.21.25 address-family ipv6-unicast route-server-client", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_replaced_idempotent(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_replaced(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", summary_only=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="import", route_map="map01")], ), dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv4", filter_list=[ dict(action="export", path_list="list01"), ], capability=dict(orf="send"), ), ], ), ], ), ), ) commands = [ "delete protocols bgp 65536 neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged", "delete protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration", "delete protocols bgp 65536 address-family ipv6-unicast redistribute ripng", "delete protocols bgp 65536 address-family ipv4-unicast network 192.2.13.0/24", "set protocols bgp 65536 address-family ipv4-unicast aggregate-address 192.0.2.0/24 summary-only", "set protocols bgp 65536 address-family ipv6-unicast redistribute ospfv3 metric 20", "set protocols bgp 65536 neighbor 192.10.21.25 address-family ipv4-unicast route-map import map01", "set protocols bgp 65536 neighbor 192.10.21.25 address-family ipv6-unicast distribute-list export 10", "set protocols bgp 65536 neighbor 192.10.21.25 address-family ipv6-unicast route-server-client", "set protocols bgp 65536 neighbor 203.0.113.5 address-family ipv4-unicast filter-list export list01", "set protocols bgp 65536 neighbor 203.0.113.5 address-family ipv4-unicast capability prefix-list send", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_overridden_idempotent(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_overridden(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="import", route_map="map01")], ), dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), ], ), ), ) commands = [ "delete protocols bgp 65536 neighbor 203.0.113.5 address-family", "delete protocols bgp 65536 neighbor 192.0.2.25 address-family", "delete protocols bgp 65536 address-family ipv6-unicast redistribute ripng", "delete protocols bgp 65536 address-family ipv4 aggregate-address", "delete protocols bgp 65536 address-family ipv4-unicast network 192.2.13.0/24", "set protocols bgp 65536 address-family ipv6-unicast redistribute ospfv3 metric 20", "set protocols bgp 65536 neighbor 192.10.21.25 address-family ipv4-unicast route-map import map01", "set protocols bgp 65536 neighbor 192.10.21.25 address-family ipv6-unicast distribute-list export 10", "set protocols bgp 65536 neighbor 192.10.21.25 address-family ipv6-unicast route-server-client", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_deleted(self): set_module_args( dict( state="deleted", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", ), ], ), dict( neighbor_address="203.0.113.5", ), ], ), ), ) commands = [ "delete protocols bgp 65536 address-family ipv4-unicast", "delete protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast", "delete protocols bgp 65536 neighbor 203.0.113.5 address-family", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_incorrect_instance(self): set_module_args( dict( state="overridden", config=dict( as_number=100, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="import", route_map="map01")], ), dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), ], ), ), ) result = self.execute_module(failed=True) self.assertIn("Only one bgp instance is allowed per device", result["msg"]) def test_vyos_bgp_address_family_rendered(self): set_module_args( dict( state="rendered", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) rendered_cmds = [ "set protocols bgp 65536 address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp 65536 address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp 65536 address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp 65536 address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp 65536 neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] result = self.execute_module(changed=False) self.assertEqual( sorted(result["rendered"]), sorted(rendered_cmds), result["rendered"], ) def test_vyos_bgp_address_family_parsed(self): commands = [ "set protocols bgp 65536 address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp 65536 address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp 65536 address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp 65536 address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp 65536 neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp 65536 neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] parsed_str = "\n".join(commands) set_module_args(dict(running_config=parsed_str, state="parsed")) result = self.execute_module(changed=False) parsed_list = { "as_number": 65536, "address_family": [ { "afi": "ipv4", "networks": [ {"prefix": "192.1.13.0/24", "route_map": "map01"}, {"prefix": "192.2.13.0/24", "backdoor": True}, ], "aggregate_address": [{"prefix": "192.0.2.0/24", "as_set": True}], }, { "afi": "ipv6", "redistribute": [{"protocol": "ripng", "metric": 20}], }, ], "neighbors": [ { "neighbor_address": "192.0.2.25", "address_family": [ {"afi": "ipv4", "soft_reconfiguration": True}, ], }, { "neighbor_address": "203.0.113.5", "address_family": [ { "afi": "ipv6", "attribute_unchanged": {"next_hop": True}, }, ], }, ], } self.assertEqual(sorted(parsed_list), sorted(result["parsed"])) def test_vyos_bgp_address_family_gathered(self): set_module_args(dict(state="gathered")) result = self.execute_module(changed=False) gather_list = { "as_number": 65536, "address_family": [ { "afi": "ipv4", "networks": [ {"prefix": "192.1.13.0/24", "route_map": "map01"}, {"prefix": "192.2.13.0/24", "backdoor": True}, ], "aggregate_address": [{"prefix": "192.0.2.0/24", "as_set": True}], }, { "afi": "ipv6", "redistribute": [{"protocol": "ripng", "metric": 20}], }, ], "neighbors": [ { "neighbor_address": "192.0.2.25", "address_family": [ {"afi": "ipv4", "soft_reconfiguration": True}, ], }, { "neighbor_address": "203.0.113.5", "address_family": [ { "afi": "ipv6", "attribute_unchanged": {"next_hop": True}, }, ], }, ], } self.assertEqual(sorted(gather_list), sorted(result["gathered"])) + def test_vyos_bgp_af_neighbor_default_originate_parsed(self): + set_module_args( + dict( + running_config="set protocols bgp 65536 neighbor 192.0.2.1 address-family ipv4-unicast default-originate", + state="parsed", + ), + ) + self.assertEqual( + self.execute_module(changed=False)["parsed"], + { + "as_number": 65536, + "neighbors": [ + { + "neighbor_address": "192.0.2.1", + "address_family": [{"afi": "ipv4", "default_originate": True}], + }, + ], + }, + ) + class TestVyosBgpafOpsModule13(TestVyosModule): module = vyos_bgp_address_family def setUp(self): super(TestVyosBgpafOpsModule13, self).setUp() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", ) self.get_resource_connection_config = self.mock_get_resource_connection_config.start() self.mock_execute_show_command = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts." + "bgp_address_family.bgp_address_family.Bgp_address_familyFacts.get_device_data", ) self.execute_show_command = self.mock_execute_show_command.start() self.mock_get_os_version = patch( - "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.bgp_address_family.bgp_address_family.get_os_version" + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.bgp_address_family.bgp_address_family.get_os_version", ) self.test_version = "1.2" self.get_os_version = self.mock_get_os_version.start() self.get_os_version.return_value = self.test_version self.mock_facts_get_os_version = patch( - "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_address_family.bgp_address_family.get_os_version" + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_address_family.bgp_address_family.get_os_version", ) self.get_facts_os_version = self.mock_facts_get_os_version.start() self.get_facts_os_version.return_value = self.test_version self.maxDiff = None def tearDown(self): super(TestVyosBgpafOpsModule13, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_execute_show_command.stop() self.mock_get_os_version.stop() self.mock_facts_get_os_version.stop() def load_fixtures(self, commands=None, filename=None): if filename is None: filename = "vyos_bgp_af_ops_config.cfg" def load_from_file(*args, **kwargs): output = load_fixture(filename) return output self.execute_show_command.side_effect = load_from_file def test_vyos_bgp_address_family_merged(self): set_module_args( dict( config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.3.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], ), ), ) commands = [ "set protocols bgp 65536 address-family ipv4-unicast network 192.3.13.0/24 backdoor", "set protocols bgp 65536 address-family ipv6-unicast redistribute ospfv3 metric 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_replaced(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", backdoor=True), ], redistribute=[ dict(protocol="ospf", metric=25), ], ), dict( afi="ipv6", redistribute=[ dict(protocol="ospfv3", metric=20), dict(protocol="ripng"), ], ), ], ), ), ) commands = [ "delete protocols bgp 65536 address-family ipv6-unicast redistribute ripng metric", "delete protocols bgp 65536 address-family ipv4-unicast network 192.2.13.0/24", "delete protocols bgp 65536 address-family ipv4-unicast redistribute rip", "set protocols bgp 65536 address-family ipv4-unicast redistribute ospf metric 25", "set protocols bgp 65536 address-family ipv4-unicast network 192.1.13.0/24 backdoor", "set protocols bgp 65536 address-family ipv6-unicast redistribute ospfv3 metric 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_overridden(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", backdoor=True), ], redistribute=[ dict(protocol="ospf", metric=25), ], ), dict( afi="ipv6", redistribute=[ dict(protocol="ospfv3", metric=20), dict(protocol="ripng"), ], ), ], ), ), ) commands = [ "delete protocols bgp 65536 address-family ipv6-unicast redistribute ripng metric", "delete protocols bgp 65536 address-family ipv4-unicast network 192.2.13.0/24", "delete protocols bgp 65536 address-family ipv4-unicast redistribute rip", "set protocols bgp 65536 address-family ipv4-unicast redistribute ospf metric 25", "set protocols bgp 65536 address-family ipv4-unicast network 192.1.13.0/24 backdoor", "set protocols bgp 65536 address-family ipv6-unicast redistribute ospfv3 metric 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_deleted(self): set_module_args( dict( state="deleted", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.2.13.0/24"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng")], ), ], ), ), ) commands = [ "delete protocols bgp 65536 address-family ipv4-unicast", "delete protocols bgp 65536 address-family ipv6-unicast", ] self.execute_module(changed=True, commands=commands) diff --git a/tests/unit/modules/network/vyos/test_vyos_bgp_address_family14.py b/tests/unit/modules/network/vyos/test_vyos_bgp_address_family14.py index 447e324f..633b8263 100644 --- a/tests/unit/modules/network/vyos/test_vyos_bgp_address_family14.py +++ b/tests/unit/modules/network/vyos/test_vyos_bgp_address_family14.py @@ -1,959 +1,1146 @@ # (c) 2016 Red Hat Inc. # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . # Make coding more python3-ish from __future__ import absolute_import, division, print_function + __metaclass__ = type from unittest.mock import patch from ansible_collections.vyos.vyos.plugins.modules import vyos_bgp_address_family from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture class TestVyosBgpafModule14(TestVyosModule): module = vyos_bgp_address_family def setUp(self): super(TestVyosBgpafModule14, self).setUp() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", ) self.get_resource_connection_config = self.mock_get_resource_connection_config.start() self.mock_execute_show_command = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts." + "bgp_address_family.bgp_address_family.Bgp_address_familyFacts.get_device_data", ) self.execute_show_command = self.mock_execute_show_command.start() self.mock_get_os_version = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.bgp_address_family.bgp_address_family.get_os_version", ) self.test_version = "1.4" self.get_os_version = self.mock_get_os_version.start() self.get_os_version.return_value = self.test_version self.mock_facts_get_os_version = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_address_family.bgp_address_family.get_os_version", ) self.get_facts_os_version = self.mock_facts_get_os_version.start() self.get_facts_os_version.return_value = self.test_version self.maxDiff = None def tearDown(self): super(TestVyosBgpafModule14, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_execute_show_command.stop() self.mock_get_os_version.stop() self.mock_facts_get_os_version.stop() def load_fixtures(self, commands=None, filename=None): if filename is None: filename = "vyos_bgp_address_family_config_14.cfg" def load_from_file(*args, **kwargs): output = load_fixture(filename) return output self.execute_show_command.side_effect = load_from_file def test_vyos_bgp_address_family_merged_idempotent(self): set_module_args( dict( config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_merged(self): set_module_args( dict( config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", summary_only=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv4", filter_list=[ dict(action="export", path_list="list01"), ], capability=dict(orf="send"), ), ], ), ], ), ), ) commands = [ "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-setipv4-unicast aggregate-address 192.0.2.0/24 summary-only", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", "set protocols bgp neighbor 203.0.113.5 address-family ipv4-unicast filter-list export list01", "set protocols bgp neighbor 203.0.113.5 address-family ipv4-unicast capability prefix-list send", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast distribute-list export 10", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast route-server-client", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_replaced_idempotent(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_replaced(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", summary_only=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="import", route_map="map01")], ), dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv4", filter_list=[ dict(action="export", path_list="list01"), ], capability=dict(orf="send"), ), ], ), ], ), ), ) commands = [ "delete protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged", "delete protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration", "delete protocols bgp address-family ipv6-unicast redistribute ripng", "delete protocols bgp address-family ipv4-unicast network 192.2.13.0/24", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 summary-only", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", "set protocols bgp neighbor 192.10.21.25 address-family ipv4-unicast route-map import map01", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast distribute-list export 10", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast route-server-client", "set protocols bgp neighbor 203.0.113.5 address-family ipv4-unicast filter-list export list01", "set protocols bgp neighbor 203.0.113.5 address-family ipv4-unicast capability prefix-list send", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_overridden_idempotent(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_overridden(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.10.21.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="import", route_map="map01")], ), dict( afi="ipv6", distribute_list=[dict(action="export", acl=10)], route_server_client=True, ), ], ), ], ), ), ) commands = [ "delete protocols bgp neighbor 203.0.113.5 address-family", "delete protocols bgp neighbor 192.0.2.25 address-family", "delete protocols bgp address-family ipv6-unicast redistribute ripng", "delete protocols bgp address-family ipv4 aggregate-address", "delete protocols bgp address-family ipv4-unicast network 192.2.13.0/24", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", "set protocols bgp neighbor 192.10.21.25 address-family ipv4-unicast route-map import map01", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast distribute-list export 10", "set protocols bgp neighbor 192.10.21.25 address-family ipv6-unicast route-server-client", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_deleted(self): set_module_args( dict( state="deleted", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", ), ], ), dict( neighbor_address="203.0.113.5", ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv4-unicast", "delete protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast", "delete protocols bgp neighbor 203.0.113.5 address-family", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_rendered(self): set_module_args( dict( state="rendered", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) rendered_cmds = [ "set protocols bgp system-as 65536", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] result = self.execute_module(changed=False) self.assertEqual( sorted(result["rendered"]), sorted(rendered_cmds), result["rendered"], ) def test_vyos_bgp_address_family_parsed(self): commands = [ "set protocols bgp system-as 65536", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] parsed_str = "\n".join(commands) set_module_args(dict(running_config=parsed_str, state="parsed")) result = self.execute_module(changed=False) parsed_list = { "as_number": 65536, "address_family": [ { "afi": "ipv4", "networks": [ {"prefix": "192.1.13.0/24", "route_map": "map01"}, {"prefix": "192.2.13.0/24", "backdoor": True}, ], "aggregate_address": [{"prefix": "192.0.2.0/24", "as_set": True}], }, { "afi": "ipv6", "redistribute": [{"protocol": "ripng", "metric": 20}], }, ], "neighbors": [ { "neighbor_address": "192.0.2.25", "address_family": [ {"afi": "ipv4", "soft_reconfiguration": True}, ], }, { "neighbor_address": "203.0.113.5", "address_family": [ { "afi": "ipv6", "attribute_unchanged": {"next_hop": True}, }, ], }, ], } self.assertEqual(sorted(parsed_list), sorted(result["parsed"])) def test_vyos_bgp_address_family_gathered(self): set_module_args(dict(state="gathered")) result = self.execute_module(changed=False) gather_list = { "as_number": 65536, "address_family": [ { "afi": "ipv4", "networks": [ {"prefix": "192.1.13.0/24", "route_map": "map01"}, {"prefix": "192.2.13.0/24", "backdoor": True}, ], "aggregate_address": [{"prefix": "192.0.2.0/24", "as_set": True}], }, { "afi": "ipv6", "redistribute": [{"protocol": "ripng", "metric": 20}], }, ], "neighbors": [ { "neighbor_address": "192.0.2.25", "address_family": [ {"afi": "ipv4", "soft_reconfiguration": True}, ], }, { "neighbor_address": "203.0.113.5", "address_family": [ { "afi": "ipv6", "attribute_unchanged": {"next_hop": True}, }, ], }, ], } self.assertEqual(sorted(gather_list), sorted(result["gathered"])) def test_vyos_bgp_address_family_replaced_asn(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_overridden_asn(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) self.execute_module(changed=False, commands=[]) def test_vyos_bgp_address_family_replaced_asn2(self): set_module_args( dict( state="replaced", config=dict( as_number=65540, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) commands = [ "set protocols bgp system-as 65540", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_overridden_asn2(self): set_module_args( dict( state="overridden", config=dict( as_number=65540, address_family=[ dict( afi="ipv4", aggregate_address=[dict(prefix="192.0.2.0/24", as_set=True)], networks=[ dict(prefix="192.1.13.0/24", route_map="map01"), dict(prefix="192.2.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng", metric=20)], ), ], neighbors=[ dict( neighbor_address="192.0.2.25", address_family=[ dict( afi="ipv4", route_map=[dict(action="export", route_map="map01")], soft_reconfiguration=True, ), ], ), dict( neighbor_address="203.0.113.5", address_family=[ dict( afi="ipv6", attribute_unchanged=dict(next_hop=True), ), ], ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv4 aggregate-address", "delete protocols bgp address-family ipv4 network", "delete protocols bgp address-family ipv6 redistribute", "delete protocols bgp neighbor 192.0.2.25 address-family", "delete protocols bgp neighbor 203.0.113.5 address-family", "set protocols bgp system-as 65540", "set protocols bgp address-family ipv4-unicast aggregate-address 192.0.2.0/24 as-set", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 route-map map01", "set protocols bgp address-family ipv4-unicast network 192.2.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ripng metric 20", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast route-map export map01", "set protocols bgp neighbor 192.0.2.25 address-family ipv4-unicast soft-reconfiguration inbound", "set protocols bgp neighbor 203.0.113.5 address-family ipv6-unicast attribute-unchanged next-hop", ] self.execute_module(changed=True, commands=commands) + def test_vyos_bgp_af_neighbor_vpn_rendered(self): + set_module_args( + dict( + config=dict( + as_number=65536, + neighbors=[ + dict( + neighbor_address="192.0.2.1", + address_family=[ + dict( + afi="ipv4-vpn", + route_reflector_client=True, + nexthop_self=True, + ), + dict(afi="ipv6-vpn", route_reflector_client=True), + ], + ), + ], + ), + state="rendered", + ), + ) + self.assertEqual( + sorted(self.execute_module(changed=False)["rendered"]), + [ + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn nexthop-self", + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn route-reflector-client", + "set protocols bgp neighbor 192.0.2.1 address-family ipv6-vpn route-reflector-client", + "set protocols bgp system-as 65536", + ], + ) + + def test_vyos_bgp_af_neighbor_vpn_parsed(self): + set_module_args( + dict( + running_config="set protocols bgp system-as 65536\n" + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-labeled-unicast\n" + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast route-reflector-client\n" + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn nexthop-self\n" + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn route-reflector-client", + state="parsed", + ), + ) + self.assertEqual( + self.execute_module(changed=False)["parsed"], + { + "as_number": 65536, + "neighbors": [ + { + "neighbor_address": "192.0.2.1", + "address_family": [ + {"afi": "ipv4", "route_reflector_client": True}, + { + "afi": "ipv4-vpn", + "nexthop_self": True, + "route_reflector_client": True, + }, + ], + }, + ], + }, + ) + + def test_vyos_bgp_af_neighbor_bare_family_rendered(self): + set_module_args( + dict( + config=dict( + as_number=65536, + neighbors=[ + dict( + neighbor_address="192.0.2.1", + address_family=[dict(afi="ipv4"), dict(afi="ipv4-vpn")], + ), + ], + ), + state="rendered", + ), + ) + self.assertEqual( + sorted(self.execute_module(changed=False)["rendered"]), + [ + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast", + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-vpn", + "set protocols bgp system-as 65536", + ], + ) + + def test_vyos_bgp_af_neighbor_bare_family_replaced(self): + set_module_args( + dict( + config=dict( + as_number=65536, + neighbors=[ + dict(neighbor_address="192.0.2.1", address_family=[dict(afi="ipv6")]), + ], + ), + state="replaced", + ), + ) + self.execute_module( + changed=True, + commands=[ + "delete protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast", + "set protocols bgp neighbor 192.0.2.1 address-family ipv6-unicast", + ], + filename="vyos_bgp_address_family_bare_config_14.cfg", + ) + + def test_vyos_bgp_af_neighbor_default_originate_rendered(self): + set_module_args( + dict( + config=dict( + as_number=65536, + neighbors=[ + dict( + neighbor_address="192.0.2.1", + address_family=[dict(afi="ipv4", default_originate=True)], + ), + dict( + neighbor_address="192.0.2.2", + address_family=[dict(afi="ipv4", default_originate="RM-DEF")], + ), + ], + ), + state="rendered", + ), + ) + self.assertEqual( + sorted(self.execute_module(changed=False)["rendered"]), + [ + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast default-originate", + "set protocols bgp neighbor 192.0.2.2 address-family ipv4-unicast default-originate route-map RM-DEF", + "set protocols bgp system-as 65536", + ], + ) + + def test_vyos_bgp_af_neighbor_default_originate_parsed(self): + set_module_args( + dict( + running_config="set protocols bgp system-as 65536\n" + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast default-originate\n" + "set protocols bgp neighbor 192.0.2.2 address-family ipv4-unicast default-originate route-map RM-DEF", + state="parsed", + ), + ) + self.assertEqual( + self.execute_module(changed=False)["parsed"], + { + "as_number": 65536, + "neighbors": [ + { + "neighbor_address": "192.0.2.1", + "address_family": [{"afi": "ipv4", "default_originate": True}], + }, + { + "neighbor_address": "192.0.2.2", + "address_family": [{"afi": "ipv4", "default_originate": "RM-DEF"}], + }, + ], + }, + ) + + def test_vyos_bgp_af_neighbor_default_originate_route_map_to_plain(self): + set_module_args( + dict( + config=dict( + as_number=65536, + neighbors=[ + dict( + neighbor_address="192.0.2.1", + address_family=[dict(afi="ipv4", default_originate=True)], + ), + ], + ), + state="merged", + ), + ) + self.execute_module( + changed=True, + commands=[ + "delete protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast default-originate", + "set protocols bgp neighbor 192.0.2.1 address-family ipv4-unicast default-originate", + ], + filename="vyos_bgp_address_family_default_originate_config_14.cfg", + ) + class TestVyosBgpafOpsModule14(TestVyosModule): module = vyos_bgp_address_family def setUp(self): super(TestVyosBgpafOpsModule14, self).setUp() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", ) self.get_resource_connection_config = self.mock_get_resource_connection_config.start() self.mock_execute_show_command = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts." + "bgp_address_family.bgp_address_family.Bgp_address_familyFacts.get_device_data", ) self.execute_show_command = self.mock_execute_show_command.start() self.mock_get_os_version = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.bgp_address_family.bgp_address_family.get_os_version", ) self.test_version = "1.4" self.get_os_version = self.mock_get_os_version.start() self.get_os_version.return_value = self.test_version self.mock_facts_get_os_version = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_address_family.bgp_address_family.get_os_version", ) self.get_facts_os_version = self.mock_facts_get_os_version.start() self.get_facts_os_version.return_value = self.test_version self.maxDiff = None def tearDown(self): super(TestVyosBgpafOpsModule14, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_execute_show_command.stop() self.mock_get_os_version.stop() self.mock_facts_get_os_version.stop() def load_fixtures(self, commands=None, filename=None): if filename is None: filename = "vyos_bgp_af_ops_config_14.cfg" def load_from_file(*args, **kwargs): output = load_fixture(filename) return output self.execute_show_command.side_effect = load_from_file def test_vyos_bgp_address_family_merged(self): set_module_args( dict( config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.3.13.0/24", backdoor=True), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ospfv3", metric=20)], ), ], ), ), ) commands = [ "set protocols bgp address-family ipv4-unicast network 192.3.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_replaced(self): set_module_args( dict( state="replaced", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", backdoor=True), ], redistribute=[ dict(protocol="ospf", metric=25), ], ), dict( afi="ipv6", redistribute=[ dict(protocol="ospfv3", metric=20), dict(protocol="ripng"), ], ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv6-unicast redistribute ripng metric", "delete protocols bgp address-family ipv4-unicast network 192.2.13.0/24", "delete protocols bgp address-family ipv4-unicast redistribute rip", "set protocols bgp address-family ipv4-unicast redistribute ospf metric 25", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_overridden(self): set_module_args( dict( state="overridden", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.1.13.0/24", backdoor=True), ], redistribute=[ dict(protocol="ospf", metric=25), ], ), dict( afi="ipv6", redistribute=[ dict(protocol="ospfv3", metric=20), dict(protocol="ripng"), ], ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv6-unicast redistribute ripng metric", "delete protocols bgp address-family ipv4-unicast network 192.2.13.0/24", "delete protocols bgp address-family ipv4-unicast redistribute rip", "set protocols bgp address-family ipv4-unicast redistribute ospf metric 25", "set protocols bgp address-family ipv4-unicast network 192.1.13.0/24 backdoor", "set protocols bgp address-family ipv6-unicast redistribute ospfv3 metric 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_bgp_address_family_deleted(self): set_module_args( dict( state="deleted", config=dict( as_number=65536, address_family=[ dict( afi="ipv4", networks=[ dict(prefix="192.2.13.0/24"), ], ), dict( afi="ipv6", redistribute=[dict(protocol="ripng")], ), ], ), ), ) commands = [ "delete protocols bgp address-family ipv4-unicast", "delete protocols bgp address-family ipv6-unicast", ] self.execute_module(changed=True, commands=commands)