Minor cleanup to nxos_vrrp (#27593)
* Safe changes * Fold `[0]` into `execute_show_command()` We only ever execute one command, so only return the one response * Streamline `results`
This commit is contained in:
parent
94748e1728
commit
b93878b685
2 changed files with 21 additions and 63 deletions
|
@ -84,7 +84,6 @@ EXAMPLES = '''
|
||||||
interface: vlan10
|
interface: vlan10
|
||||||
group: 100
|
group: 100
|
||||||
vip: 10.1.100.1
|
vip: 10.1.100.1
|
||||||
host: 68.170.147.165
|
|
||||||
|
|
||||||
- name: Ensure removal of the vrrp group config
|
- name: Ensure removal of the vrrp group config
|
||||||
# vip is required to ensure the user knows what they are removing
|
# vip is required to ensure the user knows what they are removing
|
||||||
|
@ -93,7 +92,6 @@ EXAMPLES = '''
|
||||||
group: 100
|
group: 100
|
||||||
vip: 10.1.100.1
|
vip: 10.1.100.1
|
||||||
state: absent
|
state: absent
|
||||||
host: 68.170.147.165
|
|
||||||
|
|
||||||
- name: Re-config with more params
|
- name: Re-config with more params
|
||||||
nxos_vrrp:
|
nxos_vrrp:
|
||||||
|
@ -103,39 +101,15 @@ EXAMPLES = '''
|
||||||
preempt: false
|
preempt: false
|
||||||
priority: 130
|
priority: 130
|
||||||
authentication: AUTHKEY
|
authentication: AUTHKEY
|
||||||
host: 68.170.147.165
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = '''
|
RETURN = '''
|
||||||
proposed:
|
commands:
|
||||||
description: k/v pairs of parameters passed into module
|
|
||||||
returned: always
|
|
||||||
type: dict
|
|
||||||
sample: {"authentication": "testing", "group": "150", "vip": "10.1.15.1",
|
|
||||||
"admin_state": "no shutdown"}
|
|
||||||
existing:
|
|
||||||
description: k/v pairs of existing vrrp info on the interface
|
|
||||||
returned: always
|
|
||||||
type: dict
|
|
||||||
sample: {}
|
|
||||||
end_state:
|
|
||||||
description: k/v pairs of vrrp after module execution
|
|
||||||
returned: always
|
|
||||||
type: dict
|
|
||||||
sample: {"authentication": "testing", "group": "150", "interval": "1",
|
|
||||||
"preempt": true, "priority": "100", "vip": "10.1.15.1",
|
|
||||||
"admin_state": "no shutdown"}
|
|
||||||
updates:
|
|
||||||
description: commands sent to the device
|
description: commands sent to the device
|
||||||
returned: always
|
returned: always
|
||||||
type: list
|
type: list
|
||||||
sample: ["interface vlan10", "vrrp 150", "address 10.1.15.1",
|
sample: ["interface vlan10", "vrrp 150", "address 10.1.15.1",
|
||||||
"authentication text testing", "no shutdown"]
|
"authentication text testing", "no shutdown"]
|
||||||
changed:
|
|
||||||
description: check to see if a change was made on the device
|
|
||||||
returned: always
|
|
||||||
type: boolean
|
|
||||||
sample: true
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from ansible.module_utils.nxos import load_config, run_commands
|
from ansible.module_utils.nxos import load_config, run_commands
|
||||||
|
@ -148,12 +122,12 @@ def execute_show_command(command, module):
|
||||||
output = 'json'
|
output = 'json'
|
||||||
else:
|
else:
|
||||||
output = 'text'
|
output = 'text'
|
||||||
cmds = [{
|
|
||||||
|
commands = [{
|
||||||
'command': command,
|
'command': command,
|
||||||
'output': output,
|
'output': output,
|
||||||
}]
|
}]
|
||||||
body = run_commands(module, cmds)
|
return run_commands(module, commands)[0]
|
||||||
return body
|
|
||||||
|
|
||||||
|
|
||||||
def apply_key_map(key_map, table):
|
def apply_key_map(key_map, table):
|
||||||
|
@ -161,13 +135,13 @@ def apply_key_map(key_map, table):
|
||||||
for key, value in table.items():
|
for key, value in table.items():
|
||||||
new_key = key_map.get(key)
|
new_key = key_map.get(key)
|
||||||
if new_key:
|
if new_key:
|
||||||
value = table.get(key)
|
|
||||||
if value:
|
if value:
|
||||||
new_dict[new_key] = str(value)
|
new_dict[new_key] = str(value)
|
||||||
else:
|
else:
|
||||||
new_dict[new_key] = value
|
new_dict[new_key] = value
|
||||||
return new_dict
|
return new_dict
|
||||||
|
|
||||||
|
|
||||||
def get_interface_type(interface):
|
def get_interface_type(interface):
|
||||||
if interface.upper().startswith('ET'):
|
if interface.upper().startswith('ET'):
|
||||||
return 'ethernet'
|
return 'ethernet'
|
||||||
|
@ -189,7 +163,7 @@ def is_default(interface, module):
|
||||||
command = 'show run interface {0}'.format(interface)
|
command = 'show run interface {0}'.format(interface)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
body = execute_show_command(command, module)[0]
|
body = execute_show_command(command, module)
|
||||||
if 'invalid' in body.lower():
|
if 'invalid' in body.lower():
|
||||||
return 'DNE'
|
return 'DNE'
|
||||||
else:
|
else:
|
||||||
|
@ -206,7 +180,7 @@ def get_interface_mode(interface, intf_type, module):
|
||||||
command = 'show interface {0}'.format(interface)
|
command = 'show interface {0}'.format(interface)
|
||||||
interface = {}
|
interface = {}
|
||||||
mode = 'unknown'
|
mode = 'unknown'
|
||||||
body = execute_show_command(command, module)[0]
|
body = execute_show_command(command, module)
|
||||||
interface_table = body['TABLE_interface']['ROW_interface']
|
interface_table = body['TABLE_interface']['ROW_interface']
|
||||||
name = interface_table.get('interface')
|
name = interface_table.get('interface')
|
||||||
|
|
||||||
|
@ -223,7 +197,7 @@ def get_interface_mode(interface, intf_type, module):
|
||||||
|
|
||||||
def get_vrr_status(group, module, interface):
|
def get_vrr_status(group, module, interface):
|
||||||
command = 'show run all | section interface.{0}$'.format(interface)
|
command = 'show run all | section interface.{0}$'.format(interface)
|
||||||
body = execute_show_command(command, module)[0]
|
body = execute_show_command(command, module)
|
||||||
vrf_index = None
|
vrf_index = None
|
||||||
admin_state = 'shutdown'
|
admin_state = 'shutdown'
|
||||||
|
|
||||||
|
@ -257,7 +231,7 @@ def get_existing_vrrp(interface, group, module, name):
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
vrrp_table = body[0]['TABLE_vrrp_group']
|
vrrp_table = body['TABLE_vrrp_group']
|
||||||
except (AttributeError, IndexError, TypeError):
|
except (AttributeError, IndexError, TypeError):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
@ -355,23 +329,21 @@ def main():
|
||||||
preempt=dict(required=False, type='bool'),
|
preempt=dict(required=False, type='bool'),
|
||||||
vip=dict(required=False, type='str'),
|
vip=dict(required=False, type='str'),
|
||||||
admin_state=dict(required=False, type='str',
|
admin_state=dict(required=False, type='str',
|
||||||
choices=['shutdown', 'no shutdown'],
|
choices=['shutdown', 'no shutdown'],
|
||||||
default='no shutdown'),
|
default='no shutdown'),
|
||||||
authentication=dict(required=False, type='str'),
|
authentication=dict(required=False, type='str'),
|
||||||
state=dict(choices=['absent', 'present'],
|
state=dict(choices=['absent', 'present'], required=False, default='present'),
|
||||||
required=False, default='present'),
|
|
||||||
include_defaults=dict(default=False),
|
include_defaults=dict(default=False),
|
||||||
config=dict(),
|
config=dict(),
|
||||||
save=dict(type='bool', default=False)
|
save=dict(type='bool', default=False)
|
||||||
)
|
)
|
||||||
|
|
||||||
argument_spec.update(nxos_argument_spec)
|
argument_spec.update(nxos_argument_spec)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec,
|
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||||
supports_check_mode=True)
|
|
||||||
|
|
||||||
warnings = list()
|
warnings = list()
|
||||||
check_args(module, warnings)
|
check_args(module, warnings)
|
||||||
|
results = {'changed': False, 'commands': [], 'warnings': warnings}
|
||||||
|
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
interface = module.params['interface'].lower()
|
interface = module.params['interface'].lower()
|
||||||
|
@ -418,32 +390,19 @@ def main():
|
||||||
if delta:
|
if delta:
|
||||||
command = get_commands_config_vrrp(delta, group)
|
command = get_commands_config_vrrp(delta, group)
|
||||||
commands.append(command)
|
commands.append(command)
|
||||||
|
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
if existing:
|
if existing:
|
||||||
commands.append(['no vrrp {0}'.format(group)])
|
commands.append(['no vrrp {0}'.format(group)])
|
||||||
|
|
||||||
if commands:
|
if commands:
|
||||||
commands.insert(0, ['interface {0}'.format(interface)])
|
commands.insert(0, ['interface {0}'.format(interface)])
|
||||||
|
commands = flatten_list(commands)
|
||||||
cmds = flatten_list(commands)
|
results['commands'] = commands
|
||||||
if cmds:
|
results['changed'] = True
|
||||||
if module.check_mode:
|
if not module.check_mode:
|
||||||
module.exit_json(changed=True, commands=cmds)
|
load_config(module, commands)
|
||||||
else:
|
if 'configure' in commands:
|
||||||
load_config(module, cmds)
|
commands.pop(0)
|
||||||
changed = True
|
|
||||||
end_state = get_existing_vrrp(interface, group, module, name)
|
|
||||||
if 'configure' in cmds:
|
|
||||||
cmds.pop(0)
|
|
||||||
|
|
||||||
results = {}
|
|
||||||
results['proposed'] = proposed
|
|
||||||
results['existing'] = existing
|
|
||||||
results['updates'] = cmds
|
|
||||||
results['changed'] = changed
|
|
||||||
results['warnings'] = warnings
|
|
||||||
results['end_state'] = end_state
|
|
||||||
|
|
||||||
module.exit_json(**results)
|
module.exit_json(**results)
|
||||||
|
|
||||||
|
|
|
@ -349,7 +349,6 @@ lib/ansible/modules/network/nxos/nxos_udld_interface.py
|
||||||
lib/ansible/modules/network/nxos/nxos_user.py
|
lib/ansible/modules/network/nxos/nxos_user.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vrf.py
|
lib/ansible/modules/network/nxos/nxos_vrf.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vrf_interface.py
|
lib/ansible/modules/network/nxos/nxos_vrf_interface.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vrrp.py
|
|
||||||
lib/ansible/modules/network/nxos/nxos_vtp_domain.py
|
lib/ansible/modules/network/nxos/nxos_vtp_domain.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vtp_password.py
|
lib/ansible/modules/network/nxos/nxos_vtp_password.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vtp_version.py
|
lib/ansible/modules/network/nxos/nxos_vtp_version.py
|
||||||
|
|
Loading…
Reference in a new issue