From ca5467b5f2b43441f8ec44ab13618b739545b06f Mon Sep 17 00:00:00 2001 From: Chris Van Heuveln Date: Wed, 27 Feb 2019 03:50:50 -0500 Subject: [PATCH] nxos_hsrp: fix 'sh_preempt': (#52858) * nxos_hsrp: fix 'sh_preempt': Some older nxos images fail to set this attr value. This fix checks for unknown enum and issues a second (unstructured) call to the device to get the data. * add whitespace for pep8 (cherry picked from commit 5dc65d0dfcc0a416ed1f4b1c4a6a014fb994f949) --- lib/ansible/modules/network/nxos/nxos_hsrp.py | 15 +++++++++++++++ .../targets/nxos_hsrp/tests/common/sanity.yaml | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_hsrp.py b/lib/ansible/modules/network/nxos/nxos_hsrp.py index a108fbe56ca..2135fe20cad 100644 --- a/lib/ansible/modules/network/nxos/nxos_hsrp.py +++ b/lib/ansible/modules/network/nxos/nxos_hsrp.py @@ -211,6 +211,8 @@ def get_hsrp_group(group, interface, module): try: body = run_commands(module, [command])[0] hsrp_table = body['TABLE_grp_detail']['ROW_grp_detail'] + if 'unknown enum:' in str(hsrp_table): + hsrp_table = get_hsrp_group_unknown_enum(module, command, hsrp_table) except (AttributeError, IndexError, TypeError, KeyError): return {} @@ -239,6 +241,19 @@ def get_hsrp_group(group, interface, module): return hsrp +def get_hsrp_group_unknown_enum(module, command, hsrp_table): + '''Some older NXOS images fail to set the attr values when using structured output and + instead set the values to . This fallback method is a workaround that + uses an unstructured (text) request to query the device a second time. + 'sh_preempt' is currently the only attr affected. Add checks for other attrs as needed. + ''' + if 'unknown enum:' in hsrp_table['sh_preempt']: + cmd = {'output': 'text', 'command': command.split('|')[0]} + out = run_commands(module, cmd)[0] + hsrp_table['sh_preempt'] = 'enabled' if ('may preempt' in out) else 'disabled' + return hsrp_table + + def get_commands_remove_hsrp(group, interface): commands = ['interface {0}'.format(interface), 'no hsrp {0}'.format(group)] return commands diff --git a/test/integration/targets/nxos_hsrp/tests/common/sanity.yaml b/test/integration/targets/nxos_hsrp/tests/common/sanity.yaml index 83e648b50d4..cb09438c4c6 100644 --- a/test/integration/targets/nxos_hsrp/tests/common/sanity.yaml +++ b/test/integration/targets/nxos_hsrp/tests/common/sanity.yaml @@ -9,7 +9,7 @@ - block: - name: "Enable feature hsrp" - nxos_feature: + nxos_feature: feature: hsrp provider: "{{ connection }}" state: enabled @@ -155,7 +155,7 @@ always: - name: "Disable feature hsrp" - nxos_feature: + nxos_feature: feature: hsrp provider: "{{ connection }}" state: disabled