junos_linkagg aggregate and other minor fixes (#27855)

* junos_linkagg aggregate and other minor fixes

* Minor change
This commit is contained in:
Ganesh Nalawade 2017-08-08 12:13:28 +05:30 committed by GitHub
parent 43f6a97bdc
commit 49eb81d573
9 changed files with 96 additions and 76 deletions

View file

@ -142,8 +142,6 @@ diff.prepared:
"""
import collections
from copy import copy
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.junos import load_config, map_params_to_obj, map_obj_to_ele
@ -189,7 +187,7 @@ def main():
active=dict(default=True, type='bool')
)
aggregate_spec = copy(element_spec)
aggregate_spec = element_spec.copy()
aggregate_spec['name'] = dict(required=True)
argument_spec = dict(
@ -238,7 +236,7 @@ def main():
requests = list()
for param in params:
item = copy(param)
item = param.copy()
state = item.get('state')
item['disable'] = True if state == 'down' else False

View file

@ -103,8 +103,6 @@ diff:
"""
import collections
from copy import copy
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.junos import load_config, map_params_to_obj, map_obj_to_ele
@ -130,7 +128,7 @@ def main():
active=dict(default=True, type='bool')
)
aggregate_spec = copy(element_spec)
aggregate_spec = element_spec.copy()
aggregate_spec['name'] = dict(required=True)
argument_spec = dict(
@ -174,7 +172,7 @@ def main():
requests = list()
for param in params:
item = copy(param)
item = param.copy()
if not item['ipv4'] and not item['ipv6']:
module.fail_json(msg="one of the following is required: ipv4,ipv6")

View file

@ -51,6 +51,12 @@ options:
description:
description:
- Description of Interface.
aggregate:
description: List of link aggregation definitions.
purge:
description:
- Purge link aggregation groups not defined in the aggregates parameter.
default: no
state:
description:
- State of the link aggregation group.
@ -156,7 +162,7 @@ import collections
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.junos import load_config, map_params_to_obj, map_obj_to_ele
from ansible.module_utils.junos import load_config, map_params_to_obj, map_obj_to_ele, to_param_list
from ansible.module_utils.junos import commit_configuration, discard_changes, locked_config, get_configuration
try:
@ -178,15 +184,15 @@ def validate_min_links(value, module):
module.fail_json(msg='min_links must be between 1 and 8')
def validate_param_values(module, obj):
def validate_param_values(module, obj, item):
for key in obj:
# validate the param value (if validator func exists)
validator = globals().get('validate_%s' % key)
if callable(validator):
validator(module.params.get(key), module)
validator(item.get(key), module)
def configure_lag_params(module, warnings):
def configure_lag_params(module, requests, item):
top = 'interfaces/interface'
param_lag_to_xpath_map = collections.OrderedDict()
param_lag_to_xpath_map.update([
@ -194,32 +200,29 @@ def configure_lag_params(module, warnings):
('description', 'description'),
('min_links', {'xpath': 'minimum-links', 'top': 'aggregated-ether-options'}),
('disable', {'xpath': 'disable', 'tag_only': True}),
('mode', {'xpath': module.params['mode'], 'tag_only': True, 'top': 'aggregated-ether-options/lacp'}),
('mode', {'xpath': item['mode'], 'tag_only': True, 'top': 'aggregated-ether-options/lacp'}),
])
validate_param_values(module, param_lag_to_xpath_map)
validate_param_values(module, param_lag_to_xpath_map, item)
want = map_params_to_obj(module, param_lag_to_xpath_map)
ele = map_obj_to_ele(module, want, top)
want = map_params_to_obj(module, param_lag_to_xpath_map, param=item)
ele = map_obj_to_ele(module, want, top, param=item)
requests.append(ele)
diff = load_config(module, tostring(ele), warnings, action='replace')
if module.params['device_count']:
if item['device_count']:
top = 'chassis/aggregated-devices/ethernet'
device_count_to_xpath_map = {'device_count': {'xpath': 'device-count', 'leaf_only': True}}
validate_param_values(module, device_count_to_xpath_map)
validate_param_values(module, device_count_to_xpath_map, item)
want = map_params_to_obj(module, device_count_to_xpath_map)
ele = map_obj_to_ele(module, want, top)
diff = load_config(module, tostring(ele), warnings, action='replace')
return diff
want = map_params_to_obj(module, device_count_to_xpath_map, param=item)
ele = map_obj_to_ele(module, want, top, param=item)
requests.append(ele)
def configure_member_params(module, warnings, diff=None):
def configure_member_params(module, requests, item):
top = 'interfaces/interface'
members = module.params['members']
members = item['members']
if members:
member_to_xpath_map = collections.OrderedDict()
@ -229,36 +232,34 @@ def configure_member_params(module, warnings, diff=None):
])
# link aggregation bundle assigned to member
module.params['bundle'] = module.params['name']
item['bundle'] = item['name']
for member in members:
if module.params['state'] == 'absent':
if item['state'] == 'absent':
# if link aggregate bundle is not assigned to member, trying to
# delete it results in rpc-reply error, hence if is not assigned
# skip deleting it and continue to next member.
resp = get_configuration(module)
bundle = resp.xpath("configuration/interfaces/interface[name='%s']/ether-options/"
"ieee-802.3ad[bundle='%s']" % (member, module.params['bundle']))
"ieee-802.3ad[bundle='%s']" % (member, item['bundle']))
if not bundle:
continue
# Name of member to be assigned to link aggregation bundle
module.params['name'] = member
item['name'] = member
validate_param_values(module, member_to_xpath_map)
validate_param_values(module, member_to_xpath_map, item)
want = map_params_to_obj(module, member_to_xpath_map)
ele = map_obj_to_ele(module, want, top)
diff = load_config(module, tostring(ele), warnings)
return diff
want = map_params_to_obj(module, member_to_xpath_map, param=item)
ele = map_obj_to_ele(module, want, top, param=item)
requests.append(ele)
def main():
""" main entry point for module execution
"""
argument_spec = dict(
name=dict(required=True),
element_spec = dict(
name=dict(),
mode=dict(default='on', type='str', choices=['on', 'off', 'active', 'passive']),
members=dict(type='list'),
min_links=dict(type='int'),
@ -268,14 +269,32 @@ def main():
active=dict(default=True, type='bool')
)
aggregate_spec = element_spec.copy()
aggregate_spec['name'] = dict(required=True)
argument_spec = dict(
aggregate=dict(type='list', elements='dict', options=aggregate_spec),
purge=dict(default=False, type='bool')
)
argument_spec.update(element_spec)
argument_spec.update(junos_argument_spec)
required_one_of = [['name', 'collection']]
mutually_exclusive = [['name', 'collection']]
required_one_of = [['name', 'aggregate']]
mutually_exclusive = [['name', 'aggregate'],
['mode', 'aggregate'],
['members', 'aggregate'],
['min_links', 'aggregate'],
['device_count', 'aggregate'],
['description', 'aggregate'],
['state', 'aggregate'],
['active', 'aggregate']]
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True,
required_one_of=required_one_of,
mutually_exclusive=mutually_exclusive,
supports_check_mode=True)
mutually_exclusive=mutually_exclusive)
warnings = list()
check_args(module, warnings)
@ -285,23 +304,33 @@ def main():
if warnings:
result['warnings'] = warnings
state = module.params.get('state')
module.params['disable'] = True if state == 'down' else False
params = to_param_list(module)
requests = list()
for param in params:
item = param.copy()
state = item.get('state')
item['disable'] = True if state == 'down' else False
if state in ('present', 'up', 'down'):
module.params['state'] = 'present'
item['state'] = 'present'
else:
module.params['disable'] = True
item['disable'] = True
if module.params.get('mode') == 'off':
module.params['mode'] = ''
elif module.params.get('mode') == 'on':
module.params['mode'] = 'passive'
mode = item.get('mode')
if mode == 'off':
item['mode'] = ''
elif mode == 'on':
item['mode'] = 'passive'
configure_lag_params(module, requests, item)
configure_member_params(module, requests, item)
diff = None
with locked_config(module):
diff = configure_lag_params(module, warnings)
diff = configure_member_params(module, warnings, diff)
for req in requests:
diff = load_config(module, tostring(req), warnings, action='replace')
commit = not module.check_mode
if diff:

View file

@ -173,11 +173,13 @@ def main():
('disable', {'xpath': 'disable', 'tag_only': True, 'is_key': True})
])
state = module.params.get('state')
module.params['disable'] = True if state in ('disabled', 'absent') else False
item = module.params.copy()
state = item.get('state')
item['disable'] = True if state in ('disabled', 'absent') else False
if state in ('enabled', 'disabled'):
module.params['state'] = 'present'
item['state'] = 'present'
want = map_params_to_obj(module, param_to_xpath_map)
ele = map_obj_to_ele(module, want, top)

View file

@ -142,11 +142,12 @@ def main():
('disable', {'xpath': 'disable', 'tag_only': True})
])
state = module.params.get('state')
module.params['disable'] = True if state in ('disabled', 'absent') else False
item = module.params.copy()
state = item.get('state')
item['disable'] = True if state in ('disabled', 'absent') else False
if state in ('enabled', 'disabled'):
module.params['state'] = 'present'
item['state'] = 'present'
want = map_params_to_obj(module, param_to_xpath_map)
ele = map_obj_to_ele(module, want, top)

View file

@ -126,8 +126,6 @@ diff.prepared:
"""
import collections
from copy import copy
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.junos import load_config, map_params_to_obj, map_obj_to_ele, to_param_list
@ -222,7 +220,7 @@ def main():
requests = list()
for param in params:
item = copy(param)
item = param.copy()
dest = item.get('dest')
if dest == 'console' and item.get('name'):
module.fail_json(msg="%s and %s are mutually exclusive" % ('console', 'name'))

View file

@ -132,8 +132,6 @@ diff.prepared:
"""
import collections
from copy import copy
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.junos import load_config, map_params_to_obj, map_obj_to_ele, to_param_list
@ -160,7 +158,7 @@ def main():
active=dict(default=True, type='bool')
)
aggregate_spec = copy(element_spec)
aggregate_spec = element_spec.copy()
aggregate_spec['address'] = dict(required=True)
argument_spec = dict(
@ -208,7 +206,7 @@ def main():
requests = list()
for param in params:
item = copy(param)
item = param.copy()
if item['state'] == 'present':
if not item['address'] and item['next_hop']:
module.fail_json(msg="parameters are required together: ['address', 'next_hop']")

View file

@ -110,8 +110,6 @@ diff.prepared:
"""
import collections
from copy import copy
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.junos import load_config, map_params_to_obj, map_obj_to_ele, to_param_list
@ -151,7 +149,7 @@ def main():
state=dict(default='present', choices=['present', 'absent']),
active=dict(default=True, type='bool')
)
aggregate_spec = copy(element_spec)
aggregate_spec = element_spec.copy()
aggregate_spec['name'] = dict(required=True)
aggregate_spec['vlan_id'] = dict(required=True, type='int')
@ -199,7 +197,7 @@ def main():
requests = list()
for param in params:
item = copy(param)
item = param.copy()
validate_param_values(module, param_to_xpath_map, param=item)
want = map_params_to_obj(module, param_to_xpath_map, param=item)

View file

@ -164,8 +164,6 @@ diff.prepared:
"""
import collections
from copy import copy
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.junos import load_config, map_params_to_obj, map_obj_to_ele, to_param_list
@ -192,7 +190,7 @@ def main():
active=dict(default=True, type='bool')
)
aggregate_spec = copy(element_spec)
aggregate_spec = element_spec.copy()
aggregate_spec['name'] = dict(required=True)
argument_spec = dict(
@ -241,7 +239,7 @@ def main():
requests = list()
for param in params:
item = copy(param)
item = param.copy()
item['type'] = 'vrf'