def main(): 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=True) # 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']: if dnac.module.check_mode: dnac.result['changed'] = True dnac.module.exit_json( msg='In check_mode. Changes would be required.', **dnac.result) else: 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(): 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)) 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' 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)
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.delete_obj(setting['id']) 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(): _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)