fix nxos_vrrp issues (#36930)

This commit is contained in:
saichint 2018-03-06 00:06:50 -08:00 committed by Trishna Guha
parent 51e8eab9db
commit 4ed0e5072d
2 changed files with 90 additions and 35 deletions

View file

@ -49,31 +49,38 @@ options:
description: description:
- Full name of interface that is being managed for VRRP. - Full name of interface that is being managed for VRRP.
required: true required: true
interval:
description:
- Time interval between advertisement or 'default' keyword
required: false
default: 1
version_added: 2.6
priority: priority:
description: description:
- VRRP priority. - VRRP priority or 'default' keyword
required: false required: false
default: null default: 100
preempt: preempt:
description: description:
- Enable/Disable preempt. - Enable/Disable preempt.
choices: ['True', 'False'] choices: ['True', 'False']
default: True
vip: vip:
description: description:
- VRRP virtual IP address. - VRRP virtual IP address or 'default' keyword
required: false required: false
default: null default: null
authentication: authentication:
description: description:
- Clear text authentication string. - Clear text authentication string or 'default' keyword
required: false required: false
default: null default: null
admin_state: admin_state:
description: description:
- Used to enable or disable the VRRP process. - Used to enable or disable the VRRP process.
required: false required: false
choices: ['shutdown', 'no shutdown'] choices: ['shutdown', 'no shutdown', 'default']
default: no shutdown default: shutdown
state: state:
description: description:
- Specify desired state of the resource. - Specify desired state of the resource.
@ -121,6 +128,14 @@ from ansible.module_utils.network.nxos.nxos import get_capabilities, nxos_argume
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
PARAM_TO_DEFAULT_KEYMAP = {
'priority': '100',
'interval': '1',
'vip': '0.0.0.0',
'admin_state': 'shutdown',
}
def execute_show_command(command, module): def execute_show_command(command, module):
if 'show run' not in command: if 'show run' not in command:
output = 'json' output = 'json'
@ -253,12 +268,12 @@ def get_existing_vrrp(interface, group, module, name):
if parsed_vrrp['group'] == group: if parsed_vrrp['group'] == group:
parsed_vrrp['admin_state'] = get_vrr_status(group, module, name) parsed_vrrp['admin_state'] = get_vrr_status(group, module, name)
return parsed_vrrp return parsed_vrrp
return vrrp return vrrp
def get_commands_config_vrrp(delta, group): def get_commands_config_vrrp(delta, existing, group):
commands = [] commands = []
CMDS = { CMDS = {
@ -266,32 +281,34 @@ def get_commands_config_vrrp(delta, group):
'preempt': 'preempt', 'preempt': 'preempt',
'vip': 'address {0}', 'vip': 'address {0}',
'interval': 'advertisement-interval {0}', 'interval': 'advertisement-interval {0}',
'auth': 'authentication text {0}' 'auth': 'authentication text {0}',
'admin_state': '{0}',
} }
vip = delta.get('vip') for arg in ['vip', 'priority', 'interval', 'admin_state']:
priority = delta.get('priority') val = delta.get(arg)
preempt = delta.get('preempt') if val == 'default':
interval = delta.get('interval') val = PARAM_TO_DEFAULT_KEYMAP.get(arg)
auth = delta.get('authentication') if val != existing.get(arg):
admin_state = delta.get('admin_state') commands.append((CMDS.get(arg)).format(val))
elif val:
commands.append((CMDS.get(arg)).format(val))
preempt = delta.get('preempt')
auth = delta.get('authentication')
if vip:
commands.append((CMDS.get('vip')).format(vip))
if priority:
commands.append((CMDS.get('priority')).format(priority))
if preempt: if preempt:
commands.append(CMDS.get('preempt')) commands.append(CMDS.get('preempt'))
elif preempt is False: elif preempt is False:
commands.append('no ' + CMDS.get('preempt')) commands.append('no ' + CMDS.get('preempt'))
if interval:
commands.append((CMDS.get('interval')).format(interval))
if auth: if auth:
commands.append((CMDS.get('auth')).format(auth)) if auth != 'default':
if admin_state: commands.append((CMDS.get('auth')).format(auth))
commands.append(admin_state) elif existing.get('authentication'):
commands.append('no authentication')
commands.insert(0, 'vrrp {0}'.format(group)) if commands:
commands.insert(0, 'vrrp {0}'.format(group))
return commands return commands
@ -329,12 +346,13 @@ def main():
argument_spec = dict( argument_spec = dict(
group=dict(required=True, type='str'), group=dict(required=True, type='str'),
interface=dict(required=True), interface=dict(required=True),
interval=dict(required=False, type='str'),
priority=dict(required=False, type='str'), priority=dict(required=False, type='str'),
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'],
default='no shutdown'), default='shutdown'),
authentication=dict(required=False, type='str'), authentication=dict(required=False, type='str'),
state=dict(choices=['absent', 'present'], required=False, default='present') state=dict(choices=['absent', 'present'], required=False, default='present')
) )
@ -349,6 +367,7 @@ def main():
interface = module.params['interface'].lower() interface = module.params['interface'].lower()
group = module.params['group'] group = module.params['group']
priority = module.params['priority'] priority = module.params['priority']
interval = module.params['interval']
preempt = module.params['preempt'] preempt = module.params['preempt']
vip = module.params['vip'] vip = module.params['vip']
authentication = module.params['authentication'] authentication = module.params['authentication']
@ -375,7 +394,7 @@ def main():
'a layer 3 port first.', interface=interface) 'a layer 3 port first.', interface=interface)
args = dict(group=group, priority=priority, preempt=preempt, args = dict(group=group, priority=priority, preempt=preempt,
vip=vip, authentication=authentication, vip=vip, authentication=authentication, interval=interval,
admin_state=admin_state) admin_state=admin_state)
proposed = dict((k, v) for k, v in args.items() if v is not None) proposed = dict((k, v) for k, v in args.items() if v is not None)
@ -389,8 +408,9 @@ def main():
delta = dict( delta = dict(
set(proposed.items()).difference(existing.items())) set(proposed.items()).difference(existing.items()))
if delta: if delta:
command = get_commands_config_vrrp(delta, group) command = get_commands_config_vrrp(delta, existing, group)
commands.append(command) if 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)])

View file

@ -26,7 +26,7 @@
interface: vlan10 interface: vlan10
group: 100 group: 100
vip: 10.1.100.1 vip: 10.1.100.1
admin_state: shutdown admin_state: 'no shutdown'
provider: "{{ connection }}" provider: "{{ connection }}"
register: result register: result
@ -42,13 +42,27 @@
that: that:
- "result.changed == false" - "result.changed == false"
- name: Ensure vrrp group 100 is default
nxos_vrrp: &configure_def
interface: vlan10
group: 100
vip: default
admin_state: default
provider: "{{ connection }}"
register: result
- assert: *true
- name: "Conf Idempotence"
nxos_vrrp: *configure_def
register: result
- assert: *false
- 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
nxos_vrrp: &remove nxos_vrrp: &remove
interface: vlan10 interface: vlan10
group: 100 group: 100
vip: 10.1.100.1
admin_state: shutdown
state: absent state: absent
provider: "{{ connection }}" provider: "{{ connection }}"
register: result register: result
@ -72,7 +86,8 @@
interface: vlan10 interface: vlan10
group: 100 group: 100
vip: 10.1.100.1 vip: 10.1.100.1
preempt: false preempt: False
interval: 10
priority: 130 priority: 130
authentication: AUTHKEY authentication: AUTHKEY
provider: "{{ connection }}" provider: "{{ connection }}"
@ -86,6 +101,26 @@
- assert: *false - assert: *false
- name: Re-config with defaults
nxos_vrrp: &reconfig_def
interface: vlan10
group: 100
vip: default
preempt: True
interval: default
priority: default
authentication: default
provider: "{{ connection }}"
register: result
- assert: *true
- name: "Reconfig Idempotence"
nxos_vrrp: *reconfig_def
register: result
- assert: *false
always: always:
- name: remove vrrp - name: remove vrrp
nxos_vrrp: *remove nxos_vrrp: *remove