New module to support BGP configuration management in IOS (#49121)

* ios_bgp initial push

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Added tests for ios_bgp

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Fixed docs

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Added space

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Fix Shippable Errors

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Fix Shippable Errors

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Add support for af_neighbor option

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Add support for af_neighbor option - 2

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Add support for af_neighbor option - 3

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Fix typo

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Refactor BGP

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Fix CI and previous reviews

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Add missing params documentation

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Remove previous tests

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Remove elements=dict from keys with type=list from args spec for element validation to pass

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Added function to validate input

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Fix sanity test failure

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Minor bug fixes

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Fix typo in fixture

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>

* Add integration tests

Signed-off-by: NilashishC <nilashishchakraborty8@gmail.com>
This commit is contained in:
Nilashish Chakraborty
2019-03-04 13:37:57 +05:30
committed by GitHub
parent bf3fc86437
commit 55bfa18c0c
18 changed files with 1801 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
---
testcase: "*"

View File

@@ -0,0 +1,2 @@
dependencies:
- prepare_ios_tests

View File

@@ -0,0 +1,16 @@
---
- name: collect all cli test cases
find:
paths: "{{ role_path }}/tests/cli"
patterns: "{{ testcase }}.yaml"
register: test_cases
delegate_to: localhost
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test cases (connection=network_cli)
include: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

View File

@@ -0,0 +1,2 @@
---
- { include: cli.yaml, tags: ['cli'] }

View File

@@ -0,0 +1,394 @@
- debug: msg="START ios cli/ios_bgp.yaml on connection={{ ansible_connection }}"
- name: Clear existing BGP config
ios_bgp:
operation: delete
ignore_errors: yes
- name: Configure BGP with AS 64496 and a router-id
ios_bgp: &config
operation: merge
config:
bgp_as: 64496
router_id: 192.0.2.2
register: result
- assert:
that:
- 'result.changed == true'
- "'router bgp 64496' in result.commands"
- "'bgp router-id 192.0.2.2' in result.commands"
- name: Configure BGP with AS 64496 and a router-id (idempotent)
ios_bgp: *config
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure BGP neighbors
ios_bgp: &nbr
operation: merge
config:
bgp_as: 64496
neighbors:
- neighbor: 192.0.2.10
remote_as: 64496
password: ansible
description: IBGP_NBR_1
ebgp_multihop: 100
timers:
keepalive: 300
holdtime: 360
min_neighbor_holdtime: 360
- neighbor: 192.0.2.15
remote_as: 64496
description: IBGP_NBR_2
ebgp_multihop: 150
register: result
- assert:
that:
- 'result.changed == true'
- "'router bgp 64496' in result.commands"
- "'neighbor 192.0.2.10 remote-as 64496' in result.commands"
- "'neighbor 192.0.2.10 description IBGP_NBR_1' in result.commands"
- "'neighbor 192.0.2.10 ebgp-multihop 100' in result.commands"
- "'neighbor 192.0.2.10 timers 300 360 360' in result.commands"
- "'neighbor 192.0.2.15 remote-as 64496' in result.commands"
- "'neighbor 192.0.2.15 description IBGP_NBR_2' in result.commands"
- "'neighbor 192.0.2.15 ebgp-multihop 150' in result.commands"
- name: Configure BGP neighbors (idempotent)
ios_bgp: *nbr
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure BGP neighbors with operation replace
ios_bgp: &nbr_rplc
operation: replace
config:
bgp_as: 64496
neighbors:
- neighbor: 192.0.2.15
remote_as: 64496
description: IBGP_NBR_2
ebgp_multihop: 150
- neighbor: 203.0.113.10
remote_as: 64511
description: EBGP_NBR_1
local_as: 64497
register: result
- assert:
that:
- 'result.changed == true'
- "'neighbor 203.0.113.10 remote-as 64511' in result.commands"
- "'neighbor 203.0.113.10 description EBGP_NBR_1' in result.commands"
- "'neighbor 203.0.113.10 local-as 64497' in result.commands"
- "'no neighbor 192.0.2.10' in result.commands"
- name: Configure BGP neighbors with operation replace (idempotent)
ios_bgp: *nbr_rplc
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure root-level networks for BGP
ios_bgp: &net
operation: merge
config:
bgp_as: 64496
networks:
- prefix: 203.0.113.0
masklen: 27
route_map: RMAP_1
- prefix: 203.0.113.32
masklen: 27
route_map: RMAP_2
register: result
- assert:
that:
- 'result.changed == True'
- "'router bgp 64496' in result.commands"
- "'network 203.0.113.0 mask 255.255.255.224 route-map RMAP_1' in result.commands"
- "'network 203.0.113.32 mask 255.255.255.224 route-map RMAP_2' in result.commands"
- name: Configure root-level networks for BGP (idempotent)
ios_bgp: *net
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure root-level networks for BGP with operation replace
ios_bgp: &net_rplc
operation: replace
config:
bgp_as: 64496
networks:
- prefix: 203.0.113.0
masklen: 27
route_map: RMAP_1
- prefix: 198.51.100.16
masklen: 28
register: result
- assert:
that:
- 'result.changed == True'
- "'router bgp 64496' in result.commands"
- "'network 198.51.100.16 mask 255.255.255.240' in result.commands"
- "'no network 203.0.113.32 mask 255.255.255.224 route-map RMAP_2' in result.commands"
- name: Configure root-level networks for BGP with operation replace (idempotent)
ios_bgp: *net_rplc
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure BGP neighbors under address family mode
ios_bgp: &af_nbr
operation: merge
config:
bgp_as: 64496
address_family:
- afi: ipv4
safi: unicast
neighbors:
- neighbor: 203.0.113.10
activate: yes
maximum_prefix: 250
advertisement_interval: 120
- neighbor: 192.0.2.15
activate: yes
route_reflector_client: True
register: result
- assert:
that:
- 'result.changed == true'
- "'router bgp 64496' in result.commands"
- "'address-family ipv4' in result.commands"
- "'neighbor 203.0.113.10 activate' in result.commands"
- "'neighbor 203.0.113.10 maximum-prefix 250' in result.commands"
- "'neighbor 203.0.113.10 advertisement-interval 120' in result.commands"
- "'neighbor 192.0.2.15 activate' in result.commands"
- "'neighbor 192.0.2.15 route-reflector-client' in result.commands"
- name: Configure BGP neighbors under address family mode (idempotent)
ios_bgp: *af_nbr
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure networks under address family
ios_bgp: &af_net
operation: merge
config:
bgp_as: 64496
address_family:
- afi: ipv4
safi: multicast
networks:
- prefix: 198.51.100.48
masklen: 28
route_map: RMAP_1
- prefix: 192.0.2.64
masklen: 27
- prefix: 203.0.113.160
masklen: 27
route_map: RMAP_2
- afi: ipv4
safi: unicast
networks:
- prefix: 198.51.100.64
masklen: 28
register: result
- assert:
that:
- 'result.changed == true'
- "'router bgp 64496' in result.commands"
- "'address-family ipv4 multicast' in result.commands"
- "'network 198.51.100.48 mask 255.255.255.240 route-map RMAP_1' in result.commands"
- "'network 192.0.2.64 mask 255.255.255.224' in result.commands"
- "'network 203.0.113.160 mask 255.255.255.224 route-map RMAP_2' in result.commands"
- "'exit-address-family' in result.commands"
- "'address-family ipv4' in result.commands"
- "'network 198.51.100.64 mask 255.255.255.240' in result.commands"
- "'exit-address-family' in result.commands"
- name: Configure networks under address family (idempotent)
ios_bgp: *af_net
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure networks under address family with operation replace
ios_bgp: &af_net_rplc
operation: replace
config:
bgp_as: 64496
address_family:
- afi: ipv4
safi: multicast
networks:
- prefix: 198.51.100.80
masklen: 28
- prefix: 192.0.2.64
masklen: 27
- prefix: 203.0.113.192
masklen: 27
- afi: ipv4
safi: unicast
networks:
- prefix: 198.51.100.64
masklen: 28
register: result
- assert:
that:
- 'result.changed == true'
- '"router bgp 64496" in result.commands'
- '"address-family ipv4 multicast" in result.commands'
- '"network 198.51.100.80 mask 255.255.255.240" in result.commands'
- '"network 203.0.113.192 mask 255.255.255.224" in result.commands'
- '"no network 198.51.100.48 mask 255.255.255.240 route-map RMAP_1" in result.commands'
- '"no network 203.0.113.160 mask 255.255.255.224 route-map RMAP_2" in result.commands'
- '"exit-address-family" in result.commands'
- name: Configure networks under address family with operation replace (idempotent)
ios_bgp: *af_net_rplc
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure redistribute information under address family mode
ios_bgp: &af_rdr
operation: merge
config:
bgp_as: 64496
address_family:
- afi: ipv4
safi: multicast
redistribute:
- protocol: ospf
id: 112
metric: 64
- protocol: eigrp
id: 233
metric: 256
register: result
- assert:
that:
- 'result.changed == true'
- "'router bgp 64496' in result.commands"
- "'address-family ipv4 multicast' in result.commands"
- "'redistribute ospf 112 metric 64' in result.commands"
- "'redistribute eigrp 233 metric 256' in result.commands"
- "'exit-address-family' in result.commands"
- name: Configure redistribute information under address family mode (idempotent)
ios_bgp: *af_rdr
register: result
- assert:
that:
- 'result.changed == false'
- name: Configure redistribute information under address family mode with operation replace
ios_bgp: &af_rdr_rplc
operation: replace
config:
bgp_as: 64496
address_family:
- afi: ipv4
safi: multicast
redistribute:
- protocol: ospf
id: 112
metric: 64
register: result
- assert:
that:
- 'result.changed == true'
- "'router bgp 64496' in result.commands"
- "'address-family ipv4 multicast' in result.commands"
- "'no redistribute eigrp 233' in result.commands"
- "'exit-address-family' in result.commands"
- name: Configure redistribute information under address family mode with operation replace (idempotent)
ios_bgp: *af_rdr_rplc
register: result
- assert:
that:
- 'result.changed == false'
- name: Override all the exisiting BGP config
ios_bgp:
operation: override
config:
bgp_as: 64497
router_id: 192.0.2.10
log_neighbor_changes: True
register: result
- assert:
that:
- 'result.changed == true'
- "'no router bgp 64496' in result.commands"
- "'router bgp 64497' in result.commands"
- "'bgp router-id 192.0.2.10' in result.commands"
- "'bgp log-neighbor-changes' in result.commands"
- name: Teardown
ios_bgp: &rm
operation: delete
register: result
- assert:
that:
- 'result.changed == true'
- "'no router bgp 64497' in result.commands"
- name: Teardown again (idempotent)
ios_bgp: *rm
register: result
- assert:
that:
- 'result.changed == false'
- debug: msg="END ios cli/ios_bgp.yaml on connection={{ ansible_connection }}"

View File

@@ -0,0 +1,24 @@
!
router bgp 64496
bgp router-id 192.0.2.1
bgp log-neighbor-changes
neighbor 192.51.100.1 remote-as 64496
neighbor 192.51.100.1 timers 120 360 360
neighbor 198.51.100.3 remote-as 64498
neighbor 203.0.113.5 remote-as 500
neighbor 203.0.113.5 description EBGP_PEER
!
address-family ipv4
network 192.0.2.0 mask 255.255.254.0 route-map RMAP_1
network 198.51.100.0 mask 255.255.255.128 route-map RMAP_2
redistribute static metric 100
redistribute eigrp metric 10 route-map RMAP_3
neighbor 203.0.113.1 remove-private-as
neighbor 203.0.113.1 maximum-prefix 100
exit-address-family
!
address-family ipv4 multicast
network 203.0.113.0 mask 255.255.255.224 route-map RMAP_1
network 192.0.2.0 mask 255.255.255.192 route-map RMAP_2
exit-address-family
!

View File

@@ -0,0 +1,207 @@
#
# (c) 2019, Ansible by Red Hat, inc
# 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
from ansible.module_utils.network.ios.providers.cli.config.bgp.process import Provider
from ansible.modules.network.ios import ios_bgp
from .ios_module import TestIosModule, load_fixture
class TestIosBgpModule(TestIosModule):
module = ios_bgp
def setUp(self):
super(TestIosBgpModule, self).setUp()
self._bgp_config = load_fixture('ios_bgp_config.cfg')
def test_ios_bgp(self):
obj = Provider(params=dict(config=dict(bgp_as=64496, router_id='192.0.2.2', networks=None,
address_family=None), operation='merge'))
commands = obj.render(self._bgp_config)
self.assertEqual(commands, ['router bgp 64496', 'bgp router-id 192.0.2.2', 'exit'])
def test_ios_bgp_idempotent(self):
obj = Provider(params=dict(config=dict(bgp_as=64496, router_id='192.0.2.1', networks=None,
address_family=None), operation='merge'))
commands = obj.render(self._bgp_config)
self.assertEqual(commands, [])
def test_ios_bgp_remove(self):
obj = Provider(params=dict(config=dict(bgp_as=64496, networks=None, address_family=None), operation='delete'))
commands = obj.render(self._bgp_config)
self.assertEqual(commands, ['no router bgp 64496'])
def test_ios_bgp_neighbor(self):
obj = Provider(params=dict(config=dict(bgp_as=64496, neighbors=[dict(neighbor='192.51.100.2', remote_as=64496)],
networks=None, address_family=None),
operation='merge'))
commands = obj.render(self._bgp_config)
self.assertEqual(commands, ['router bgp 64496', 'neighbor 192.51.100.2 remote-as 64496', 'exit'])
def test_ios_bgp_neighbor_idempotent(self):
obj = Provider(params=dict(config=dict(bgp_as=64496, neighbors=[dict(neighbor='192.51.100.1', remote_as=64496,
timers=dict(keepalive=120, holdtime=360,
min_neighbor_holdtime=360))],
networks=None, address_family=None),
operation='merge'))
commands = obj.render(self._bgp_config)
self.assertEqual(commands, [])
def test_ios_bgp_network(self):
obj = Provider(params=dict(config=dict(bgp_as=64496, networks=[dict(prefix='192.0.1.0', masklen=23, route_map='RMAP_1')],
address_family=None),
operation='merge'))
commands = obj.render(self._bgp_config)
self.assertEqual(sorted(commands), sorted(['router bgp 64496', 'network 192.0.1.0 mask 255.255.254.0 route-map RMAP_1',
'exit']))
def test_ios_bgp_network_idempotent(self):
obj = Provider(
params=dict(config=dict(bgp_as=64496, networks=[dict(prefix='192.0.2.0', masklen=23, route_map='RMAP_1'),
dict(prefix='198.51.100.0', masklen=25,
route_map='RMAP_2')],
address_family=None),
operation='merge'))
commands = obj.render(self._bgp_config)
self.assertEqual(commands, [])
def test_ios_bgp_address_family_redistribute(self):
rd_1 = dict(protocol='ospf', id='233', metric=90, route_map=None)
config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='unicast', redistribute=[rd_1])],
networks=None)
obj = Provider(params=dict(config=config, operation='merge'))
commands = obj.render(self._bgp_config)
cmd = ['router bgp 64496', 'address-family ipv4', 'redistribute ospf 233 metric 90',
'exit-address-family', 'exit']
self.assertEqual(sorted(commands), sorted(cmd))
def test_ios_bgp_address_family_redistribute_idempotent(self):
rd_1 = dict(protocol='eigrp', metric=10, route_map='RMAP_3', id=None)
rd_2 = dict(protocol='static', metric=100, id=None, route_map=None)
config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='unicast', redistribute=[rd_1, rd_2])],
networks=None)
obj = Provider(params=dict(config=config, operation='merge'))
commands = obj.render(self._bgp_config)
self.assertEqual(commands, [])
def test_ios_bgp_address_family_neighbors(self):
af_nbr_1 = dict(neighbor='192.51.100.1', maximum_prefix=35, activate=True)
af_nbr_2 = dict(neighbor='192.51.100.3', route_reflector_client=True, activate=True)
config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='multicast', neighbors=[af_nbr_1, af_nbr_2])],
networks=None)
obj = Provider(params=dict(config=config, operation='merge'))
commands = obj.render(self._bgp_config)
cmd = ['router bgp 64496', 'address-family ipv4 multicast', 'neighbor 192.51.100.1 activate',
'neighbor 192.51.100.1 maximum-prefix 35', 'neighbor 192.51.100.3 activate',
'neighbor 192.51.100.3 route-reflector-client', 'exit-address-family', 'exit']
self.assertEqual(sorted(commands), sorted(cmd))
def test_ios_bgp_address_family_neighbors_idempotent(self):
af_nbr_1 = dict(neighbor='203.0.113.1', remove_private_as=True, maximum_prefix=100)
config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='unicast', neighbors=[af_nbr_1])],
networks=None)
obj = Provider(params=dict(config=config, operation='merge'))
commands = obj.render(self._bgp_config)
self.assertEqual(commands, [])
def test_ios_bgp_address_family_networks(self):
net = dict(prefix='1.0.0.0', masklen=8, route_map='RMAP_1')
net2 = dict(prefix='192.168.1.0', masklen=24, route_map='RMAP_2')
config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='multicast', networks=[net, net2])],
networks=None)
obj = Provider(params=dict(config=config, operation='merge'))
commands = obj.render(self._bgp_config)
cmd = ['router bgp 64496', 'address-family ipv4 multicast', 'network 1.0.0.0 mask 255.0.0.0 route-map RMAP_1',
'network 192.168.1.0 mask 255.255.255.0 route-map RMAP_2', 'exit-address-family', 'exit']
self.assertEqual(sorted(commands), sorted(cmd))
def test_ios_bgp_address_family_networks_idempotent(self):
net = dict(prefix='203.0.113.0', masklen=27, route_map='RMAP_1')
net2 = dict(prefix='192.0.2.0', masklen=26, route_map='RMAP_2')
config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='multicast', networks=[net, net2])],
networks=None)
obj = Provider(params=dict(config=config, operation='merge'))
commands = obj.render(self._bgp_config)
self.assertEqual(commands, [])
def test_ios_bgp_operation_override(self):
net_1 = dict(prefix='1.0.0.0', masklen=8, route_map='RMAP_1')
net_2 = dict(prefix='192.168.1.0', masklen=24, route_map='RMAP_2')
nbr_1 = dict(neighbor='192.51.100.1', remote_as=64496, update_source='GigabitEthernet0/1')
nbr_2 = dict(neighbor='192.51.100.3', remote_as=64496, timers=dict(keepalive=300, holdtime=360,
min_neighbor_holdtime=360))
af_nbr_1 = dict(neighbor='192.51.100.1', maximum_prefix=35)
af_nbr_2 = dict(neighbor='192.51.100.3', route_reflector_client=True)
af_1 = dict(afi='ipv4', safi='unicast', neighbors=[af_nbr_1, af_nbr_2])
af_2 = dict(afi='ipv4', safi='multicast', networks=[net_1, net_2])
config = dict(bgp_as=64496, neighbors=[nbr_1, nbr_2], address_family=[af_1, af_2], networks=None)
obj = Provider(params=dict(config=config, operation='override'))
commands = obj.render(self._bgp_config)
cmd = ['no router bgp 64496', 'router bgp 64496', 'neighbor 192.51.100.1 remote-as 64496',
'neighbor 192.51.100.1 update-source GigabitEthernet0/1', 'neighbor 192.51.100.3 remote-as 64496',
'neighbor 192.51.100.3 timers 300 360 360', 'address-family ipv4',
'neighbor 192.51.100.1 maximum-prefix 35', 'neighbor 192.51.100.3 route-reflector-client',
'exit-address-family',
'address-family ipv4 multicast', 'network 1.0.0.0 mask 255.0.0.0 route-map RMAP_1',
'network 192.168.1.0 mask 255.255.255.0 route-map RMAP_2',
'exit-address-family', 'exit']
self.assertEqual(sorted(commands), sorted(cmd))
def test_ios_bgp_operation_replace(self):
rd = dict(protocol='ospf', id=223, metric=110, route_map=None)
net = dict(prefix='203.0.113.0', masklen=27, route_map='RMAP_1')
net2 = dict(prefix='192.0.2.0', masklen=26, route_map='RMAP_2')
af_1 = dict(afi='ipv4', safi='unicast', redistribute=[rd])
af_2 = dict(afi='ipv4', safi='multicast', networks=[net, net2])
config = dict(bgp_as=64496, address_family=[af_1, af_2], networks=None)
obj = Provider(params=dict(config=config, operation='replace'))
commands = obj.render(self._bgp_config)
cmd = ['router bgp 64496', 'address-family ipv4', 'redistribute ospf 223 metric 110',
'no redistribute eigrp',
'no redistribute static', 'exit-address-family', 'exit']
self.assertEqual(sorted(commands), sorted(cmd))
def test_ios_bgp_operation_replace_with_new_as(self):
rd = dict(protocol='ospf', id=223, metric=110, route_map=None)
af_1 = dict(afi='ipv4', safi='unicast', redistribute=[rd])
config = dict(bgp_as=64497, address_family=[af_1], networks=None)
obj = Provider(params=dict(config=config, operation='replace'))
commands = obj.render(self._bgp_config)
cmd = ['no router bgp 64496', 'router bgp 64497', 'address-family ipv4',
'redistribute ospf 223 metric 110',
'exit-address-family', 'exit']
self.assertEqual(sorted(commands), sorted(cmd))