def main(): """ main entry point for module execution """ element_spec = dict(name=dict(), description=dict(), rd=dict(type='list'), interfaces=dict(type='list'), target=dict(type='list'), state=dict(default='present', choices=['present', 'absent']), active=dict(default=True, type='bool'), table_label=dict(default=True, type='bool')) aggregate_spec = deepcopy(element_spec) aggregate_spec['name'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict(aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [['aggregate', 'name']] mutually_exclusive = [['aggregate', 'name']] module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive) warnings = list() result = {'changed': False} if warnings: result['warnings'] = warnings top = 'routing-instances/instance' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ('name', { 'xpath': 'name', 'is_key': True }), ('description', 'description'), ('type', 'instance-type'), ('rd', 'route-distinguisher/rd-type'), ('interfaces', 'interface/name'), ('target', 'vrf-target/community'), ('table_label', { 'xpath': 'vrf-table-label', 'tag_only': True }), ]) params = to_param_list(module) requests = list() for param in params: # 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] item = param.copy() item['type'] = 'vrf' want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') 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)
def main(): """ main entry point for module execution """ element_spec = dict( address=dict(type='str', aliases=['prefix']), next_hop=dict(type='str'), preference=dict(type='int', aliases=['admin_distance']), qualified_next_hop=dict(type='str'), qualified_preference=dict(type='int'), state=dict(default='present', choices=['present', 'absent']), active=dict(default=True, type='bool') ) aggregate_spec = copy(element_spec) aggregate_spec['address'] = 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 = [['aggregate', 'address']] mutually_exclusive = [['aggregate', 'address'], ['aggregate', 'next_hop'], ['aggregate', 'preference'], ['aggregate', 'qualified_next_hop'], ['aggregate', 'qualified_preference'], ['aggregate', 'state'], ['aggregate', 'active']] module = AnsibleModule(argument_spec=argument_spec, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings top = 'routing-options/static/route' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ('address', {'xpath': 'name', 'is_key': True}), ('next_hop', 'next-hop'), ('preference', 'preference/metric-value'), ('qualified_next_hop', {'xpath': 'name', 'top': 'qualified-next-hop'}), ('qualified_preference', {'xpath': 'preference', 'top': 'qualified-next-hop'}) ]) params = to_param_list(module) requests = list() for param in params: item = copy(param) if item['state'] == 'present': if not item['address'] and item['next_hop']: module.fail_json(msg="parameters are required together: ['address', 'next_hop']") want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='replace') 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)
def main(): """ main entry point for module execution """ element_spec = dict( name=dict(), ipv4=dict(), ipv6=dict(), unit=dict(default=0, type='int'), state=dict(default='present', choices=['present', 'absent']), active=dict(default=True, type='bool') ) aggregate_spec = deepcopy(element_spec) aggregate_spec['name'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict( aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [['name', 'aggregate']] mutually_exclusive = [['name', 'aggregate']] module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, mutually_exclusive=mutually_exclusive, required_one_of=required_one_of) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings top = 'interfaces/interface' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ('name', {'xpath': 'name', 'parent_attrib': False, 'is_key': True}), ('unit', {'xpath': 'name', 'top': 'unit', 'parent_attrib': False, 'is_key': True}), ('ipv4', {'xpath': 'inet/address/name', 'top': 'unit/family', 'is_key': True}), ('ipv6', {'xpath': 'inet6/address/name', 'top': 'unit/family', 'is_key': True}) ]) params = to_param_list(module) requests = list() for param in params: # 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] item = param.copy() if not item['ipv4'] and not item['ipv6']: module.fail_json(msg="one of the following is required: ipv4,ipv6") want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='replace') 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)
def main(): """ main entry point for module execution """ element_spec = dict(name=dict(), mode=dict(default='on', choices=['on', 'off', 'active', 'passive']), members=dict(type='list'), min_links=dict(type='int'), device_count=dict(type='int'), description=dict(default=DEFAULT_COMMENT), state=dict(default='present', choices=['present', 'absent', 'up', 'down']), active=dict(default=True, type='bool')) aggregate_spec = deepcopy(element_spec) aggregate_spec['name'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict(aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [['name', 'aggregate']] mutually_exclusive = [['name', 'aggregate']] module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings params = to_param_list(module) requests = list() for param in params: # 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] item = param.copy() state = item.get('state') item['disable'] = True if state == 'down' else False if state in ('present', 'up', 'down'): item['state'] = 'present' 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): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') 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)
def main(): """ main entry point for module execution """ element_spec = dict( name=dict(), description=dict(), rd=dict(type='list'), interfaces=dict(type='list'), target=dict(type='list'), state=dict(default='present', choices=['present', 'absent']), active=dict(default=True, type='bool'), table_label=dict(default=True, type='bool') ) aggregate_spec = deepcopy(element_spec) aggregate_spec['name'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict( aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [['aggregate', 'name']] mutually_exclusive = [['aggregate', 'name']] module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings top = 'routing-instances/instance' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ('name', {'xpath': 'name', 'is_key': True}), ('description', 'description'), ('type', 'instance-type'), ('rd', 'route-distinguisher/rd-type'), ('interfaces', 'interface/name'), ('target', 'vrf-target/community'), ('table_label', {'xpath': 'vrf-table-label', 'tag_only': True}), ]) params = to_param_list(module) requests = list() for param in params: # 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] item = param.copy() item['type'] = 'vrf' want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') 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)
def main(): """ main entry point for module execution """ neighbors_spec = dict(host=dict(), port=dict()) element_spec = dict(name=dict(), description=dict(), enabled=dict(default=True, type='bool'), speed=dict(), mtu=dict(type='int'), duplex=dict(choices=['full', 'half', 'auto']), tx_rate=dict(), rx_rate=dict(), neighbors=dict(type='list', elements='dict', options=neighbors_spec), delay=dict(default=10, type='int'), state=dict(default='present', choices=['present', 'absent', 'up', 'down']), active=dict(default=True, type='bool')) aggregate_spec = deepcopy(element_spec) aggregate_spec['name'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict(aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [['name', 'aggregate']] mutually_exclusive = [['name', 'aggregate']] module = AnsibleModule(argument_spec=argument_spec, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() result = {'changed': False} if warnings: result['warnings'] = warnings top = 'interfaces/interface' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([('name', { 'xpath': 'name', 'is_key': True }), ('description', 'description'), ('speed', 'speed'), ('mtu', 'mtu'), ('duplex', 'link-mode'), ('disable', { 'xpath': 'disable', 'tag_only': True })]) choice_to_value_map = { 'link-mode': { 'full': 'full-duplex', 'half': 'half-duplex', 'auto': 'automatic' } } params = to_param_list(module) requests = list() for param in params: # 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] item = param.copy() state = item.get('state') item['disable'] = True if not item.get('enabled') else False if state in ('present', 'up', 'down'): item['state'] = 'present' validate_param_values(module, param_to_xpath_map, param=item) want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append( map_obj_to_ele(module, want, top, value_map=choice_to_value_map, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') # issue commit after last configuration change is done 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} failed_conditions = [] neighbors = None for item in params: state = item.get('state') tx_rate = item.get('tx_rate') rx_rate = item.get('rx_rate') want_neighbors = item.get('neighbors') if state not in ( 'up', 'down' ) and tx_rate is None and rx_rate is None and want_neighbors is None: continue element = Element('get-interface-information') intf_name = SubElement(element, 'interface-name') intf_name.text = item.get('name') if result['changed']: sleep(item.get('delay')) reply = exec_rpc(module, tostring(element), ignore_warning=False) if state in ('up', 'down'): admin_status = reply.xpath( 'interface-information/physical-interface/admin-status') if not admin_status or not conditional( state, admin_status[0].text.strip()): failed_conditions.append('state ' + 'eq(%s)' % state) if tx_rate: output_bps = reply.xpath( 'interface-information/physical-interface/traffic-statistics/output-bps' ) if not output_bps or not conditional( tx_rate, output_bps[0].text.strip(), cast=int): failed_conditions.append('tx_rate ' + tx_rate) if rx_rate: input_bps = reply.xpath( 'interface-information/physical-interface/traffic-statistics/input-bps' ) if not input_bps or not conditional( rx_rate, input_bps[0].text.strip(), cast=int): failed_conditions.append('rx_rate ' + rx_rate) if want_neighbors: if neighbors is None: element = Element('get-lldp-interface-neighbors') intf_name = SubElement(element, 'interface-device') intf_name.text = item.get('name') reply = exec_rpc(module, tostring(element), ignore_warning=False) have_host = [ item.text for item in reply.xpath( 'lldp-neighbors-information/lldp-neighbor-information/lldp-remote-system-name' ) ] have_port = [ item.text for item in reply.xpath( 'lldp-neighbors-information/lldp-neighbor-information/lldp-remote-port-id' ) ] for neighbor in want_neighbors: host = neighbor.get('host') port = neighbor.get('port') if host and host not in have_host: failed_conditions.append('host ' + host) if port and port not in have_port: failed_conditions.append('port ' + port) if failed_conditions: msg = 'One or more conditional statements have not be satisfied' module.fail_json(msg=msg, failed_conditions=failed_conditions) module.exit_json(**result)
def main(): """ main entry point for module execution """ element_spec = dict( name=dict(), ipv4=dict(), ipv6=dict(), unit=dict(default=0, type='int'), state=dict(default='present', choices=['present', 'absent']), active=dict(default=True, type='bool') ) aggregate_spec = deepcopy(element_spec) aggregate_spec['name'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict( aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [['name', 'aggregate']] mutually_exclusive = [['name', 'aggregate']] module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, mutually_exclusive=mutually_exclusive, required_one_of=required_one_of) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings top = 'interfaces/interface' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ('name', {'xpath': 'name', 'parent_attrib': False, 'is_key': True}), ('unit', {'xpath': 'name', 'top': 'unit', 'parent_attrib': False, 'is_key': True}), ('ipv4', {'xpath': 'inet/address/name', 'top': 'unit/family', 'is_key': True}), ('ipv6', {'xpath': 'inet6/address/name', 'top': 'unit/family', 'is_key': True}) ]) params = to_param_list(module) requests = list() for param in params: # 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] item = param.copy() if not item['ipv4'] and not item['ipv6']: module.fail_json(msg="one of the following is required: ipv4,ipv6") want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') 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)
def main(): """ main entry point for module execution """ element_spec = dict(dest=dict(choices=['console', 'host', 'file', 'user']), name=dict(), facility=dict(), level=dict(), rotate_frequency=dict(type='int'), size=dict(type='int'), files=dict(type='int'), src_addr=dict(), state=dict(default='present', choices=['present', 'absent']), active=dict(default=True, type='bool')) aggregate_spec = deepcopy(element_spec) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict(aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_if = [('dest', 'host', ['name', 'facility', 'level']), ('dest', 'file', ['name', 'facility', 'level']), ('dest', 'user', ['name', 'facility', 'level']), ('dest', 'console', ['facility', 'level'])] module = AnsibleModule(argument_spec=argument_spec, required_if=required_if, supports_check_mode=True) warnings = list() result = {'changed': False} if warnings: result['warnings'] = warnings params = to_param_list(module) requests = list() for param in params: # 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] module._check_required_if(required_if, 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')) top = 'system/syslog' is_facility_key = False field_top = None if dest: if dest == 'console': field_top = dest is_facility_key = True else: field_top = dest + '/contents' is_facility_key = False param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ('name', { 'xpath': 'name', 'is_key': True, 'top': dest }), ('facility', { 'xpath': 'name', 'is_key': is_facility_key, 'top': field_top }), ('size', { 'xpath': 'size', 'leaf_only': True, 'is_key': True, 'top': 'archive' }), ('files', { 'xpath': 'files', 'leaf_only': True, 'is_key': True, 'top': 'archive' }), ('rotate_frequency', { 'xpath': 'log-rotate-frequency', 'leaf_only': True }), ]) if item.get('level'): param_to_xpath_map['level'] = { 'xpath': item.get('level'), 'tag_only': True, 'top': field_top } validate_param_values(module, param_to_xpath_map, param=item) want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') 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)
def main(): """ main entry point for module execution """ element_spec = dict( address=dict(aliases=['prefix']), next_hop=dict(), preference=dict(type='int', aliases=['admin_distance']), qualified_next_hop=dict(type='str'), qualified_preference=dict(type='int'), state=dict(default='present', choices=['present', 'absent']), active=dict(default=True, type='bool') ) aggregate_spec = deepcopy(element_spec) aggregate_spec['address'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) 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 = [['aggregate', 'address']] mutually_exclusive = [['aggregate', 'address']] module = AnsibleModule(argument_spec=argument_spec, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings top = 'routing-options/static/route' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ('address', {'xpath': 'name', 'is_key': True}), ('next_hop', 'next-hop'), ('preference', 'preference/metric-value'), ('qualified_next_hop', {'xpath': 'name', 'top': 'qualified-next-hop'}), ('qualified_preference', {'xpath': 'preference', 'top': 'qualified-next-hop'}) ]) params = to_param_list(module) requests = list() for param in params: # 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] 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']") want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') 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)
def main(): """ main entry point for module execution """ element_spec = dict(name=dict(), description=dict(), enabled=dict(), speed=dict(), mtu=dict(type='int'), duplex=dict(choices=['full', 'half', 'auto']), tx_rate=dict(), rx_rate=dict(), state=dict(default='present', choices=['present', 'absent', 'up', 'down']), active=dict(default=True, type='bool')) aggregate_spec = copy(element_spec) 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', 'aggregate']] mutually_exclusive = [['name', 'aggregate'], ['state', 'aggregate'], ['active', 'aggregate']] module = AnsibleModule(argument_spec=argument_spec, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings top = 'interfaces/interface' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([('name', { 'xpath': 'name', 'is_key': True }), ('description', 'description'), ('speed', 'speed'), ('mtu', 'mtu'), ('duplex', 'link-mode'), ('disable', { 'xpath': 'disable', 'tag_only': True })]) choice_to_value_map = { 'link-mode': { 'full': 'full-duplex', 'half': 'half-duplex', 'auto': 'automatic' } } params = to_param_list(module) requests = list() for param in params: item = copy(param) state = item.get('state') item['disable'] = True if state == 'down' else False if state in ('present', 'up', 'down'): item['state'] = 'present' else: item['disable'] = True validate_param_values(module, param_to_xpath_map, param=item) want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append( map_obj_to_ele(module, want, top, value_map=choice_to_value_map, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='replace') # issue commit after last configuration change is done 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)
def main(): """ main entry point for module execution """ element_spec = dict(name=dict(), vlan_id=dict(type='int'), description=dict(), interfaces=dict(), state=dict(default='present', choices=['present', 'absent']), active=dict(default=True, type='bool')) aggregate_spec = copy(element_spec) aggregate_spec['name'] = dict(required=True) aggregate_spec['vlan_id'] = dict(required=True, type='int') 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 = [['aggregate', 'name']] required_together = [['name', 'vlan_id']] mutually_exclusive = [['aggregate', 'name'], ['aggregate', 'vlan_id'], ['aggregate', 'description'], ['aggregate', 'interfaces'], ['aggregate', 'state'], ['aggregate', 'active']] module = AnsibleModule(argument_spec=argument_spec, required_one_of=required_one_of, required_together=required_together, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings top = 'vlans/vlan' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([('name', { 'xpath': 'name', 'is_key': True }), ('vlan_id', 'vlan-id'), ('description', 'description')]) params = to_param_list(module) requests = list() for param in params: item = copy(param) validate_param_values(module, param_to_xpath_map, param=item) want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, param=item)) with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='replace') 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)
def main(): """ main entry point for module execution """ neighbors_spec = dict( host=dict(), port=dict() ) element_spec = dict( name=dict(), description=dict(), enabled=dict(default=True, type='bool'), speed=dict(), mtu=dict(type='int'), duplex=dict(choices=['full', 'half', 'auto']), tx_rate=dict(), rx_rate=dict(), neighbors=dict(type='list', elements='dict', options=neighbors_spec), delay=dict(default=10, type='int'), state=dict(default='present', choices=['present', 'absent', 'up', 'down']), active=dict(default=True, type='bool') ) aggregate_spec = deepcopy(element_spec) aggregate_spec['name'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict( aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [['name', 'aggregate']] mutually_exclusive = [['name', 'aggregate']] module = AnsibleModule(argument_spec=argument_spec, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings top = 'interfaces/interface' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ('name', {'xpath': 'name', 'is_key': True}), ('description', 'description'), ('speed', 'speed'), ('mtu', 'mtu'), ('duplex', 'link-mode'), ('disable', {'xpath': 'disable', 'tag_only': True}) ]) choice_to_value_map = { 'link-mode': {'full': 'full-duplex', 'half': 'half-duplex', 'auto': 'automatic'} } params = to_param_list(module) requests = list() for param in params: # 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] item = param.copy() state = item.get('state') item['disable'] = True if not item.get('enabled') else False if state in ('present', 'up', 'down'): item['state'] = 'present' validate_param_values(module, param_to_xpath_map, param=item) want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append(map_obj_to_ele(module, want, top, value_map=choice_to_value_map, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') # issue commit after last configuration change is done 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} failed_conditions = [] neighbors = None for item in params: state = item.get('state') tx_rate = item.get('tx_rate') rx_rate = item.get('rx_rate') want_neighbors = item.get('neighbors') if state not in ('up', 'down') and tx_rate is None and rx_rate is None and want_neighbors is None: continue element = Element('get-interface-information') intf_name = SubElement(element, 'interface-name') intf_name.text = item.get('name') if result['changed']: sleep(item.get('delay')) reply = send_request(module, element, ignore_warning=False) if state in ('up', 'down'): admin_status = reply.xpath('interface-information/physical-interface/admin-status') if not admin_status or not conditional(state, admin_status[0].text.strip()): failed_conditions.append('state ' + 'eq(%s)' % state) if tx_rate: output_bps = reply.xpath('interface-information/physical-interface/traffic-statistics/output-bps') if not output_bps or not conditional(tx_rate, output_bps[0].text.strip(), cast=int): failed_conditions.append('tx_rate ' + tx_rate) if rx_rate: input_bps = reply.xpath('interface-information/physical-interface/traffic-statistics/input-bps') if not input_bps or not conditional(rx_rate, input_bps[0].text.strip(), cast=int): failed_conditions.append('rx_rate ' + rx_rate) if want_neighbors: if neighbors is None: element = Element('get-lldp-interface-neighbors') intf_name = SubElement(element, 'interface-device') intf_name.text = item.get('name') reply = send_request(module, element, ignore_warning=False) have_host = [item.text for item in reply.xpath('lldp-neighbors-information/lldp-neighbor-information/lldp-remote-system-name')] have_port = [item.text for item in reply.xpath('lldp-neighbors-information/lldp-neighbor-information/lldp-remote-port-id')] for neighbor in want_neighbors: host = neighbor.get('host') port = neighbor.get('port') if host and host not in have_host: failed_conditions.append('host ' + host) if port and port not in have_port: failed_conditions.append('port ' + port) if failed_conditions: msg = 'One or more conditional statements have not be satisfied' module.fail_json(msg=msg, failed_conditions=failed_conditions) module.exit_json(**result)
def main(): """ main entry point for module execution """ element_spec = dict( name=dict(), mode=dict(default='on', choices=['on', 'off', 'active', 'passive']), members=dict(type='list'), min_links=dict(type='int'), device_count=dict(type='int'), description=dict(), state=dict(default='present', choices=['present', 'absent', 'up', 'down']), active=dict(default=True, type='bool') ) aggregate_spec = deepcopy(element_spec) aggregate_spec['name'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict( aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [['name', 'aggregate']] mutually_exclusive = [['name', 'aggregate']] module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive) warnings = list() check_args(module, warnings) result = {'changed': False} if warnings: result['warnings'] = warnings params = to_param_list(module) requests = list() for param in params: # 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] item = param.copy() state = item.get('state') item['disable'] = True if state == 'down' else False if state in ('present', 'up', 'down'): item['state'] = 'present' 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): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') 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)