diff --git a/lib/ansible/modules/network/junos/junos_interface.py b/lib/ansible/modules/network/junos/junos_interface.py index 5919086b847..a8866ccc4e1 100644 --- a/lib/ansible/modules/network/junos/junos_interface.py +++ b/lib/ansible/modules/network/junos/junos_interface.py @@ -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 diff --git a/lib/ansible/modules/network/junos/junos_l3_interface.py b/lib/ansible/modules/network/junos/junos_l3_interface.py index f1a86407cf4..68b490de764 100644 --- a/lib/ansible/modules/network/junos/junos_l3_interface.py +++ b/lib/ansible/modules/network/junos/junos_l3_interface.py @@ -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") diff --git a/lib/ansible/modules/network/junos/junos_linkagg.py b/lib/ansible/modules/network/junos/junos_linkagg.py index 0c320b2dac0..bcf546667c9 100644 --- a/lib/ansible/modules/network/junos/junos_linkagg.py +++ b/lib/ansible/modules/network/junos/junos_linkagg.py @@ -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() - if state in ('present', 'up', 'down'): - module.params['state'] = 'present' + state = item.get('state') + item['disable'] = True if state == 'down' else False - else: - module.params['disable'] = True + if state in ('present', 'up', 'down'): + item['state'] = 'present' - if module.params.get('mode') == 'off': - module.params['mode'] = '' - elif module.params.get('mode') == 'on': - module.params['mode'] = 'passive' + else: + item['disable'] = True + 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: diff --git a/lib/ansible/modules/network/junos/junos_lldp.py b/lib/ansible/modules/network/junos/junos_lldp.py index 4706ac51592..33fe7c5e271 100644 --- a/lib/ansible/modules/network/junos/junos_lldp.py +++ b/lib/ansible/modules/network/junos/junos_lldp.py @@ -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) diff --git a/lib/ansible/modules/network/junos/junos_lldp_interface.py b/lib/ansible/modules/network/junos/junos_lldp_interface.py index 1f522c9cb6e..bc568ec1079 100644 --- a/lib/ansible/modules/network/junos/junos_lldp_interface.py +++ b/lib/ansible/modules/network/junos/junos_lldp_interface.py @@ -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) diff --git a/lib/ansible/modules/network/junos/junos_logging.py b/lib/ansible/modules/network/junos/junos_logging.py index 8bc0dc3b88b..7077704b867 100644 --- a/lib/ansible/modules/network/junos/junos_logging.py +++ b/lib/ansible/modules/network/junos/junos_logging.py @@ -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')) diff --git a/lib/ansible/modules/network/junos/junos_static_route.py b/lib/ansible/modules/network/junos/junos_static_route.py index 5204e929e29..bfff06dc773 100644 --- a/lib/ansible/modules/network/junos/junos_static_route.py +++ b/lib/ansible/modules/network/junos/junos_static_route.py @@ -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']") diff --git a/lib/ansible/modules/network/junos/junos_vlan.py b/lib/ansible/modules/network/junos/junos_vlan.py index 45068df8d1d..7b19366030c 100644 --- a/lib/ansible/modules/network/junos/junos_vlan.py +++ b/lib/ansible/modules/network/junos/junos_vlan.py @@ -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) diff --git a/lib/ansible/modules/network/junos/junos_vrf.py b/lib/ansible/modules/network/junos/junos_vrf.py index 07749fe1cca..45bd7d0a327 100644 --- a/lib/ansible/modules/network/junos/junos_vrf.py +++ b/lib/ansible/modules/network/junos/junos_vrf.py @@ -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'