diff --git a/changelogs/fragments/cdp_fix.yaml b/changelogs/fragments/cdp_fix.yaml new file mode 100644 index 000000000..127a059f2 --- /dev/null +++ b/changelogs/fragments/cdp_fix.yaml @@ -0,0 +1,3 @@ +bugfixes: + - cisco.nxos.nxos_l2_interfaces - Fix cdp_enable config parsing. + - cisco.nxos.nxos_l3_interfaces - Improved the code logic for handling redirects. diff --git a/plugins/module_utils/network/nxos/config/l2_interfaces/l2_interfaces.py b/plugins/module_utils/network/nxos/config/l2_interfaces/l2_interfaces.py index e2b5ce845..e3b0876b9 100644 --- a/plugins/module_utils/network/nxos/config/l2_interfaces/l2_interfaces.py +++ b/plugins/module_utils/network/nxos/config/l2_interfaces/l2_interfaces.py @@ -59,6 +59,7 @@ def __init__(self, module): "beacon", "link_flap.error_disable", "cdp_enable", + "no_cdp_enable", ] def execute_module(self): @@ -107,6 +108,12 @@ def _compare(self, want, have): for the L2_interfaces network resource. """ begin = len(self.commands) + want_without_name = want.copy() + want_without_name.pop("name", None) + pre_pop_want = bool(want_without_name) + want_cdp = want.pop("cdp_enable", None) + have_cdp = have.pop("cdp_enable", None) + self.handle_cdp(want_cdp, have_cdp, "cdp_enable", pre_pop_want) self.compare(parsers=self.parsers, want=want, have=have) self._compare_lists(want, have) if len(self.commands) != begin: @@ -175,3 +182,16 @@ def process_list_attrs(self, param): vlanList = val.get("trunk").get(vlan, []) if vlanList and vlanList != "none": val["trunk"][vlan] = vlan_range_to_list(val.get("trunk").get(vlan)) + + def handle_cdp(self, want_cdp, have_cdp, parser, want): + if want_cdp is None and have_cdp is None: + if self.state == "replaced" or (self.state == "overridden" and want): + self.addcmd({parser: True}, parser, True) + else: + if want_cdp is True and have_cdp is False: + self.addcmd({parser: want_cdp}, parser, not want_cdp) + elif want_cdp is False and have_cdp is None: + self.addcmd({parser: not want_cdp}, parser, not want_cdp) + elif want_cdp is None and have_cdp is False: + if self.state in ["overridden", "deleted"] and not want: + self.addcmd({parser: not have_cdp}, parser, have_cdp) diff --git a/plugins/module_utils/network/nxos/config/l3_interfaces/l3_interfaces.py b/plugins/module_utils/network/nxos/config/l3_interfaces/l3_interfaces.py index 190ffb4e1..24e62ff51 100644 --- a/plugins/module_utils/network/nxos/config/l3_interfaces/l3_interfaces.py +++ b/plugins/module_utils/network/nxos/config/l3_interfaces/l3_interfaces.py @@ -241,12 +241,12 @@ def compare_lists(self, wanted, haved, parser): def handle_redirects(self, want_redirects, have_redirects, parser, want): if want_redirects is None and have_redirects is None: if self.state == "replaced" or (self.state == "overridden" and want): - self.addcmd({"redirects": True}, parser, True) + self.addcmd({parser: True}, parser, True) else: if want_redirects is True and have_redirects is False: - self.addcmd({"redirects": want_redirects}, parser, not want_redirects) + self.addcmd({parser: want_redirects}, parser, not want_redirects) elif want_redirects is False and have_redirects is None: - self.addcmd({"redirects": not want_redirects}, parser, not want_redirects) + self.addcmd({parser: not want_redirects}, parser, not want_redirects) elif want_redirects is None and have_redirects is False: if self.state in ["overridden", "deleted"] and not want: - self.addcmd({"redirects": not have_redirects}, parser, have_redirects) + self.addcmd({parser: not have_redirects}, parser, have_redirects) diff --git a/plugins/module_utils/network/nxos/rm_templates/l2_interfaces.py b/plugins/module_utils/network/nxos/rm_templates/l2_interfaces.py index 73b5b0aff..59b0cc685 100644 --- a/plugins/module_utils/network/nxos/rm_templates/l2_interfaces.py +++ b/plugins/module_utils/network/nxos/rm_templates/l2_interfaces.py @@ -150,13 +150,27 @@ def __init__(self, lines=None, module=None): "name": "cdp_enable", "getval": re.compile( r""" - \s+cdp\s+(?Penable) + \s+cdp\senable $""", re.VERBOSE, ), "setval": "cdp enable", "result": { '{{ name }}': { - 'cdp_enable': "{{ True if cdp_enable }}", + 'cdp_enable': True, + }, + }, + }, + { + "name": "no_cdp_enable", + "getval": re.compile( + r""" + \s+no\scdp\senable + $""", re.VERBOSE, + ), + "setval": "no cdp enable", + "result": { + '{{ name }}': { + 'cdp_enable': False, }, }, }, diff --git a/tests/integration/targets/nxos_l2_interfaces/tests/common/_remove_config.yaml b/tests/integration/targets/nxos_l2_interfaces/tests/common/_remove_config.yaml index 37d461651..0b063b998 100644 --- a/tests/integration/targets/nxos_l2_interfaces/tests/common/_remove_config.yaml +++ b/tests/integration/targets/nxos_l2_interfaces/tests/common/_remove_config.yaml @@ -1,9 +1,18 @@ --- - name: Cleanup ignore_errors: true + when: ansible_connection == 'ansible.netcommon.network_cli' cisco.nxos.nxos_config: lines: - "default interface {{ nxos_int1 }}" - "default interface {{ nxos_int2 }}" - vars: - ansible_connection: ansible.netcommon.network_cli + +- name: Cleanup for httpapi + when: ansible_connection == 'ansible.netcommon.httpapi' + ignore_errors: true + cisco.nxos.nxos_config: + lines: + - "{{ item }}" + loop: + - "default interface {{ nxos_int1 }}" + - "default interface {{ nxos_int2 }}" diff --git a/tests/integration/targets/nxos_l2_interfaces/tests/common/empty_config.yaml b/tests/integration/targets/nxos_l2_interfaces/tests/common/empty_config.yaml index 0f222dbbf..39e3869ce 100644 --- a/tests/integration/targets/nxos_l2_interfaces/tests/common/empty_config.yaml +++ b/tests/integration/targets/nxos_l2_interfaces/tests/common/empty_config.yaml @@ -1,6 +1,6 @@ --- - ansible.builtin.debug: - msg: START nxos_lacp empty_config integration tests on connection={{ ansible_connection }} + msg: START nxos_l2_interfaces empty_config integration tests on connection={{ ansible_connection }} - name: Merged with empty configuration should give appropriate error message register: result diff --git a/tests/integration/targets/nxos_l2_interfaces/tests/common/overridden.yaml b/tests/integration/targets/nxos_l2_interfaces/tests/common/overridden.yaml index 077914d28..8dfdce65e 100644 --- a/tests/integration/targets/nxos_l2_interfaces/tests/common/overridden.yaml +++ b/tests/integration/targets/nxos_l2_interfaces/tests/common/overridden.yaml @@ -52,9 +52,11 @@ - result.changed == true - "'interface {{ test_int1 }}' in result.commands" - "'no switchport trunk allowed vlan' in result.commands" + - "'no cdp enable' in result.commands" - "'interface {{ test_int2 }}' in result.commands" - "'switchport access vlan 6' in result.commands" - "'switchport trunk allowed vlan 10-12' in result.commands" + - "'no cdp enable' in result.commands" - name: Gather l2_interfaces post facts cisco.nxos.nxos_facts: *id001 diff --git a/tests/integration/targets/nxos_l2_interfaces/tests/common/replaced.yaml b/tests/integration/targets/nxos_l2_interfaces/tests/common/replaced.yaml index 53e1f4cb3..983d9887c 100644 --- a/tests/integration/targets/nxos_l2_interfaces/tests/common/replaced.yaml +++ b/tests/integration/targets/nxos_l2_interfaces/tests/common/replaced.yaml @@ -71,11 +71,14 @@ - "'interface {{ test_int1 }}' in result.commands" - "'switchport access vlan 8' in result.commands" - "'switchport trunk allowed vlan 10-12' in result.commands" + - "'no cdp enable' in result.commands" - "'interface {{ test_int2 }}' in result.commands" - "'no switchport trunk native vlan 15' in result.commands" + - "'no cdp enable' in result.commands" - "'interface {{ test_int3 }}' in result.commands" - "'switchport trunk allowed vlan none' in result.commands" - - result.commands|length == 7 + - "'no cdp enable' in result.commands" + - result.commands|length == 10 - name: Gather l2_interfaces post facts cisco.nxos.nxos_facts: *id001 diff --git a/tests/integration/targets/nxos_l2_interfaces/tests/common/rtt.yaml b/tests/integration/targets/nxos_l2_interfaces/tests/common/rtt.yaml index 5ef0b101c..f09b92f7b 100644 --- a/tests/integration/targets/nxos_l2_interfaces/tests/common/rtt.yaml +++ b/tests/integration/targets/nxos_l2_interfaces/tests/common/rtt.yaml @@ -24,9 +24,11 @@ trunk: native_vlan: 10 allowed_vlans: 2,4,15 + cdp_enable: false - name: "{{ nxos_int2 }}" access: vlan: 30 + cdp_enable: false state: merged tags: base_config @@ -45,13 +47,16 @@ - name: "{{ nxos_int1 }}" trunk: native_vlan: 20 + cdp_enable: true - name: "{{ nxos_int2 }}" access: vlan: 31 + cdp_enable: true - name: "{{ nxos_int3 }}" trunk: native_vlan: 20 allowed_vlans: 5-10, 15 + cdp_enable: true state: overridden - ansible.builtin.assert: diff --git a/tests/unit/modules/network/nxos/test_nxos_l2_interfaces.py b/tests/unit/modules/network/nxos/test_nxos_l2_interfaces.py index 0309ce39e..44adf1ee0 100644 --- a/tests/unit/modules/network/nxos/test_nxos_l2_interfaces.py +++ b/tests/unit/modules/network/nxos/test_nxos_l2_interfaces.py @@ -184,6 +184,7 @@ def test_l2_interfaces_merged(self): default interface Ethernet1/6 interface Ethernet1/6 switchport + no cdp enable """, ) @@ -196,6 +197,7 @@ def test_l2_interfaces_merged(self): "trunk": { "allowed_vlans": "10-12", }, + "cdp_enable": True, }, ], ), @@ -203,6 +205,7 @@ def test_l2_interfaces_merged(self): expected_commands = [ "interface Ethernet1/6", + "cdp enable", "switchport mode trunk", "switchport trunk allowed vlan 10-12", ] @@ -252,6 +255,7 @@ def test_l2_interfaces_replaced(self): "trunk": { "allowed_vlans": "none", }, + "cdp_enable": True, }, ], state="replaced", @@ -260,9 +264,11 @@ def test_l2_interfaces_replaced(self): expected_commands = [ "interface Ethernet1/6", + "no cdp enable", "switchport access vlan 8", "switchport trunk allowed vlan 10-12", "interface Ethernet1/7", + "no cdp enable", "no switchport trunk native vlan 15", "interface Ethernet1/8", "switchport trunk allowed vlan none", @@ -305,6 +311,7 @@ def test_l2_interfaces_overridden(self): "interface Ethernet1/6", "no switchport trunk allowed vlan", "interface Ethernet1/7", + "no cdp enable", "switchport access vlan 6", "switchport trunk allowed vlan 10-12", ]