nxos_vlan fix (#24973)

This commit is contained in:
Trishna Guha 2017-05-25 09:45:43 +05:30 committed by GitHub
commit 307fd1b3dc

View file

@ -22,7 +22,6 @@ ANSIBLE_METADATA = {
'supported_by': 'community', 'supported_by': 'community',
} }
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: nxos_vlan module: nxos_vlan
@ -75,8 +74,8 @@ options:
required: false required: false
default: present default: present
choices: ['present','absent'] choices: ['present','absent']
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: Ensure a range of VLANs are not present on the switch - name: Ensure a range of VLANs are not present on the switch
nxos_vlan: nxos_vlan:
@ -105,10 +104,12 @@ commands:
type: list type: list
sample: ["vlan 20", "vlan 55", "vn-segment 5000"] sample: ["vlan 20", "vlan 55", "vn-segment 5000"]
''' '''
import re import re
from ansible.module_utils.nxos import get_config, load_config, run_commands from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.nxos import nxos_argument_spec
from ansible.module_utils.nxos import check_args as nxos_check_args
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -170,7 +171,7 @@ def get_vlan_config_commands(vlan, vid):
# value map has more than 1 key # value map has more than 1 key
vlan = apply_value_map(reverse_value_map, vlan) vlan = apply_value_map(reverse_value_map, vlan)
VLAN_ARGS = { vlan_args = {
'name': 'name {0}', 'name': 'name {0}',
'vlan_state': 'state {0}', 'vlan_state': 'state {0}',
'admin_state': '{0}', 'admin_state': '{0}',
@ -184,7 +185,7 @@ def get_vlan_config_commands(vlan, vid):
if param == 'mapped_vni' and value == 'default': if param == 'mapped_vni' and value == 'default':
command = 'no vn-segment' command = 'no vn-segment'
else: else:
command = VLAN_ARGS.get(param).format(vlan.get(param)) command = vlan_args.get(param).format(vlan.get(param))
if command: if command:
commands.append(command) commands.append(command)
@ -195,9 +196,9 @@ def get_vlan_config_commands(vlan, vid):
def get_list_of_vlans(module): def get_list_of_vlans(module):
body = run_commands(module, ['show vlan | json']) body = run_commands(module, ['show vlan | json'])[0]
vlan_list = [] vlan_list = []
vlan_table = body[0].get('TABLE_vlanbrief')['ROW_vlanbrief'] vlan_table = body.get('TABLE_vlanbrief')['ROW_vlanbrief']
if isinstance(vlan_table, list): if isinstance(vlan_table, list):
for vlan in vlan_table: for vlan in vlan_table:
@ -224,11 +225,10 @@ def get_vlan(vlanid, module):
"""Get instance of VLAN as a dictionary """Get instance of VLAN as a dictionary
""" """
command = 'show vlan id %s | json' % vlanid command = 'show vlan id %s | json' % vlanid
body = run_commands(module, [command])
try: try:
vlan_table = body[0]['TABLE_vlanbriefid']['ROW_vlanbriefid'] body = run_commands(module, [command])[0]
except (TypeError, IndexError): vlan_table = body['TABLE_vlanbriefid']['ROW_vlanbriefid']
except (TypeError, IndexError, KeyError):
return {} return {}
key_map = { key_map = {
@ -267,6 +267,14 @@ def apply_value_map(value_map, resource):
return resource return resource
def check_args(module, warnings):
nxos_check_args(module, warnings)
for key in ('include_defaults', 'config', 'save'):
if module.params[key] is not None:
warnings.append('argument %s is no longer supported, ignoring value' % key)
def main(): def main():
argument_spec = dict( argument_spec = dict(
vlan_id=dict(required=False, type='str'), vlan_id=dict(required=False, type='str'),
@ -292,6 +300,7 @@ def main():
warnings = list() warnings = list()
check_args(module, warnings) check_args(module, warnings)
results = dict(changed=False, warnings=warnings)
vlan_range = module.params['vlan_range'] vlan_range = module.params['vlan_range']
vlan_id = module.params['vlan_id'] vlan_id = module.params['vlan_id']
@ -301,8 +310,6 @@ def main():
mapped_vni = module.params['mapped_vni'] mapped_vni = module.params['mapped_vni']
state = module.params['state'] state = module.params['state']
changed = False
if vlan_id: if vlan_id:
if not vlan_id.isdigit(): if not vlan_id.isdigit():
module.fail_json(msg='vlan_id must be a valid VLAN ID') module.fail_json(msg='vlan_id must be a valid VLAN ID')
@ -348,18 +355,12 @@ def main():
existing.get('mapped_vni') != '0' and proposed.get('mapped_vni') != 'default'): existing.get('mapped_vni') != '0' and proposed.get('mapped_vni') != 'default'):
commands.insert(1, 'no vn-segment') commands.insert(1, 'no vn-segment')
if module.check_mode: if module.check_mode:
module.exit_json(changed=True, module.exit_json(changed=True, commands=commands)
commands=commands)
else: else:
load_config(module, commands) load_config(module, commands)
changed = True results['changed'] = True
results = {
'commands': commands,
'changed': changed,
'warnings': warnings
}
results['commands'] = commands
module.exit_json(**results) module.exit_json(**results)