From d55c0cf8dc2c043e487946c3dec95ed7342e4ac7 Mon Sep 17 00:00:00 2001 From: Mike Wiebe Date: Fri, 10 May 2019 01:24:51 -0400 Subject: [PATCH] nxos_vtp_*: Fixes n6k issues (#55737) * Add n6k support for nxos_vtp_domain * Add n6k support for nxos_vtp_version * Add n6k support for nxos_vtp_password * Fix shippable error --- .../modules/network/nxos/nxos_vtp_domain.py | 35 ++++++++++------- .../modules/network/nxos/nxos_vtp_password.py | 39 +++++++++++-------- .../modules/network/nxos/nxos_vtp_version.py | 38 +++++++++--------- .../nxos_vtp_domain/tests/common/sanity.yaml | 13 +++++++ .../tests/common/sanity.yaml | 13 +++++++ .../nxos_vtp_version/tests/common/sanity.yaml | 13 +++++++ 6 files changed, 101 insertions(+), 50 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_vtp_domain.py b/lib/ansible/modules/network/nxos/nxos_vtp_domain.py index c53092a22a0..4caa484b6cb 100644 --- a/lib/ansible/modules/network/nxos/nxos_vtp_domain.py +++ b/lib/ansible/modules/network/nxos/nxos_vtp_domain.py @@ -89,15 +89,12 @@ changed: from ansible.module_utils.network.nxos.nxos import load_config, run_commands from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args +from ansible.module_utils.network.nxos.nxos import get_capabilities from ansible.module_utils.basic import AnsibleModule import re -def execute_show_command(command, module, command_type='cli_show'): - if 'status' not in command: - output = 'json' - else: - output = 'text' +def execute_show_command(command, module, output='json'): cmds = [{ 'command': command, 'output': output, @@ -119,7 +116,7 @@ def flatten_list(command_lists): def get_vtp_config(module): command = 'show vtp status' body = execute_show_command( - command, module)[0] + command, module, 'text')[0] vtp_parsed = {} if body: @@ -148,15 +145,23 @@ def get_vtp_config(module): def get_vtp_password(module): command = 'show vtp password' - body = execute_show_command(command, module)[0] - try: - password = body['passwd'] - if password: - return str(password) - else: - return "" - except TypeError: - return "" + output = 'json' + cap = get_capabilities(module)['device_info']['network_os_model'] + if re.search(r'Nexus 6', cap): + output = 'text' + + body = execute_show_command(command, module, output)[0] + + if output == 'json': + password = body.get('passwd', '') + else: + password = '' + rp = r'VTP Password: (\S+)' + mo = re.search(rp, body) + if mo: + password = mo.group(1) + + return str(password) def main(): diff --git a/lib/ansible/modules/network/nxos/nxos_vtp_password.py b/lib/ansible/modules/network/nxos/nxos_vtp_password.py index 7ba62db7877..2609801eb3d 100644 --- a/lib/ansible/modules/network/nxos/nxos_vtp_password.py +++ b/lib/ansible/modules/network/nxos/nxos_vtp_password.py @@ -101,15 +101,12 @@ changed: from ansible.module_utils.network.nxos.nxos import load_config, run_commands from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args +from ansible.module_utils.network.nxos.nxos import get_capabilities from ansible.module_utils.basic import AnsibleModule import re -def execute_show_command(command, module, command_type='cli_show'): - if 'status' not in command: - output = 'json' - else: - output = 'text' +def execute_show_command(command, module, output='json'): cmds = [{ 'command': command, 'output': output, @@ -145,7 +142,7 @@ def get_vtp_config(module): command = 'show vtp status' body = execute_show_command( - command, module)[0] + command, module, 'text')[0] vtp_parsed = {} if body: @@ -174,15 +171,23 @@ def get_vtp_config(module): def get_vtp_password(module): command = 'show vtp password' - body = execute_show_command(command, module)[0] - try: - password = body['passwd'] - if password: - return str(password) - else: - return "" - except TypeError: - return "" + output = 'json' + cap = get_capabilities(module)['device_info']['network_os_model'] + if re.search(r'Nexus 6', cap): + output = 'text' + + body = execute_show_command(command, module, output)[0] + + if output == 'json': + password = body.get('passwd', '') + else: + password = '' + rp = r'VTP Password: (\S+)' + mo = re.search(rp, body) + if mo: + password = mo.group(1) + + return str(password) def main(): @@ -214,8 +219,8 @@ def main(): commands = [] if state == 'absent': - # if vtp_password is not set, some devices returns '\\' - if not existing['vtp_password'] or existing['vtp_password'] == '\\': + # if vtp_password is not set, some devices returns '\\' or the string 'None' + if not existing['vtp_password'] or existing['vtp_password'] == '\\' or existing['vtp_password'] == 'None': pass elif vtp_password is not None: if existing['vtp_password'] == proposed['vtp_password']: diff --git a/lib/ansible/modules/network/nxos/nxos_vtp_version.py b/lib/ansible/modules/network/nxos/nxos_vtp_version.py index e0508c9aa16..28a7b4588f2 100644 --- a/lib/ansible/modules/network/nxos/nxos_vtp_version.py +++ b/lib/ansible/modules/network/nxos/nxos_vtp_version.py @@ -84,18 +84,12 @@ changed: ''' from ansible.module_utils.network.nxos.nxos import load_config, run_commands from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args +from ansible.module_utils.network.nxos.nxos import get_capabilities from ansible.module_utils.basic import AnsibleModule - - -import re import re -def execute_show_command(command, module, command_type='cli_show'): - if 'status' not in command: - output = 'json' - else: - output = 'text' +def execute_show_command(command, module, output='json'): cmds = [{ 'command': command, 'output': output, @@ -117,7 +111,7 @@ def flatten_list(command_lists): def get_vtp_config(module): command = 'show vtp status' body = execute_show_command( - command, module)[0] + command, module, 'text')[0] vtp_parsed = {} if body: @@ -146,15 +140,23 @@ def get_vtp_config(module): def get_vtp_password(module): command = 'show vtp password' - body = execute_show_command(command, module)[0] - try: - password = body['passwd'] - if password: - return str(password) - else: - return "" - except TypeError: - return "" + output = 'json' + cap = get_capabilities(module)['device_info']['network_os_model'] + if re.search(r'Nexus 6', cap): + output = 'text' + + body = execute_show_command(command, module, output)[0] + + if output == 'json': + password = body.get('passwd', '') + else: + password = '' + rp = r'VTP Password: (\S+)' + mo = re.search(rp, body) + if mo: + password = mo.group(1) + + return str(password) def main(): diff --git a/test/integration/targets/nxos_vtp_domain/tests/common/sanity.yaml b/test/integration/targets/nxos_vtp_domain/tests/common/sanity.yaml index a3f90510bc3..4ba271ba1dc 100644 --- a/test/integration/targets/nxos_vtp_domain/tests/common/sanity.yaml +++ b/test/integration/targets/nxos_vtp_domain/tests/common/sanity.yaml @@ -3,7 +3,18 @@ - debug: msg="Using provider={{ connection.transport }}" when: ansible_connection == "local" +- set_fact: vtp_run="true" +- set_fact: vtp_run="false" + when: platform is search('N3K-F|N9K-F') + - block: + - name: disable feature vtp + nxos_feature: + feature: vtp + provider: "{{ connection }}" + state: disabled + ignore_errors: yes + - name: enable feature vtp nxos_feature: feature: vtp @@ -28,6 +39,8 @@ that: - "result.changed == false" + when: vtp_run + always: - name: disable feature vtp nxos_feature: diff --git a/test/integration/targets/nxos_vtp_password/tests/common/sanity.yaml b/test/integration/targets/nxos_vtp_password/tests/common/sanity.yaml index 6fe6ca2927b..2c7ac02d426 100644 --- a/test/integration/targets/nxos_vtp_password/tests/common/sanity.yaml +++ b/test/integration/targets/nxos_vtp_password/tests/common/sanity.yaml @@ -3,7 +3,18 @@ - debug: msg="Using provider={{ connection.transport }}" when: ansible_connection == "local" +- set_fact: vtp_run="true" +- set_fact: vtp_run="false" + when: platform is search('N3K-F|N9K-F') + - block: + - name: disable feature vtp + nxos_feature: + feature: vtp + provider: "{{ connection }}" + state: disabled + ignore_errors: yes + - name: enable feature vtp nxos_feature: feature: vtp @@ -49,6 +60,8 @@ - assert: *false + when: vtp_run + always: - name: disable feature vtp nxos_feature: diff --git a/test/integration/targets/nxos_vtp_version/tests/common/sanity.yaml b/test/integration/targets/nxos_vtp_version/tests/common/sanity.yaml index 126261b4ed6..08814d60330 100644 --- a/test/integration/targets/nxos_vtp_version/tests/common/sanity.yaml +++ b/test/integration/targets/nxos_vtp_version/tests/common/sanity.yaml @@ -3,7 +3,18 @@ - debug: msg="Using provider={{ connection.transport }}" when: ansible_connection == "local" +- set_fact: vtp_run="true" +- set_fact: vtp_run="false" + when: platform is search('N3K-F|N9K-F') + - block: + - name: disable feature vtp + nxos_feature: + feature: vtp + provider: "{{ connection }}" + state: disabled + ignore_errors: yes + - name: enable feature vtp nxos_feature: feature: vtp @@ -28,6 +39,8 @@ that: - "result.changed == false" + when: vtp_run + always: - name: disable feature vtp nxos_feature: