From d462a425f27186c60201b4d956daa9307b877988 Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Wed, 24 Jan 2018 23:16:33 +0100 Subject: [PATCH] Remove all config from interface if absent on eos_l2_interface (#35318) If the module is set to absent, read all possible configurations and remove them. i.e. not just do a 'no switchport', as leaving around other options make the logic handling harder. If the user wants to remove switchport, remove all config for the interface and finally push a 'no switchport'. Also, I needed to map all possible params from interface regardless if state is absent or present, so I can later detect what I need to remove in case of absent. --- .../modules/network/eos/eos_l2_interface.py | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/ansible/modules/network/eos/eos_l2_interface.py b/lib/ansible/modules/network/eos/eos_l2_interface.py index feb488a3b15..b94e6095f91 100644 --- a/lib/ansible/modules/network/eos/eos_l2_interface.py +++ b/lib/ansible/modules/network/eos/eos_l2_interface.py @@ -139,6 +139,18 @@ def map_obj_to_commands(updates, module): module.fail_json(msg='invalid interface {0}'.format(name)) if state == 'absent': + if obj_in_have['mode'] == 'access': + commands.append('no switchport access vlan {0}'.format(obj_in_have['access_vlan'])) + + if obj_in_have['mode'] == 'trunk': + commands.append('no switchport mode trunk') + + if obj_in_have['native_vlan']: + commands.append('no switchport trunk native vlan {0}'.format(obj_in_have['native_vlan'])) + + if obj_in_have['trunk_allowed_vlans']: + commands.append('no switchport trunk allowed vlan {0}'.format(obj_in_have['trunk_allowed_vlans'])) + if obj_in_have['state'] == 'present': commands.append('no switchport') else: @@ -207,14 +219,13 @@ def map_config_to_obj(module): 'state': state, } - if state == 'present': - obj['access_vlan'] = parse_config_argument(configobj, item, 'switchport access vlan') - obj['native_vlan'] = parse_config_argument(configobj, item, 'switchport trunk native vlan') - obj['trunk_allowed_vlans'] = parse_config_argument(configobj, item, 'switchport trunk allowed vlan') - if obj['access_vlan']: - obj['mode'] = 'access' - else: - obj['mode'] = 'trunk' + obj['access_vlan'] = parse_config_argument(configobj, item, 'switchport access vlan') + obj['native_vlan'] = parse_config_argument(configobj, item, 'switchport trunk native vlan') + obj['trunk_allowed_vlans'] = parse_config_argument(configobj, item, 'switchport trunk allowed vlan') + if obj['access_vlan']: + obj['mode'] = 'access' + else: + obj['mode'] = 'trunk' instances.append(obj)