2017-07-11 04:22:53 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2017, Ansible by Red Hat, inc
|
2017-08-04 09:25:58 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-07-11 04:22:53 +00:00
|
|
|
'status': ['preview'],
|
2017-08-16 04:10:36 +00:00
|
|
|
'supported_by': 'network'}
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: junos_linkagg
|
|
|
|
version_added: "2.4"
|
|
|
|
author: "Ganesh Nalawade (@ganeshrn)"
|
|
|
|
short_description: Manage link aggregation groups on Juniper JUNOS network devices
|
|
|
|
description:
|
|
|
|
- This module provides declarative management of link aggregation groups
|
|
|
|
on Juniper JUNOS network devices.
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the link aggregation group.
|
|
|
|
required: true
|
|
|
|
mode:
|
|
|
|
description:
|
|
|
|
- Mode of the link aggregation group. A value of C(on) will enable LACP in C(passive) mode.
|
|
|
|
C(active) configures the link to actively information about the state of the link,
|
|
|
|
or it can be configured in C(passive) mode ie. send link state information only when
|
|
|
|
received them from another link. A value of C(off) will disable LACP.
|
|
|
|
default: off
|
|
|
|
choices: ['on', 'off', 'active', 'passive']
|
|
|
|
members:
|
|
|
|
description:
|
|
|
|
- List of members interfaces of the link aggregation group. The value can be
|
|
|
|
single interface or list of interfaces.
|
|
|
|
required: true
|
|
|
|
min_links:
|
|
|
|
description:
|
|
|
|
- Minimum members that should be up
|
|
|
|
before bringing up the link aggregation group.
|
|
|
|
device_count:
|
|
|
|
description:
|
|
|
|
- Number of aggregated ethernet devices that can be configured.
|
|
|
|
Acceptable integer value is between 1 and 128.
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Description of Interface.
|
2017-08-08 06:43:28 +00:00
|
|
|
aggregate:
|
|
|
|
description: List of link aggregation definitions.
|
2017-07-11 04:22:53 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- State of the link aggregation group.
|
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent', 'up', 'down']
|
|
|
|
active:
|
|
|
|
description:
|
|
|
|
- Specifies whether or not the configuration is active or deactivated
|
|
|
|
default: True
|
|
|
|
choices: [True, False]
|
|
|
|
requirements:
|
|
|
|
- ncclient (>=v0.5.2)
|
|
|
|
notes:
|
|
|
|
- This module requires the netconf system service be enabled on
|
2017-08-14 04:47:03 +00:00
|
|
|
the remote device being managed.
|
2017-08-24 14:31:47 +00:00
|
|
|
- Tested against vSRX JUNOS version 15.1X49-D15.4, vqfx-10000 JUNOS Version 15.1X53-D60.4.
|
2018-04-17 04:57:29 +00:00
|
|
|
- Recommended connection is C(netconf). See L(the Junos OS Platform Options,../network/user_guide/platform_junos.html).
|
|
|
|
- This module also works with C(local) connections for legacy playbooks.
|
2018-01-17 21:31:53 +00:00
|
|
|
extends_documentation_fragment: junos
|
2017-07-11 04:22:53 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: configure link aggregation
|
|
|
|
junos_linkagg:
|
|
|
|
name: ae11
|
|
|
|
members:
|
|
|
|
- ge-0/0/5
|
|
|
|
- ge-0/0/6
|
|
|
|
- ge-0/0/7
|
|
|
|
lacp: active
|
|
|
|
device_count: 4
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: delete link aggregation
|
|
|
|
junos_linkagg:
|
|
|
|
name: ae11
|
|
|
|
members:
|
|
|
|
- ge-0/0/5
|
|
|
|
- ge-0/0/6
|
|
|
|
- ge-0/0/7
|
|
|
|
lacp: active
|
|
|
|
device_count: 4
|
|
|
|
state: delete
|
|
|
|
|
|
|
|
- name: deactivate link aggregation
|
|
|
|
junos_linkagg:
|
|
|
|
name: ae11
|
|
|
|
members:
|
|
|
|
- ge-0/0/5
|
|
|
|
- ge-0/0/6
|
|
|
|
- ge-0/0/7
|
|
|
|
lacp: active
|
|
|
|
device_count: 4
|
|
|
|
state: present
|
|
|
|
active: False
|
|
|
|
|
|
|
|
- name: Activate link aggregation
|
|
|
|
junos_linkagg:
|
|
|
|
name: ae11
|
|
|
|
members:
|
|
|
|
- ge-0/0/5
|
|
|
|
- ge-0/0/6
|
|
|
|
- ge-0/0/7
|
|
|
|
lacp: active
|
|
|
|
device_count: 4
|
|
|
|
state: present
|
|
|
|
active: True
|
|
|
|
|
|
|
|
- name: Disable link aggregation
|
|
|
|
junos_linkagg:
|
|
|
|
name: ae11
|
|
|
|
state: down
|
|
|
|
|
|
|
|
- name: Enable link aggregation
|
|
|
|
junos_linkagg:
|
|
|
|
name: ae11
|
|
|
|
state: up
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
diff:
|
|
|
|
description: Configuration difference before and after applying change.
|
|
|
|
returned: when configuration is changed and diff option is enabled.
|
|
|
|
type: string
|
|
|
|
sample: >
|
|
|
|
[edit interfaces]
|
|
|
|
+ ge-0/0/6 {
|
|
|
|
+ ether-options {
|
|
|
|
+ 802.3ad ae0;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
[edit interfaces ge-0/0/7]
|
|
|
|
+ ether-options {
|
|
|
|
+ 802.3ad ae0;
|
|
|
|
+ }
|
|
|
|
[edit interfaces]
|
|
|
|
+ ae0 {
|
|
|
|
+ description "configured by junos_linkagg";
|
|
|
|
+ aggregated-ether-options {
|
|
|
|
+ lacp {
|
|
|
|
+ active;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
"""
|
|
|
|
import collections
|
|
|
|
|
2017-08-14 04:47:03 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
2017-07-11 04:22:53 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.common.utils import remove_default_spec
|
|
|
|
from ansible.module_utils.network.junos.junos import junos_argument_spec
|
|
|
|
from ansible.module_utils.network.junos.junos import load_config, map_params_to_obj, map_obj_to_ele, to_param_list
|
|
|
|
from ansible.module_utils.network.junos.junos import commit_configuration, discard_changes, locked_config, get_configuration
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from lxml.etree import tostring
|
|
|
|
except ImportError:
|
|
|
|
from xml.etree.ElementTree import tostring
|
|
|
|
|
|
|
|
USE_PERSISTENT_CONNECTION = True
|
|
|
|
|
|
|
|
|
|
|
|
def validate_device_count(value, module):
|
|
|
|
if value and not 1 <= value <= 128:
|
|
|
|
module.fail_json(msg='device_count must be between 1 and 128')
|
|
|
|
|
|
|
|
|
|
|
|
def validate_min_links(value, module):
|
|
|
|
if value and not 1 <= value <= 8:
|
|
|
|
module.fail_json(msg='min_links must be between 1 and 8')
|
|
|
|
|
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
def validate_param_values(module, obj, item):
|
2017-07-11 04:22:53 +00:00
|
|
|
for key in obj:
|
|
|
|
# validate the param value (if validator func exists)
|
|
|
|
validator = globals().get('validate_%s' % key)
|
|
|
|
if callable(validator):
|
2017-08-08 06:43:28 +00:00
|
|
|
validator(item.get(key), module)
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
def configure_lag_params(module, requests, item):
|
2017-07-11 04:22:53 +00:00
|
|
|
top = 'interfaces/interface'
|
|
|
|
param_lag_to_xpath_map = collections.OrderedDict()
|
|
|
|
param_lag_to_xpath_map.update([
|
|
|
|
('name', {'xpath': 'name', 'is_key': True}),
|
|
|
|
('description', 'description'),
|
|
|
|
('min_links', {'xpath': 'minimum-links', 'top': 'aggregated-ether-options'}),
|
|
|
|
('disable', {'xpath': 'disable', 'tag_only': True}),
|
2017-08-08 06:43:28 +00:00
|
|
|
('mode', {'xpath': item['mode'], 'tag_only': True, 'top': 'aggregated-ether-options/lacp'}),
|
2017-07-11 04:22:53 +00:00
|
|
|
])
|
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
validate_param_values(module, param_lag_to_xpath_map, item)
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
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)
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
if item['device_count']:
|
2017-07-11 04:22:53 +00:00
|
|
|
top = 'chassis/aggregated-devices/ethernet'
|
|
|
|
device_count_to_xpath_map = {'device_count': {'xpath': 'device-count', 'leaf_only': True}}
|
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
validate_param_values(module, device_count_to_xpath_map, item)
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
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)
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
def configure_member_params(module, requests, item):
|
2017-07-11 04:22:53 +00:00
|
|
|
top = 'interfaces/interface'
|
2017-08-08 06:43:28 +00:00
|
|
|
members = item['members']
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
if members:
|
|
|
|
member_to_xpath_map = collections.OrderedDict()
|
|
|
|
member_to_xpath_map.update([
|
|
|
|
('name', {'xpath': 'name', 'is_key': True, 'parent_attrib': False}),
|
|
|
|
('bundle', {'xpath': 'bundle', 'leaf_only': True, 'top': 'ether-options/ieee-802.3ad', 'is_key': True}),
|
|
|
|
])
|
|
|
|
|
|
|
|
# link aggregation bundle assigned to member
|
2017-08-08 06:43:28 +00:00
|
|
|
item['bundle'] = item['name']
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
for member in members:
|
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
if item['state'] == 'absent':
|
2017-07-11 04:22:53 +00:00
|
|
|
# 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/"
|
2017-08-08 06:43:28 +00:00
|
|
|
"ieee-802.3ad[bundle='%s']" % (member, item['bundle']))
|
2017-07-11 04:22:53 +00:00
|
|
|
if not bundle:
|
|
|
|
continue
|
|
|
|
# Name of member to be assigned to link aggregation bundle
|
2017-08-08 06:43:28 +00:00
|
|
|
item['name'] = member
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
validate_param_values(module, member_to_xpath_map, item)
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
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)
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
""" main entry point for module execution
|
|
|
|
"""
|
2017-08-08 06:43:28 +00:00
|
|
|
element_spec = dict(
|
|
|
|
name=dict(),
|
2017-08-14 04:47:03 +00:00
|
|
|
mode=dict(default='on', choices=['on', 'off', 'active', 'passive']),
|
2017-07-11 04:22:53 +00:00
|
|
|
members=dict(type='list'),
|
|
|
|
min_links=dict(type='int'),
|
|
|
|
device_count=dict(type='int'),
|
2017-08-18 18:39:01 +00:00
|
|
|
description=dict(),
|
2017-07-11 04:22:53 +00:00
|
|
|
state=dict(default='present', choices=['present', 'absent', 'up', 'down']),
|
|
|
|
active=dict(default=True, type='bool')
|
|
|
|
)
|
|
|
|
|
2017-08-14 04:47:03 +00:00
|
|
|
aggregate_spec = deepcopy(element_spec)
|
2017-08-08 06:43:28 +00:00
|
|
|
aggregate_spec['name'] = dict(required=True)
|
|
|
|
|
2017-08-14 04:47:03 +00:00
|
|
|
# remove default in aggregate spec, to handle common arguments
|
|
|
|
remove_default_spec(aggregate_spec)
|
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
argument_spec = dict(
|
|
|
|
aggregate=dict(type='list', elements='dict', options=aggregate_spec),
|
|
|
|
)
|
|
|
|
|
|
|
|
argument_spec.update(element_spec)
|
2017-07-11 04:22:53 +00:00
|
|
|
argument_spec.update(junos_argument_spec)
|
2017-08-08 06:43:28 +00:00
|
|
|
|
|
|
|
required_one_of = [['name', 'aggregate']]
|
2017-08-14 04:47:03 +00:00
|
|
|
mutually_exclusive = [['name', 'aggregate']]
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
2017-08-08 06:43:28 +00:00
|
|
|
supports_check_mode=True,
|
2017-07-11 04:22:53 +00:00
|
|
|
required_one_of=required_one_of,
|
2017-08-08 06:43:28 +00:00
|
|
|
mutually_exclusive=mutually_exclusive)
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
warnings = list()
|
|
|
|
result = {'changed': False}
|
|
|
|
|
|
|
|
if warnings:
|
|
|
|
result['warnings'] = warnings
|
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
params = to_param_list(module)
|
|
|
|
requests = list()
|
|
|
|
for param in params:
|
2017-08-14 04:47:03 +00:00
|
|
|
# if key doesn't exist in the item, get it from module.params
|
|
|
|
for key in param:
|
|
|
|
if param.get(key) is None:
|
|
|
|
param[key] = module.params[key]
|
2017-08-08 06:43:28 +00:00
|
|
|
|
2017-08-14 04:47:03 +00:00
|
|
|
item = param.copy()
|
2017-08-08 06:43:28 +00:00
|
|
|
state = item.get('state')
|
|
|
|
item['disable'] = True if state == 'down' else False
|
|
|
|
|
|
|
|
if state in ('present', 'up', 'down'):
|
|
|
|
item['state'] = 'present'
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
else:
|
|
|
|
item['disable'] = True
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
mode = item.get('mode')
|
|
|
|
if mode == 'off':
|
|
|
|
item['mode'] = ''
|
|
|
|
elif mode == 'on':
|
|
|
|
item['mode'] = 'passive'
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
configure_lag_params(module, requests, item)
|
|
|
|
configure_member_params(module, requests, item)
|
2017-07-11 04:22:53 +00:00
|
|
|
|
2017-08-08 06:43:28 +00:00
|
|
|
diff = None
|
2017-07-11 04:22:53 +00:00
|
|
|
with locked_config(module):
|
2017-08-08 06:43:28 +00:00
|
|
|
for req in requests:
|
2017-08-18 04:20:02 +00:00
|
|
|
diff = load_config(module, tostring(req), warnings, action='merge')
|
2017-07-11 04:22:53 +00:00
|
|
|
|
|
|
|
commit = not module.check_mode
|
|
|
|
if diff:
|
|
|
|
if commit:
|
|
|
|
commit_configuration(module)
|
|
|
|
else:
|
|
|
|
discard_changes(module)
|
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
if module._diff:
|
|
|
|
result['diff'] = {'prepared': diff}
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|