diff --git a/lib/ansible/modules/network/onyx/onyx_bgp.py b/lib/ansible/modules/network/onyx/onyx_bgp.py index eb3a19326a..00c0b3351a 100644 --- a/lib/ansible/modules/network/onyx/onyx_bgp.py +++ b/lib/ansible/modules/network/onyx/onyx_bgp.py @@ -49,6 +49,12 @@ options: - BGP state. default: present choices: ['present', 'absent'] + purge: + description: + - will remove all neighbors when it is True. + type: bool + default: false + version_added: 2.8 """ EXAMPLES = """ @@ -59,6 +65,7 @@ EXAMPLES = """ neighbors: - remote_as: 321 neighbor: 10.3.3.4 + purge: True state: present networks: - 172.16.1.0/24 @@ -93,6 +100,7 @@ class OnyxBgpModule(BaseOnyxModule): r'^\s+router bgp\s+(\d+).*neighbor\s+(\S+)\s+remote\-as\s+(\S+).*') NETWORK_REGEX = re.compile( r'^\s+router bgp\s+(\d+).*network\s+(\S+)\s+(\S+).*') + _purge = False def init_module(self): """ initialize module @@ -108,6 +116,7 @@ class OnyxBgpModule(BaseOnyxModule): options=neighbor_spec), networks=dict(type='list', elements='str'), state=dict(choices=['present', 'absent'], default='present'), + purge=dict(default=False, type='bool'), ) argument_spec = dict() @@ -126,6 +135,7 @@ class OnyxBgpModule(BaseOnyxModule): neighbors=req_neighbors, networks=module_params['networks']) neighbors = module_params['neighbors'] or list() + self._purge = module_params.get('purge', False) for neighbor_data in neighbors: req_neighbors.append( (neighbor_data['neighbor'], neighbor_data['remote_as'])) @@ -197,13 +207,10 @@ class OnyxBgpModule(BaseOnyxModule): def _generate_neighbors_cmds(self, as_number, bgp_removed): req_neighbors = self._required_config['neighbors'] curr_neighbors = self._current_config.get('neighbors', []) - if not bgp_removed: + if self._purge: for neighbor_data in curr_neighbors: - if neighbor_data not in req_neighbors: - (neighbor, remote_as) = neighbor_data - self._commands.append( - 'router bgp %s no neighbor %s remote-as %s' % - (as_number, neighbor, remote_as)) + (neighbor, remote_as) = neighbor_data + self._commands.append('router bgp %s no neighbor %s remote-as %s' % (as_number, neighbor, remote_as)) for neighbor_data in req_neighbors: if bgp_removed or neighbor_data not in curr_neighbors: diff --git a/test/units/modules/network/onyx/test_onyx_bgp.py b/test/units/modules/network/onyx/test_onyx_bgp.py index 75c7864de4..687693f08e 100644 --- a/test/units/modules/network/onyx/test_onyx_bgp.py +++ b/test/units/modules/network/onyx/test_onyx_bgp.py @@ -68,7 +68,8 @@ class TestOnyxBgpModule(TestOnyxModule): def test_bgp_del_neighbor(self): set_module_args(dict(as_number=172, - networks=['172.16.1.0/24'])) + networks=['172.16.1.0/24'], + purge=True)) commands = ['router bgp 172 no neighbor 10.2.3.4 remote-as 173'] self.execute_module(changed=True, commands=commands)