[CLI_CONF] Refactor nxos module common library to use cliconf plugin (#31524)

* refactor nxos modules

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>

* get_config update

* fix get_config

* update nxos module

* device_info

* fix conflict

* add failure message check

* pep8 fixes

* use get_capabilities to check cliconf

* Add logic to detect platform in get_capabilities and cache in module_utils

* return msg in edit_config

* fix conflicts

* make modules compatible

* fix nxos cliconf api

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>

* cache capabilities

* Update transport queries to get_capabilities in module level

* fix unit tests

* load_config error code

* remove unnecessary change

* Refactor nxos_install_os module
This commit is contained in:
Trishna Guha 2017-12-19 12:22:33 +05:30 committed by GitHub
parent bd399ec674
commit 9f86b923e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 220 additions and 132 deletions

View file

@ -156,18 +156,20 @@ changed:
import re
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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
def execute_show_command(command, module, command_type='cli_show'):
provider = module.params['provider']
if provider['transport'] == 'cli':
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if network_api == 'cliconf':
if 'show run' not in command:
command += ' | json'
cmds = [command]
body = run_commands(module, cmds)
elif provider['transport'] == 'nxapi':
elif network_api == 'nxapi':
cmds = {'command': command, 'output': 'text'}
body = run_commands(module, cmds)
@ -279,7 +281,6 @@ def main():
supports_check_mode=True)
warnings = list()
check_args(module, warnings)
server_type = module.params['server_type']
address = module.params['address']

View file

@ -683,7 +683,7 @@ def main():
if candidate:
candidate = candidate.items_text()
warnings.extend(load_config(module, candidate))
load_config(module, candidate)
result['changed'] = True
result['commands'] = candidate
else:

View file

@ -493,7 +493,7 @@ def main():
if candidate:
candidate = candidate.items_text()
warnings.extend(load_config(module, candidate))
load_config(module, candidate)
result['changed'] = True
result['commands'] = candidate
else:

View file

@ -728,7 +728,7 @@ def main():
if candidate:
candidate = candidate.items_text()
warnings.extend(load_config(module, candidate))
load_config(module, candidate)
result['changed'] = True
result['commands'] = candidate
else:

View file

@ -184,11 +184,13 @@ class FactsBase(object):
def populate(self):
pass
def run(self, command, output=None):
def run(self, command, output='text'):
command_string = command
if output:
command = {'command': command, 'output': output}
resp = run_commands(self.module, command, check_rc=False)
command = {
'command': command,
'output': output
}
resp = run_commands(self.module, [command], check_rc=False)
try:
return resp[0]
except IndexError:
@ -226,7 +228,7 @@ class Default(FactsBase):
])
def populate(self):
data = self.run('show version', 'json')
data = self.run('show version', output='json')
if data:
if data.get('sys_ver_str'):
self.facts.update(self.transform_dict(data, self.VERSION_MAP_7K))
@ -244,11 +246,11 @@ class Config(FactsBase):
class Hardware(FactsBase):
def populate(self):
data = self.run('dir', 'text')
data = self.run('dir')
if data:
self.facts['filesystems'] = self.parse_filesystems(data)
data = self.run('show system resources', 'json')
data = self.run('show system resources', output='json')
if data:
self.facts['memtotal_mb'] = int(data['memory_usage_total']) / 1024
self.facts['memfree_mb'] = int(data['memory_usage_free']) / 1024
@ -282,7 +284,7 @@ class Interfaces(FactsBase):
])
def ipv6_structure_op_supported(self):
data = self.run('show version', 'json')
data = self.run('show version', output='json')
if data:
unsupported_versions = ['I2', 'F1', 'A8']
for ver in unsupported_versions:
@ -294,11 +296,11 @@ class Interfaces(FactsBase):
self.facts['all_ipv4_addresses'] = list()
self.facts['all_ipv6_addresses'] = list()
data = self.run('show interface', 'json')
data = self.run('show interface', output='json')
if data:
self.facts['interfaces'] = self.populate_interfaces(data)
data = self.run('show ipv6 interface', 'json') if self.ipv6_structure_op_supported() else None
data = self.run('show ipv6 interface', output='json') if self.ipv6_structure_op_supported() else None
if data and not isinstance(data, string_types):
self.parse_ipv6_interfaces(data)
@ -306,7 +308,7 @@ class Interfaces(FactsBase):
if data:
self.facts['neighbors'] = self.populate_neighbors(data)
data = self.run('show cdp neighbors detail', 'json')
data = self.run('show cdp neighbors detail', output='json')
if data:
self.facts['neighbors'] = self.populate_neighbors_cdp(data)
@ -428,23 +430,23 @@ class Legacy(FactsBase):
])
def populate(self):
data = self.run('show version', 'json')
data = self.run('show version', output='json')
if data:
self.facts.update(self.transform_dict(data, self.VERSION_MAP))
data = self.run('show interface', 'json')
data = self.run('show interface', output='json')
if data:
self.facts['_interfaces_list'] = self.parse_interfaces(data)
data = self.run('show vlan brief', 'json')
data = self.run('show vlan brief', output='json')
if data:
self.facts['_vlan_list'] = self.parse_vlans(data)
data = self.run('show module', 'json')
data = self.run('show module', output='json')
if data:
self.facts['_module'] = self.parse_module(data)
data = self.run('show environment', 'json')
data = self.run('show environment', output='json')
if data:
self.facts['_fan_info'] = self.parse_fan_info(data)
self.facts['_power_supply_info'] = self.parse_power_supply_info(data)

View file

@ -165,16 +165,18 @@ changed:
import re
from ansible.module_utils.network.nxos.nxos import get_config, 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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
def execute_show_command(command, module, command_type='cli_show_ascii'):
cmds = [command]
provider = module.params['provider']
if provider['transport'] == 'cli':
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if network_api == 'cliconf':
body = run_commands(module, cmds)
elif provider['transport'] == 'nxapi':
elif network_api == 'nxapi':
body = run_commands(module, cmds)
return body
@ -292,7 +294,6 @@ def main():
supports_check_mode=True)
warnings = list()
check_args(module, warnings)
state = module.params['state']
mode = get_system_mode(module)

View file

@ -124,17 +124,19 @@ commands:
'''
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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
def execute_show_command(command, module):
provider = module.params['provider']
if provider['transport'] == 'cli':
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if network_api == 'cliconf':
command += ' | json'
cmds = [command]
body = run_commands(module, cmds)
elif provider['transport'] == 'nxapi':
elif network_api == 'nxapi':
cmds = [command]
body = run_commands(module, cmds)
@ -382,7 +384,6 @@ def main():
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
warnings = list()
check_args(module, warnings)
results = dict(changed=False, warnings=warnings)
interface = module.params['interface'].lower()
@ -395,7 +396,8 @@ def main():
auth_type = module.params['auth_type']
auth_string = module.params['auth_string']
transport = module.params['provider']['transport']
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if state == 'present' and not vip:
module.fail_json(msg='the "vip" param is required when state=present')
@ -405,7 +407,7 @@ def main():
validate_params(param, module)
intf_type = get_interface_type(interface)
if (intf_type != 'ethernet' and transport == 'cli'):
if (intf_type != 'ethernet' and network_api == 'cliconf'):
if is_default(interface, module) == 'DNE':
module.fail_json(msg='That interface does not exist yet. Create '
'it first.', interface=interface)
@ -463,7 +465,7 @@ def main():
load_config(module, commands)
# validate IP
if transport == 'cli' and state == 'present':
if network_api == 'cliconf' and state == 'present':
commands.insert(0, 'config t')
body = run_commands(module, commands)
validate_config(body, vip, module)

View file

@ -238,15 +238,27 @@ def parse_show_install(data):
if len(data) > 0:
data = massage_install_data(data)
ud = {'raw': data}
ud['list_data'] = data.split('\n')
ud['processed'] = []
ud['disruptive'] = False
ud['upgrade_needed'] = False
ud['error'] = False
ud['install_in_progress'] = False
ud['backend_processing_error'] = False
ud['server_error'] = False
ud['upgrade_succeeded'] = False
ud['use_impact_data'] = False
# Check for server errors
if isinstance(data, int):
if data == -1:
ud['server_error'] = True
elif data >= 500:
ud['server_error'] = True
elif data == -32603:
ud['server_error'] = True
return ud
else:
ud['list_data'] = data.split('\n')
for x in ud['list_data']:
# Check for errors and exit if found.
if re.search(r'Pre-upgrade check failed', x):
@ -264,7 +276,10 @@ def parse_show_install(data):
ud['install_in_progress'] = True
break
if re.search(r'Backend processing error', x):
ud['backend_processing_error'] = True
ud['server_error'] = True
break
if re.search(r'^(-1|5\d\d)$', x):
ud['server_error'] = True
break
# Check for messages indicating a successful upgrade.
@ -465,7 +480,7 @@ def check_install_in_progress(module, commands, opts):
def check_mode(module, issu, image, kick=None):
"""Check switch upgrade impact using 'show install all impact' command"""
data = check_mode_nextgen(module, issu, image, kick)
if data['backend_processing_error']:
if data['server_error']:
# We encountered an unrecoverable error in the attempt to get upgrade
# impact data from the 'show install all impact' command.
# Fallback to legacy method.
@ -500,11 +515,11 @@ def do_install_all(module, issu, image, kick=None):
# it's done.
upgrade = check_install_in_progress(module, commands, opts)
# Special case: If we encounter a backend processing error at this
# stage it means the command was sent and the upgrade was started but
# Special case: If we encounter a server error at this stage
# it means the command was sent and the upgrade was started but
# we will need to use the impact data instead of the current install
# data.
if upgrade['backend_processing_error']:
if upgrade['server_error']:
upgrade['upgrade_succeeded'] = True
upgrade['use_impact_data'] = True

View file

@ -176,7 +176,7 @@ except ImportError:
HAS_IPADDRESS = False
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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
@ -457,6 +457,9 @@ def flatten_list(command_lists):
def validate_params(addr, interface, mask, dot1q, tag, allow_secondary, version, state, intf_type, module):
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if state == "present":
if addr is None or mask is None:
module.fail_json(msg="An IP address AND a mask must be provided "
@ -466,7 +469,7 @@ def validate_params(addr, interface, mask, dot1q, tag, allow_secondary, version,
module.fail_json(msg="IPv6 address and mask must be provided when "
"state=absent.")
if intf_type != "ethernet" and module.params["provider"]["transport"] == "cli":
if intf_type != "ethernet" and network_api == 'cliconf':
if is_default(interface, module) == "DNE":
module.fail_json(msg="That interface does not exist yet. Create "
"it first.", interface=interface)
@ -538,7 +541,6 @@ def main():
module.fail_json(msg="ipaddress is required for this module. Run 'pip install ipaddress' for install.")
warnings = list()
check_args(module, warnings)
addr = module.params['addr']
version = module.params['version']

View file

@ -126,18 +126,17 @@ import re
from ansible.module_utils.network.nxos.nxos import run_commands, load_config
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec
from ansible.module_utils.network.nxos.nxos import check_args as nxos_check_args
from ansible.module_utils.network.nxos.nxos import get_capabilities
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import iteritems
def check_args(module, warnings):
provider = module.params['provider']
if provider['transport'] == 'nxapi':
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if network_api == 'nxapi':
module.fail_json(msg='module not supported over nxapi transport')
nxos_check_args(module, warnings)
state = module.params['state']
if state == 'started':

View file

@ -99,7 +99,7 @@ import collections
import re
from ansible.module_utils.network.nxos.nxos import get_config, 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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.common.config import CustomNetworkConfig
@ -142,13 +142,15 @@ def get_custom_value(arg, config, module):
def execute_show_command(command, module):
provider = module.params['provider']
if provider['transport'] == 'cli':
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if network_api == 'cliconf':
if 'show port-channel summary' in command:
command += ' | json'
cmds = [command]
body = run_commands(module, cmds)
elif provider['transport'] == 'nxapi':
elif network_api == 'nxapi':
cmds = [command]
body = run_commands(module, cmds)
@ -446,7 +448,6 @@ def main():
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
warnings = list()
check_args(module, warnings)
results = dict(changed=False, warnings=warnings)
group = str(module.params['group'])

View file

@ -127,7 +127,7 @@ commands:
'''
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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
@ -434,12 +434,14 @@ def apply_value_map(value_map, resource):
def execute_show_command(command, module, command_type='cli_show'):
provider = module.params['provider']
if provider['transport'] == 'cli':
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if network_api == 'cliconf':
command += ' | json'
cmds = [command]
body = run_commands(module, cmds)
elif provider['transport'] == 'nxapi':
elif network_api == 'nxapi':
cmds = [command]
body = run_commands(module, cmds)
@ -479,7 +481,6 @@ def main():
warnings = list()
commands = []
results = {'changed': False}
check_args(module, warnings)
interface = module.params['interface']
mode = module.params['mode']

View file

@ -110,24 +110,23 @@ changed:
sample: true
'''
import re
from ansible.module_utils.network.nxos.nxos import get_config, 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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
import re
import re
def execute_show_command(command, module, command_type='cli_show'):
provider = module.params['provider']
if provider['transport'] == 'cli':
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if network_api == 'cliconf':
if 'show run' not in command:
command += ' | json'
cmds = [command]
body = run_commands(module, cmds)
elif provider['transport'] == 'nxapi':
elif network_api == 'nxapi':
cmds = [command]
body = run_commands(module, cmds)
@ -227,7 +226,6 @@ def main():
supports_check_mode=True)
warnings = list()
check_args(module, warnings)
aggressive = module.params['aggressive']
msg_time = module.params['msg_time']

View file

@ -111,18 +111,20 @@ changed:
from ansible.module_utils.network.nxos.nxos import get_config, 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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
def execute_show_command(command, module, command_type='cli_show'):
provider = module.params['provider']
if provider['transport'] == 'cli':
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if network_api == 'cliconf':
if 'show run' not in command:
command += ' | json'
cmds = [command]
body = run_commands(module, cmds)
elif provider['transport'] == 'nxapi':
elif network_api == 'nxapi':
cmds = [command]
body = run_commands(module, cmds)
@ -248,7 +250,6 @@ def main():
supports_check_mode=True)
warnings = list()
check_args(module, warnings)
interface = module.params['interface'].lower()
mode = module.params['mode']

View file

@ -81,7 +81,7 @@ commands:
import re
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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
@ -199,20 +199,22 @@ def main():
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
warnings = list()
check_args(module, warnings)
results = {'changed': False, 'commands': [], 'warnings': warnings}
vrf = module.params['vrf']
interface = module.params['interface'].lower()
state = module.params['state']
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
current_vrfs = get_vrf_list(module)
if vrf not in current_vrfs:
warnings.append("The VRF is not present/active on the device. "
"Use nxos_vrf to fix this.")
intf_type = get_interface_type(interface)
if (intf_type != 'ethernet' and module.params['provider']['transport'] == 'cli'):
if (intf_type != 'ethernet' and network_api == 'cliconf'):
if is_default(interface, module) == 'DNE':
module.fail_json(msg="interface does not exist on switch. Verify "
"switch platform or create it first with "

View file

@ -114,7 +114,7 @@ commands:
'''
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, nxos_argument_spec
from ansible.module_utils.basic import AnsibleModule
@ -343,7 +343,6 @@ def main():
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
warnings = list()
check_args(module, warnings)
results = {'changed': False, 'commands': [], 'warnings': warnings}
state = module.params['state']
@ -355,13 +354,14 @@ def main():
authentication = module.params['authentication']
admin_state = module.params['admin_state']
transport = module.params['transport']
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
if state == 'present' and not vip:
module.fail_json(msg='the "vip" param is required when state=present')
intf_type = get_interface_type(interface)
if (intf_type != 'ethernet' and transport == 'cli'):
if (intf_type != 'ethernet' and network_api == 'cliconf'):
if is_default(interface, module) == 'DNE':
module.fail_json(msg='That interface does not exist yet. Create '
'it first.', interface=interface)