Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nxos_hsrp: fix 'sh_preempt': <unknown enum:> #52858

Merged
merged 2 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/ansible/modules/network/nxos/nxos_hsrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down Expand Up @@ -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 <unknown enum>. 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]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does nxapi fail as well?
Does the command error out with structured json output? Or is it just for a specific key?

If the command fails for json request:
We actually have the logic already implemented in module_utils https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/network/nxos/nxos.py#L164.
When check_rc is set to retry_json, run_commands will send the command the request with non structured format if json fails.
You will just need to append the error string to if it is any different than Invalid Command or Ambiguous Command https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/network/nxos/nxos.py#L170

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this fails for nxapi as well.
The show command is successful with structured, it is only the sh_preempt key that is faulty.

[{u'TABLE_grp_detail': {u'ROW_grp_detail': {u'sh_active_router_addr': u'0.0.0.0',
 u'sh_group_num': u'100', u'sh_authentication_data': u'1234', u'sh_cur_hold_attr': u'sec',
 u'sh_ip_redund_name_attr': u'Default', u'sh_prio': u'25', u'sh_standby_router_addr': u'0.0.0.0',
 u'sh_vmac_attr': u'Default MAC', u'sh_cur_hello_attr': u'sec', u'sh_vip': u'192.0.2.2',
 u'sh_cur_hello': u'3', u'sh_vip_attr': u'config', u'sh_keystring_attr': u'unencrypted',
 u'sh_num_track_obj': u'0', u'sh_active_router_prio': u'0', u'sh_num_of_total_state_changes': u'0',
 u'sh_cfg_prio': u'25', u'sh_if_index': u'Ethernet1/1', u'sh_can_forward': u'unknown enum:<411125249>',
 u'sh_group_type': u'v4', u'sh_fwd_upper_threshold': u'25', u'sh_group_state': u'Initial',
 u'sh_cur_hold': u'10', u'sh_standby_router_prio': u'0', u'sh_fwd_lower_threshold': u'0', u'sh_group_version': u'v2',
 u'sh_preempt': u'unknown enum:<808728065>',   <-------------<< *****
 u'sh_vmac': u'0000.0c9f.f064', u'sh_ip_redund_name': u'hsrp-Eth1/1-100', 'sh_num_of_state_changes':
 u'0', u'sh_authentication_type': u'md5', u'sh_state_reason': u'(Interface Down)'}}}]

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
Expand Down
4 changes: 2 additions & 2 deletions test/integration/targets/nxos_hsrp/tests/common/sanity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

- block:
- name: "Enable feature hsrp"
nxos_feature:
nxos_feature:
feature: hsrp
provider: "{{ connection }}"
state: enabled
Expand Down Expand Up @@ -155,7 +155,7 @@

always:
- name: "Disable feature hsrp"
nxos_feature:
nxos_feature:
feature: hsrp
provider: "{{ connection }}"
state: disabled
Expand Down