def main(): _device_exists = False payload = '' module_args = dnac_argument_spec module_args.update(device_name=dict(type='str', required=False), device_mgmt_ip=dict(type='str', required=False), running_config=dict(type='str', required=False, default=False), startup_config=dict(type='str', required=False, default=False), vlans=dict(type='str', required=False, default=False), all=dict(type='str', required=False, default=True)) result = dict(changed=False, original_message='', message='') module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) # Instantiate the DnaCenter class object dnac = DnaCenter(module) # Get device details based on either the Management IP or the Name Provided if module.params['device_mgmt_ip'] is not None: dnac.api_path = 'api/v1/network-device?managementIpAddress=' + module.params[ 'device_mgmt_ip'] elif module.params['device_name'] is not None: dnac.api_path = 'api/v1/network-device?hostname=' + module.params[ 'device_name'] device_results = dnac.get_obj() device_id = device_results['response'][0]['id'] payload = { "deviceIds": [device_id], "configFileType": { "runningconfig": module.params['running_config'], "startupconfig": module.params['startup_config'], "vlan": module.params['vlans'], "all": module.params['all'] } } dnac.api_path = 'api/v1/archive-config' archive_config_results = dnac.create_obj(payload) if not archive_config_results.get('isError'): result['changed'] = True result['original_message'] = archive_config_results module.exit_json(msg='Device Config Archived Successfully.', **result) elif archive_config_results.get('isError'): result['changed'] = False result['original_message'] = archive_config_results module.fail_json(msg='Failed to Archive Device Configuration!', **result)
def main(): _setting_exists = False module_args = dnac_argument_spec module_args.update(dhcp_servers=dict(type='list', required=False), group_name=dict(type='str', default='-1')) module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) # Define Local Variables state = module.params['state'] dhcp_servers = module.params['dhcp_servers'] group_name = module.params['group_name'] # Build the payload dictionary payload = [{ "instanceType": "ip", "namespace": "global", "type": "ip.address", "key": "dhcp.server", "value": dhcp_servers, "groupUuid": "-1" }] # instansiate the dnac class dnac = DnaCenter(module) # obtain the groupUuid and update the payload dictionary group_id = dnac.get_group_id(group_name) # Set the api_path dnac.api_path = 'api/v1/commonsetting/global/' + group_id + '?key=dhcp.server' dnac.process_common_settings(payload, group_id)
def main(): _device_exists = False payload = '' module_args = dnac_argument_spec module_args.update(device_name=dict(type='str', required=False), device_mgmt_ip=dict(type='str', required=False), device_role=dict(type='str', required=True, choices=[ 'ACCESS', 'DISTRIBUTION', 'CORE', 'BORDER ROUTER', 'UNKOWN' ])) result = dict(changed=False, original_message='', message='') module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) # Instantiate the DnaCenter class object dnac = DnaCenter(module) # Get device details based on either the Management IP or the Name Provided if module.params['device_mgmt_ip'] is not None: dnac.api_path = 'api/v1/network-device?managementIpAddress=' + module.params[ 'device_mgmt_ip'] elif module.params['device_name'] is not None: dnac.api_path = 'api/v1/network-device?hostname=' + module.params[ 'device_name'] device_results = dnac.get_obj() current_device_role = device_results['response'][0]['role'] device_id = device_results['response'][0]['id'] if current_device_role != module.params['device_role']: dnac.api_path = 'api/v1/network-device/brief' payload = { 'id': device_id, 'role': module.params['device_role'], 'roleSource': 'MANUAL' } dnac.update_obj(payload) else: result['changed'] = False module.exit_json(msg='Device Already in desired Role')
def main(): _device_exists = False payload = '' module_args = dnac_argument_spec module_args.update(device_name=dict(type='str', required=False), device_mgmt_ip=dict(type='str', required=False)) result = dict(changed=False, original_message='', message='') module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) # Instantiate the DnaCenter class object dnac = DnaCenter(module) # Get device details based on either the Management IP or the Name Provided if module.params['device_mgmt_ip'] is not None: dnac.api_path = 'api/v1/network-device?managementIpAddress=' + module.params[ 'device_mgmt_ip'] elif module.params['device_name'] is not None: dnac.api_path = 'api/v1/network-device?hostname=' + module.params[ 'device_name'] # extract the device iD device_results = dnac.get_obj() device_id = device_results['response'][0]['id'] # delete the associated archives dnac.api_path = 'api/v1/archive-config/network-device' del_archive_config_results = dnac.delete_obj(device_id) if not del_archive_config_results.get('isError'): result['changed'] = True result['original_message'] = del_archive_config_results module.exit_json(msg='Device Config Archived Deleted Successfully.', **result) elif device_role_results.get('isError'): result['changed'] = False result['original_message'] = del_archive_config_results module.fail_json( msg='Failed to Delete Device Archived Configurations!', **result)
def main(): _banner_exists = False module_args = dnac_argument_spec module_args.update( banner_message=dict(type='str',default='', required=False), group_name=dict(type='str',default='-1'), retain_banner=dict(type='bool', default=True) ) module = AnsibleModule( argument_spec = module_args, supports_check_mode = True ) # Set Local Variables banner_message = module.params['banner_message'] retain_banner = module.params['retain_banner'] group_name = module.params['group_name'] state = module.params['state'] # Build the payload dictionary payload = [ {"instanceType":"banner", "namespace":"global", "type": "banner.setting", "key":"device.banner", "value": [ { "bannerMessage": banner_message, "retainExistingBanner": retain_banner }], "groupUuid":"-1" } ] # instansiate the dnac class dnac = DnaCenter(module) # obtain the groupUuid and update the payload dictionary group_id = dnac.get_group_id(group_name) # set the retain banner attribute if retain_banner: payload[0]['value'][0]['retainExistingBanner'] = True else: payload[0]['value'][0]['retainExistingBanner'] = False # set the api_path dnac.api_path = 'api/v1/commonsetting/global/' + group_id + '?key=device.banner' dnac.process_common_settings(payload, group_id)
def main(): module_args = dnac_argument_spec module_args.update(group_name=dict(type='str', default='-1', required=False), snmp_servers=dict(type='list', required=False), enable_dnac=dict(type='bool', required=False, default=True)) module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) # Set Local Variables state = module.params['state'] snmp_servers = module.params['snmp_servers'] group_name = module.params['group_name'] enable_dnac = module.params['enable_dnac'] # Build the payload dictionary payload = [{ "instanceType": "snmp", "namespace": "global", "type": "snmp.setting", "key": "snmp.trap.receiver", "value": [{ "ipAddresses": snmp_servers, "configureDnacIP": enable_dnac }], "groupUuid": "-1", }] # instansiate the dnac class dnac = DnaCenter(module) # obtain the groupUuid and update the payload dictionary group_id = dnac.get_group_id(group_name) # Set the api path dnac.api_path = 'api/v1/commonsetting/global/' + group_id + '?key=snmp.trap.receiver' # Process Setting Changes dnac.process_common_settings(payload, group_id)
def main(): module_args = dnac_argument_spec module_args.update( primary_dns_server=dict(type='str', required=False, default=''), secondary_dns_server=dict(type='str', required=False), domain_name=dict(type='str', required=False, default=''), group_name=dict(type='str', required=False, default='-1') ) module = AnsibleModule( argument_spec = module_args, supports_check_mode = True ) # Define local variables state = module.params['state'] domain_name = module.params['domain_name'] primary_dns_server = module.params['primary_dns_server'] secondary_dns_server = module.params['secondary_dns_server'] group_name = module.params['group_name'] # Build the payload dictionary payload = [ {"instanceType":"dns", "namespace":"global", "type": "dns.setting", "key":"dns.server", "value":[{ "domainName" : domain_name, "primaryIpAddress": primary_dns_server, "secondaryIpAddress": secondary_dns_server }], "groupUuid":"-1", } ] # instansiate the dnac class dnac = DnaCenter(module) # obtain the groupUuid and update the payload dictionary group_id = dnac.get_group_id(group_name) # Set the api_path dnac.api_path = 'api/v1/commonsetting/global/' + group_id + '?key=dns.server' dnac.process_common_settings(payload, group_id)
def main(): module_args = dnac_argument_spec module_args.update(timezone=dict(type='str', default='GMT'), group_name=dict(type='str', default='-1'), location=dict(type='str')) module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) # set local variables state = module.params['state'] group_name = module.params['group_name'] location = module.params['location'] timezone = module.params['timezone'] # Build the payload dictionary payload = [{ "instanceType": "timezone", "instanceUuid": "", "namespace": "global", "type": "timezone.setting", "key": "timezone.site", "value": [""], "groupUuid": "-1" }] # instansiate the dnac class dnac = DnaCenter(module) # obtain the groupUuid and update the payload dictionary group_id = dnac.get_group_id(group_name) # update payload with timezone if location: timezone = dnac.timezone_lookup(location) payload[0].update({'value': [timezone]}) # # check if the configuration is already in the desired state dnac.api_path = 'api/v1/commonsetting/global/' + group_id + '?key=timezone.site' # process common settings dnac.process_common_settings(payload, group_id)
def main(): _group_exists = False _parent_exists = False module_args = dnac_argument_spec module_args.update( state=dict(type='str', default='present', choices=['absent', 'present', 'update']), group_name=dict(type='str', required=True), group_type=dict(type='str', default='area', choices=['area','building']), group_parent_name=dict(type='str', default='-1'), group_building_address=dict(type='str', default='123') ) result = dict( changed=False, original_message='', message='') module = AnsibleModule( argument_spec = module_args, supports_check_mode = False ) # build the required payload data structure payload = { "childIds":[""], "groupTypeList": ["SITE"], "name": module.params["group_name"], "additionalInfo":[ {"attributes":{ "type":module.params["group_type"] }, "nameSpace":"Location"} ] } # Instantiate the DnaCenter class object dnac = DnaCenter(module) dnac.api_path = 'api/v1/group' # Get the groups groups = dnac.get_obj() try: _group_names = [group['name'] for group in groups['response']] except TypeError: module.fail_json(msg=groups) # does group provided exist if module.params['group_name'] in _group_names: _group_exists = True else: _group_exists = False # does parent provided exist if module.params['group_parent_name'] in _group_names: _parent_exists = True else: _parent_exists = False module.fail_json(msg='Parent Group does not exist...') # find the parent Id specificed by the name #_parent_id = [ group['id'] for group in groups['response'] if group['name'] == module.params['group_parent_name']] if module.params['group_parent_name'] == 'Global' or module.params['group_parent_name'] == '-1': payload.update({'parentId' : ''}) elif _parent_exists: _parent_id = dnac.get_group_id(module.params['group_parent_name']) payload.update({'parentId': _parent_id}) else: result['changed'] = False module.fail_json(msg="Parent doesn't exist!", **result) # lookup lat/long based on provided address if module.params['group_type'] == 'building': attribs = dnac.parse_geo(module.params['group_building_address']) payload['additionalInfo'][0]['attributes'].update(attribs) if module.params['state'] == 'present' and _group_exists: result['changed'] = False result['intended_payload'] = payload module.exit_json(msg='Group already exists.', **result) elif module.params['state'] == 'present' and not _group_exists: dnac.create_obj(payload) elif module.params['state'] == 'absent' and _group_exists: _group_id = [group['id'] for group in groups['response'] if group['name'] == module.params['group_name']] dnac.delete_obj(_group_id[0]) elif module.params['state'] == 'absent' and not _group_exists: result['changed'] = False module.exit_json(msg='Group Does not exist. Cannot delete.', **result) '''
def main(): _profile_exists = False module_args = dnac_argument_spec module_args.update(state=dict(type='str', choices=['absent', 'present']), name=dict(type='str', required=True), sites=dict(type='list', required=False), ssid_name=dict(type='str', required=False), ssid_type=dict(type='str', required=False, default='Enterprise', choices=['Guest', 'Enterprise']), fabric_enabled=dict(type='bool', required=False, default=False), flexconnect=dict(type='bool', required=False, default=False), flexconnect_vlan=dict(type='str', required=False), interface=dict(type='str', required=False)) result = dict(changed=False, original_message='', message='', orig_config='', proposed_config='') module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) # build the required payload data structure payload = { "profileDetails": { "name": module.params['name'], "sites": module.params['sites'], } } # If ssid information is provided add to the payload if module.params['ssid_name'] and module.params['ssid_type']: payload['profileDetails'].update({ "ssidDetails": [{ "name": module.params['ssid_name'], "type": module.params['ssid_type'], "enableFabric": module.params['fabric_enabled'], }] }) if module.params['interface']: payload.update({"interfaceName": module.params['interface']}) # If Flexconnect is in play, add flexconnect variables if module.params['flexconnect']: flexconnect = { "flexConnect": { "enableFlexConnect": module.params['flexconnect'], "localToVlan": module.params['flexconnect_vlan'] } } else: flexconnect = { "flexConnect": { "enableFlexConnect": module.params['flexconnect'] } } payload['profileDetails']['ssidDetails'][0].update(flexconnect) # Instantiate the DnaCenter class object dnac = DnaCenter(module) dnac.api_path = 'dna/intent/api/v1/wireless/profile' dnac.result = result # check if the configuration is already in the desired state # get the SSIDs profiles = dnac.get_obj() result['orig_config'] = [ profile for profile in profiles if profile['profileDetails']['name'] == module.params['name'] ] result['proposed_config'] = payload if len(profiles) > 0: _profile_names = [ profile['profileDetails']['name'] for profile in profiles ] else: _profile_names = [] # does pool provided exist if module.params['name'] in _profile_names: _profile_exists = True else: _profile_exists = False # actions if module.params['state'] == 'present' and _profile_exists: orig_config = dnac.result['orig_config'] proposed_config = dnac.result['proposed_config'] if len(orig_config) > 0: del (orig_config[0]['profileDetails']['instanceUuid']) if orig_config == proposed_config: result['changed'] = False module.exit_json(msg='Wireless Profile already exists.', **result) else: dnac.update_obj(proposed_config) dnac.result['changed'] = True module.exit_json(msg='Updated Wireless Profile.', **dnac.result) else: dnac.result['changed'] = True dnac.update_obj(proposed_config) # result['changed'] = False # module.exit_json(msg='Wireless Profile already exists.', **result) elif module.params['state'] == 'present' and not _profile_exists: dnac.create_obj(payload) elif module.params['state'] == 'absent' and _profile_exists: # Create payload of existing profile payload = [ profile for profile in profiles if profile['profileDetails']['name'] == module.params['name'] ] # Remove Site Assignment payload[0]['profileDetails']['sites'] = [] dnac.update_obj(payload[0]) # Delete the Wireless Profile dnac.delete_obj(module.params['name']) elif module.params['state'] == 'absent' and not _profile_exists: result['changed'] = False module.exit_json( msg='Wireless Profile Does not exist. Cannot delete.', **result)
def main(): _user_exists = False module_args = dnac_argument_spec module_args.update( cli_user=dict(type='str', required=True), cli_password=dict(type='str', required=True, no_log=True), cli_enable_password=dict(type='str', required=True, no_log=True), cli_desc=dict(type='str', required=True), cli_comments=dict(type='str', required=False), group_name=dict(type='str',default='-1') ) result = dict( changed=False, original_message='', message='') module = AnsibleModule( argument_spec = module_args, supports_check_mode = False ) # Build the payload dictionary payload = [ {"username": module.params['cli_user'], "password": module.params['cli_password'], "enablePassword": module.params['cli_enable_password'], "description": module.params['cli_desc'], "comments": module.params['cli_comments'] } ] # instansiate the dnac class dnac = DnaCenter(module) dnac.api_path = 'api/v1/global-credential?credentialSubType=CLI' # check if the configuration is already in the desired state settings = dnac.get_obj() _usernames = [ user['username'] for user in settings['response']] if module.params['cli_user'] in _usernames: _user_exists = True else: _user_exists = False ''' check if username exists check state flag: present = create, absent = delete, update = change url_password if state = present and user doesn't exist, create user if state = absent and user exists, delete user if state = update and user exists, use put to update user ''' for setting in settings['response']: if setting['username'] == payload[0]['username']: if module.params['state'] == 'absent': dnac.api_path='api/v1/global-credential' #dnac.api_path='api/v1/global-credential' response = dnac.delete_obj(setting['id']) result['changed'] = True result['msg'] = 'User Deleted.' module.exit_json(**result) elif module.params['state'] == 'update': # call update function payload = payload[0].update({'id': setting['id']}) dnac.api_path = 'api/v1/global-credential/cli' #dnac.api_path = 'dna/intent/api/v1/global-credential/cli' dnac.update_obj(payload) if not _user_exists and module.params['state'] == 'present': # call create function dnac.api_path = 'api/v1/global-credential/cli' #dnac.api_path = 'dna/intent/api/v1/global-credential/cli' dnac.create_obj(payload) elif not _user_exists and module.params['state'] == 'update': module.fail_json(msg="User doesn't exist. Cannot delete or update.", **result) elif not _user_exists and module.params['state'] == 'absent': module.fail_json(msg="User doesn't exist. Cannot delete or update.", **result) elif _user_exists and module.params['state'] == 'present': result['changed'] = False result['msg'] = 'User exists. Use state: update to change user' module.exit_json(**result)
def main(): _site_exists = False _parent_exists = False module_args = dnac_argument_spec module_args.update( state=dict(type='str', default='present', choices=['absent', 'present', 'update']), name=dict(type='str', required=True), site_type=dict(type='str', default='area', choices=['area', 'building', 'floor']), parent_name=dict(type='str', default='Global'), address=dict(type='str'), latitude=dict(type='str', required=False), longitude=dict(type='str', required=False), rf_model=dict(type='str', choices=[ 'Cubes And Walled Offices', 'Drywall Office Only', 'Indoor High Ceiling', 'Outdoor Open Space]' ]), width=dict(type='str', required=False), length=dict(type='str', required=False), height=dict(type='str', required=False), ) result = dict(changed=False, original_message='', message='') module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) # Instantiate the DnaCenter class object dnac = DnaCenter(module) dnac.api_path = 'api/v1/group' # Get the sites sites = dnac.get_obj() try: _site_names = [site['name'] for site in sites['response']] except TypeError: module.fail_json(msg=sites) # does site provided exist if module.params['name'] in _site_names: _site_exists = True else: _site_exists = False # does parent provided exist if module.params['parent_name'] in _site_names or module.params[ 'parent_name'] == 'Global': _parent_exists = True else: _parent_exists = False module.fail_json(msg='Parent Site does not exist...') # Obtain Parent groupNameHierarchy if module.params['parent_name'] == "Global": parent_hierarchy = "Global" else: parent_hierarchy = [ site['groupNameHierarchy'] for site in sites['response'] if site['name'] == module.params['parent_name'] ][0] # build the required payload data structure if module.params['site_type'] == 'area': payload = { "type": "area", "site": { "area": { "name": module.params['name'], "parentName": parent_hierarchy } } } elif module.params['site_type'] == 'building': payload = { "type": "building", "site": { "building": { "name": module.params['name'], "address": module.params['address'], "parentName": parent_hierarchy, "latitude": module.params['latitude'], "longitude": module.params['longitude'] } } } elif module.params['site_type'] == 'floor': payload = { "type": "floor", "site": { "floor": { "name": module.params['name'], "parentName": parent_hierarchy, "rfModel": module.params['rf_model'], "width": module.params['width'], "length": module.params['length'], "height": module.params['height'] } } } # Do the stuff dnac.api_path = 'dna/intent/api/v1/site' if module.params['state'] == 'present' and _site_exists: result['changed'] = False result['intended_payload'] = payload module.exit_json(msg='Site already exists.', **result) elif module.params['state'] == 'present' and not _site_exists: dnac.create_obj(payload) elif module.params['state'] == 'absent' and _site_exists: _site_id = [ site['id'] for site in sites['response'] if site['name'] == module.params['name'] ] dnac.delete_obj(_site_id[0]) elif module.params['state'] == 'absent' and not _site_exists: result['changed'] = False module.exit_json(msg='Site Does not exist. Cannot delete.', **result)
def main(): _ip_pool_exists = False module_args = dnac_argument_spec module_args.update( # removed api_path local variable state=dict(type='str', default='present', choices=['absent', 'present', 'update']), ip_pool_name=dict(type='str', required=True), ip_pool_subnet=dict(type='str', required=True), ip_pool_prefix_len=dict(type='str', default='/8'), ip_pool_gateway=dict(type='str', required=True), ip_pool_dhcp_servers=dict(type='list'), ip_pool_dns_servers=dict(type='list'), ip_pool_overlapping=dict(type='bool', default=False)) result = dict(changed=False, original_message='', message='') module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) # build the required payload data structure payload = { "ipPoolName": module.params['ip_pool_name'], "ipPoolCidr": module.params['ip_pool_subnet'] + module.params['ip_pool_prefix_len'], "gateways": module.params['ip_pool_gateway'].split(','), "dhcpServerIps": module.params['ip_pool_dhcp_servers'], "dnsServerIps": module.params['ip_pool_dns_servers'], "overlapping": module.params['ip_pool_overlapping'] } # Instantiate the DnaCenter class object dnac = DnaCenter(module) dnac.api_path = 'api/v2/ippool' # check if the configuration is already in the desired state # Get the ip pools ip_pools = dnac.get_obj() _ip_pool_names = [pool['ipPoolName'] for pool in ip_pools['response']] # does pool provided exist if module.params['ip_pool_name'] in _ip_pool_names: _ip_pool_exists = True else: _ip_pool_exists = False # actions if module.params['state'] == 'present' and _ip_pool_exists: result['changed'] = False module.exit_json(msg='IP Pool already exists.', **result) elif module.params['state'] == 'present' and not _ip_pool_exists: dnac.create_obj(payload) elif module.params['state'] == 'absent' and _ip_pool_exists: _ip_pool_id = [ pool['id'] for pool in ip_pools['response'] if pool['ipPoolName'] == module.params['ip_pool_name'] ] dnac.delete_obj(_ip_pool_id[0]) elif module.params['state'] == 'absent' and not _ip_pool_exists: result['changed'] = False module.exit_json(msg='Ip pool Does not exist. Cannot delete.', **result)
def main(): _credential_exists = False module_args = dnac_argument_spec module_args.update( credential_name=dict(type='str', required=True), credential_type=dict(type='str', default='SNMPV2_WRITE_COMMUNITY', \ choices=['SNMPV2_READ_COMMUNITY', 'SNMPV2_WRITE_COMMUNITY', 'CLI']), group_name=dict(type='str', default= 'Global', required=False) ) result = dict(changed=False, original_message='', message='') module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) # instantiate dnac object dnac = DnaCenter(module) # lookup credential id dnac.api_path = 'api/v1/global-credential?credentialSubType=' + module.params[ 'credential_type'] settings = dnac.get_obj() if module.params['credential_type'] == 'CLI': _cred_id = [ user['id'] for user in settings['response'] if user['username'] == module.params['credential_name'] ] elif module.params['credential_type'] == 'SNMPV2_READ_COMMUNITY' or \ module.params['credential_type'] == 'SNMPV2_WRITE_COMMUNITY': _cred_id = [ cred['id'] for cred in settings['response'] if cred['description'] == module.params['credential_name'] ] # set key string if module.params['credential_type'] == 'CLI': _credential_key = 'credential.cli' _credential_val_type = 'credential_cli' elif module.params['credential_type'] == 'SNMPV2_READ_COMMUNITY': _credential_key = 'credential.snmp_v2_read' _credential_val_type = 'credential_snmp_v2_read' elif module.params['credential_type'] == 'SNMPV2_WRITE_COMMUNITY': _credential_key = 'credential.snmp_v2_write' _credential_val_type = 'credential_snmp_v2_write' # lookup group id dnac.api_path = 'api/v1/group' groups = dnac.get_obj()['response'] _group_id = [ g['id'] for g in groups if g['name'] == module.params['group_name'] ] if len(_group_id) == 1: _group_id = _group_id[0] if len(_cred_id) == 1: _cred_id = _cred_id[0] # build the payload dictionary payload = [{ "instanceUuid": "", "inheritedGroupName": "", "version": "1", "namespace": "global", "groupUuid": _group_id, "key": _credential_key, "instanceType": "reference", "type": "reference.setting", "value": [{ "type": _credential_val_type, "objReferences": [_cred_id], "url": "" }] }] dnac.api_path = 'api/v1/commonsetting/global/' + _group_id dnac.create_obj(payload)
def main(): _device_exists = False payload = '' module_args = dnac_argument_spec module_args.update( device_name=dict(type='str', required=False), device_mgmt_ip=dict(type='str',required=False), group_name=dict(type='str',required=False), group_name_hierarchy=dict(type='str',required=False) ) result = dict( changed=False, original_message='', message='') module = AnsibleModule( argument_spec = module_args, supports_check_mode = False ) # Instantiate the DnaCenter class object dnac = DnaCenter(module) # Get device details based on either the Management IP or the Name Provided if module.params['device_mgmt_ip'] is not None: dnac.api_path = 'api/v1/network-device?managementIpAddress=' + module.params['device_mgmt_ip'] elif module.params['device_name'] is not None: dnac.api_path = 'api/v1/network-device?hostname=' + module.params['device_name'] device_results = dnac.get_obj() try: device_id = device_results['response'][0]['id'] except IndexError: module.fail_json(msg='Unable to find device with supplied information.') # get the group id if module.params['group_name'] is not None: dnac.api_path = 'api/v1/group?groupName=' + module.params['group_name'] elif module.params['group_name_hierarchy'] is not None: dnac.api_path = 'api/v1/group?groupNameHierarchy=' + module.params['group_name_hierarchy'] # dnac.api_path = 'api/v1/group?groupName=' + module.params['group_name'] group_results = dnac.get_obj() try: group_id = group_results['response'][0]['id'] except IndexError: module.fail_json(msg='Unable to find group with the supplied information.') # check if the device is already a member of that group # 1.2 ??? dnac.api_path = 'api/v1/member/group?groupType=SITE&id=' + device_id dnac.api_path = 'api/v1/member/group?groupType=SITE&id=' + device_id group_assignment = dnac.get_obj() payload = {'networkdevice': [device_id]} dnac.api_path = 'api/v1/group/' + group_id + '/member' if module.params['state'] == 'present': if len(group_assignment['response'][device_id]) > 0: if group_assignment['response'][device_id][0]['id'] == group_id: result['changed'] = False module.exit_json(msg='Device assigned to the correct group.', **result) else: result['changed'] = False module.fail_json(msg='Device is already assigned to another group. Use update as the state.', **result) else: dnac.create_obj(payload) elif module.params['state'] == 'absent': if not group_assignment['response'][device_id][0]: module.fail_json(msg='Device is not assigned to a group. Cannot remove assignment') elif group_assignment['response'][device_id][0]['id'] != group_id: module.fail_json(msg='Device is not assigned to the group provided. Device is currently in group:' + group_assignment['response'][device_id][0]['groupNameHierarchy']) else: dnac.delete_obj(device_id) elif module.params['state'] == 'update': if not len(group_assignment['response'][device_id]) > 0: #if not group_assignment['response'][device_id][0]: dnac.create_obj(payload) elif group_assignment['response'][device_id][0]['id'] == group_id: result['changed'] = False module.exit_json(msg='Device already assigned to the target group. No changes required.') elif group_assignment['response'][device_id][0]['id'] != group_id: _current_group_id = group_assignment['response'][device_id][0]['id'] dnac.api_path = 'api/v1/group/' + _current_group_id + '/member' dnac.delete_obj(device_id) # change the API to the new group dnac.api_path = 'api/v1/group/' + group_id + '/member' dnac.create_obj(payload)
def main(): _ssid_exists = False module_args = dnac_argument_spec module_args.update( state=dict(type='str', choices=['absent','present']), name=dict(type='str', required=True), security_level=dict(type='str', required=True, choices=['WPA2_ENTERPRISE', 'WPA2_PERSONAL', 'OPEN']), passphrase=dict(type='str', required=False, no_log=True), enable_fastlane=dict(type='bool', required=False, default=False), enable_mac_filtering=dict(type='bool', required=False, default=False), traffic_type=dict(type='str', required=False, choices=['voicedata','data'], default='voicedata'), radio_policy=dict(type='str', required=False, choices=['Dual band operation (2.4GHz and 5GHz)', 'Dual band operation with band select', '5GHz only', '2.4GHz only)'], default='Dual band operation (2.4GHz and 5GHz)'), enable_broadcast_ssid=dict(type=bool, required=False, default=True), fast_transition=dict(type='str', required=False, choices=[ 'Adaptive', 'Enable', 'Disable'], default='Disable') ) result = dict( changed=False, original_message='', message='') module = AnsibleModule( argument_spec = module_args, supports_check_mode = False ) # build the required payload data structure payload = { "name": module.params['name'], "securityLevel": module.params['security_level'], "passphrase": module.params['passphrase'], "enableFastLane": module.params['enable_fastlane'], "enableMACFiltering": module.params['enable_mac_filtering'], "trafficType": module.params['traffic_type'], "radioPolicy": module.params['radio_policy'], "enableBroadcastSSID": module.params['enable_broadcast_ssid'], "fastTransition": module.params['fast_transition'] } # Instantiate the DnaCenter class object dnac = DnaCenter(module) dnac.api_path= 'dna/intent/api/v1/enterprise-ssid' # check if the configuration is already in the desired state # get the SSIDs ssids = dnac.get_obj() _ssid_names = [ssid['ssidDetails'][0]['name'] for ssid in ssids] # does pool provided exist if module.params['name'] in _ssid_names: _ssid_exists = True else: _ssid_exists = False dnac.api_path= 'dna/intent/api/v1/enterprise-ssid' # actions if module.params['state'] == 'present' and _ssid_exists: result['changed'] = False module.exit_json(msg='SSID already exists.', **result) elif module.params['state'] == 'present' and not _ssid_exists: dnac.create_obj(payload) elif module.params['state'] == 'absent' and _ssid_exists: # _ssid_id = [ssid['instanceUuid'] for ssid in ssids if ssid['ssidDetails'][0]['name'] == module.params['name']] # dnac.delete_obj(_ssid_id[0]) dnac.delete_obj(module.params['name']) elif module.params['state'] == 'absent' and not _ssid_exists: result['changed'] = False module.exit_json(msg='SSID Does not exist. Cannot delete.', **result)
def main(): _credential_exists = False module_args = dnac_argument_spec module_args.update( credential_type=dict(type='str', default='SNMPV2_WRITE_COMMUNITY',choices=['SNMPV2_READ_COMMUNITY','SNMPV2_WRITE_COMMUNITY']), snmp_community=dict(type='str', required=True), snmp_description=dict(type='str', required=True), snmp_comments=dict(type='str', required=True) ) result = dict( changed=False, original_message='', message='') module = AnsibleModule( argument_spec = module_args, supports_check_mode = False ) if module.params['credential_type'] == 'SNMPV2_WRITE_COMMUNITY': _community_key_name = 'writeCommunity' _url_suffix = 'snmpv2-write-community' elif module.params['credential_type'] == 'SNMPV2_READ_COMMUNITY': _community_key_name = 'readCommunity' _url_suffix = 'snmpv2-read-community' # Build the payload dictionary payload = [ {_community_key_name: module.params['snmp_community'], "description": module.params['snmp_description'], "comments": module.params['snmp_comments'] } ] # instansiate the dnac class dnac = DnaCenter(module) dnac.api_path = 'api/v1/global-credential?credentialSubType=' + module.params['credential_type'] # # check if the configuration is already in the desired state settings = dnac.get_obj() # _creds = [ cred['description'] for cred in settings['response']] _creds = [(cred['description'], cred['id']) for cred in settings['response'] if cred['description'] == module.params['snmp_description']] if len(_creds) > 1: module.fail_json(msg="Multiple matching entries...invalid.", **result) elif len(_creds) == 0: _credential_exists = False else: _credential_exists = True ''' check if cred exists check state flag: present = create, absent = delete, update = change url_password if state = present and cred doesn't exist, create user if state = absent and cred exists, delete user if state = update and cred exists, use put to update user ''' if _credential_exists: if module.params['state'] == 'present': # in desired state result['changed'] = False result['msg'] = 'Credential exists. Use state: update to change credential' module.exit_json(**result) elif module.params['state'] == 'absent': dnac.api_path = 'api/v1/global-credential/' dnac.delete_obj(_creds[0][1]) elif not _credential_exists: if module.params['state'] == 'present': dnac.api_path = 'api/v1/global-credential/' + _url_suffix dnac.create_obj(payload) elif module.params['state'] == 'absent': module.fail_json(msg="Credential doesn't exist. Cannot delete or update.", **result)
def main(): module_args = dnac_argument_spec module_args.update(state=dict(type='str', choices=['present']), name=dict(type='str', required=True), site=dict(type='str', required=False), managed_ap_locations=dict(type='list', required=False), interface_ip=dict(type='str', required=False, default='1.1.1.1'), interface_prefix_length=dict(type='str', required=False, default='24'), interface_gateway=dict(type='str', required=False, default='1.1.1.2'), lag_or_port_number=dict(type='str', required=False), vlan=dict(type='str', required=False), interface=dict(type='str', required=False), reprovision=dict(type='bool', required=False, default=False)) result = dict(changed=False, original_message='', message='') module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) # build the required payload data structure payload = [{ "deviceName": module.params['name'], "site": module.params['site'], "managedAPLocations": module.params['managed_ap_locations'] }] if module.params['interface']: payload[0].update({ "dynamicInterfaces": [{ "interfaceIPAddress": module.params['interface_ip'], "interfaceNetmaskInCIDR": module.params['interface_prefix_length'], "interfaceGateway": module.params['interface_gateway'], "lagOrPortNumber": module.params['lag_or_port_number'], "vlanId": module.params['vlan'], "interfaceName": module.params['interface'], }] }) # Instantiate the DnaCenter class object dnac = DnaCenter(module) # Check if Device Has been Provisioned # dnac.api_path = 'dna/intent/api/v1/network-device?hostname=' + module.params['name'] # device = dnac.get_obj() # if device['response']: # if device['response'][0]['location'] == None: # _PROVISIONED = False # else: # _PROVISIONED = True # else: # _PROVISIONED = False if module.params['reprovision']: _PROVISIONED = True else: _PROVISIONED = False # Reset API Path dnac.api_path = 'dna/intent/api/v1/wireless/provision' # actions if module.params['state'] == 'present' and _PROVISIONED: # module.exit_json(msg=payload) dnac.update_obj(payload) elif module.params['state'] == 'present' and not _PROVISIONED: # module.exit_json(msg='provision') dnac.create_obj(payload)
def main(): _discovery_exists = False payload = '' module_args = dnac_argument_spec module_args.update(discovery_name=dict(alias='name', type='str', required=True), discovery_type=dict(type='str', required=True, choices=['CDP', 'Range']), discovery_cdp_level=dict(alias='cdp_level', type='str', required=False), discovery_preferred_ip_method=dict( alias='preferred_ip_method', type='str', required=False, default='None', choices=['None', 'UseLoopBack']), discovery_ip_filter_list=dict(type='str', required=False), discovery_ip_addr_list=dict(type='str', required=True), global_cli_cred=dict(type='str', required=True), global_snmp_cred=dict(type='str', required=True), netconf_port=dict(type='str', required=False), rediscovery=dict(type='bool', required=False, default=False)) result = dict(changed=False, original_message='', message='') module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) # Instantiate the DnaCenter class object dnac = DnaCenter(module) # build the required payload data structure # lookup global credentials. dnac.api_path = 'api/v1/global-credential?credentialSubType=CLI' cli_cred = dnac.get_obj() for cli in cli_cred['response']: if cli['username'] == module.params['global_cli_cred']: cli_id = cli['id'] dnac.api_path = 'api/v1/global-credential?credentialSubType=SNMPV2_WRITE_COMMUNITY' snmp_cred = dnac.get_obj() for snmp in snmp_cred['response']: if snmp['description'] == module.params['global_snmp_cred']: snmp_id = snmp['id'] payload = { "preferredMgmtIPMethod": module.params['discovery_preferred_ip_method'], "name": module.params['discovery_name'], "cdpLevel": module.params['discovery_cdp_level'], "globalCredentialIdList": [cli_id, snmp_id], "ipFilterList": module.params['discovery_ip_filter_list'], "ipAddressList": module.params['discovery_ip_addr_list'], "discoveryType": module.params['discovery_type'], "protocolOrder": "ssh", "retry": 3, "timeout": 5, "lldpLevel": "16", "netconfPort": module.params['netconf_port'], "rediscovery": module.params['rediscovery'] } ''' { "preferredMgmtIPMethod": module.params['discovery_preferred_ip_method'], "name": module.params['discovery_name'], "snmpROCommunityDesc": "", "snmpRWCommunityDesc": "", "parentDiscoveryId": "", "globalCredentialIdList": [ "" ], "httpReadCredential": { "port": 0, "password": "", "username": "", "secure": false, "description": "", "credentialType": "", "comments": "", "instanceUuid": "", "id": "" }, "httpWriteCredential": { "port": 0, "password": "", "username": "", "secure": false, "description": "", "credentialType": "", "comments": "", "instanceUuid": "", "id": "" }, "snmpUserName": "", "snmpMode": "", "netconfPort": "", "cdpLevel": 0, "enablePasswordList": [ "" ], "ipFilterList": [ "" ], "passwordList": [ "" ], "protocolOrder": "", "reDiscovery": false, "retry": 0, "snmpAuthPassphrase": "", "snmpAuthProtocol": "", "snmpPrivPassphrase": "", "snmpPrivProtocol": "", "snmpROCommunity": "", "snmpRWCommunity": "", "userNameList": [ "" ], "ipAddressList": "", "snmpVersion": "", "timeout": 0, "discoveryType": "" } ''' # Get the discoveries dnac.api_path = 'api/v1/discovery' #'api/v1/discovery' discoveries = dnac.get_obj() _discovery_names = [ discovery['name'] for discovery in discoveries['response'] ] # does discovery provided exist if module.params['discovery_name'] in _discovery_names: _discovery_exists = True _discovery_id = [ d['id'] for d in discoveries['response'] if d['name'] == module.params['discovery_name'] ][0] else: _discovery_exists = False # actions if module.params[ 'state'] == 'present' and _discovery_exists and module.params[ 'rediscovery']: result['changed'] = True dnac.api_path = 'api/v1/discovery' payload.update({'id': _discovery_id}) dnac.update_obj(payload) module.exit_json(msg='Discovery already exists.', **result) elif module.params['state'] == 'present' and _discovery_exists: result['changed'] = False module.exit_json(msg='Discovery already exists.', **result) elif module.params['state'] == 'present' and not _discovery_exists: dnac.create_obj(payload) elif module.params['state'] == 'absent' and _discovery_exists: _discovery_id = [ discovery['id'] for discovery in discoveries['response'] if discovery['name'] == module.params['discovery_name'] ] dnac.delete_obj(_discovery_id[0]) elif module.params['state'] == 'absent' and not _discovery_exists: result['changed'] = False module.exit_json(msg='Discovery Does not exist. Cannot delete.', **result)