def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(id = dict(type='str', required=True), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) response = viptela.request('/dataservice/device/action/status/{0}'.format(viptela.params['id'])) if response.json: viptela.result['json'] = response.json viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(factory_default=dict(type='bool', default=False), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, original_message='', message='') # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) viptela.result['feature_templates'] = viptela.get_feature_template_list( factory_default=viptela.params['factory_default']) viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update( name=dict(type='str', required=True), device_ip=dict(type='str', alias='deviceIP'), uuid=dict(type='str'), model=dict(type='str'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) viptela.result['what_changed'] = [] device = {} # See if we can find the device by deviceIP if viptela.params['device_ip']: device = viptela.get_device_by_device_ip(viptela.params['device_ip']) # If we could not find the device by deviceIP, see if we can find it be (host)name if not device: device = viptela.get_device_by_name(viptela.params['name']) if not device: # if device['manageConnectionState'] != 'connected' # if UUID is specified, try to get by uuid if viptela.params['uuid']: device = viptela.get_device_by_uuid(viptela.params['uuid']) if not device: viptela.fail_json(msg="Could not find device with UUID: {0}". format(viptela.params['uuid'])) elif viptela.params['model']: # if no uuid was specified, just grab the first free device device = viptela.get_unused_device(viptela.params['model']) if not device: viptela.fail_json(msg="Could not find available device") viptela.result['what_changed'].append('bootstrap') if not module.check_mode: bootstrap = viptela.generate_bootstrap(device['uuid']) viptela.result['bootstrap'] = bootstrap if viptela.result['what_changed']: viptela.result['changed'] = True viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(factory_default=dict(type='bool', default=False), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, original_message='', message='' ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications if module.check_mode: return result viptela.result['feature_templates'] = viptela.get_feature_templates(factory_default=viptela.params['factory_default']) viptela.result['device_templates'] = viptela.get_device_templates() # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications # FIXME: Work with viptela so they can implement a check mode if module.check_mode: viptela.exit_json(**viptela.result) # execute checks for argument completeness # manipulate or modify the state as needed (this is going to be the # part where your module will do what it needs to do) # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # vSmart policies # response = viptela.request('/dataservice/template/policy/vsmart') # response_json = response.json() # vsmart_policies = response_json['data'] policy_lists = {} # for list_type in viptela.POLICY_LIST_TYPES: # list = viptela.get_policy_list_list(list_type) # policy_lists[list_type] = list policy_lists = viptela.get_policy_list_list('all') # # Prefix lists # prefix_lists = viptela.get_policy_list_list('prefix') # # # VPN lists # vpn_lists = viptela.get_policy_list_list('vpn') # # policy_lists = { # # 'vsmart_policies': vsmart_policies, # 'vmanage_site_lists': site_lists, # 'vmanage_prefix_lists': prefix_lists, # 'vmanage_vpn_lists': vpn_lists, # } viptela.result['policy_lists'] = policy_lists viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # vSmart policies # response = viptela.request('/dataservice/template/policy/vsmart') # response_json = response.json() # vsmart_policies = response_json['data'] policy_definitions = {} policy_list_dict = viptela.get_policy_list_dict('all', key_name='listId') for list_type in viptela.POLICY_DEFINITION_TYPES: definition_list = viptela.get_policy_definition_list(list_type) definition_detail_list = [] for definition in definition_list: definition_detail = viptela.get_policy_definition( list_type, definition['definitionId']) for sequence in definition_detail['sequences']: for entry in sequence['match']['entries']: entry['listName'] = policy_list_dict[entry['ref']]['name'] entry['listType'] = policy_list_dict[entry['ref']]['type'] definition_detail_list.append(definition_detail) policy_definitions[list_type] = definition_detail_list viptela.result['policy_definitions'] = policy_definitions viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(file=dict(type='str', required=True), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, original_message='', message='') # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) if not module.check_mode: response = viptela.request( '/dataservice/system/device/fileupload', method='POST', files={'file': open(module.params['file'], 'rb')}, data={ 'validity': 'valid', 'upload': 'true' }, headers=None) viptela.result['msg'] = response.json['vedgeListUploadStatus'] if 'successfully' in response.json['vedgeListUploadStatus']: viptela.result['changed'] = True viptela.exit_json()
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(file = dict(type='str', required=True), factory_default=dict(type='str', default=False), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) feature_template_export = viptela.get_feature_template_list(factory_default=viptela.params['factory_default']) device_template_export = viptela.get_device_template_list(factory_default=viptela.params['factory_default']) template_export = { 'feature_templates': feature_template_export, 'device_templates': device_template_export } if not module.check_mode: with open(viptela.params['file'], 'w') as f: json.dump(template_export, f, indent=4, sort_keys=True) viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(organization=dict(type='str'), vbond=dict(type='str'), vbond_port=dict(type='int', default=12346), root_cert=dict(type='str'), push=dict(type='bool') ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) viptela.result['what_changed'] = [] if viptela.params['push']: viptela.push_certificates() if viptela.result['what_changed']: viptela.result['changed'] = True viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(state=dict(type='str', choices=['absent', 'present'], default='present'), name=dict(type='str', required=True), device_ip=dict(type='str', alias='deviceIP'), personality=dict(type='str', choices=['vmanage', 'vsmart', 'vbond', 'vedge'], required=True), device_username=dict(type='str', alias='device_user'), device_password=dict(type='str') ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) viptela.result['what_changed'] = [] if viptela.params['personality'] == 'vedge': device_type = 'vedges' else: device_type = 'controllers' device = {} # See if we can find the device by deviceIP if viptela.params['device_ip']: device = viptela.get_device_by_device_ip(viptela.params['device_ip'], type=device_type) # If we could not find the device by deviceIP, see if we can find it be (host)name if not device: device = viptela.get_device_by_name(viptela.params['name'], type=device_type) if viptela.params['state'] == 'present': if device: # Can't really change anything pass else: viptela.result['what_changed'].append('new') if not viptela.params['device_username'] or not viptela.params['device_password']: viptela.fail_json(msg="device_username and device_password must be specified when add a new device") if not module.check_mode: response = viptela.create_controller(viptela.params['device_ip'], viptela.params['personality'], viptela.params['device_username'], viptela.params['device_password']) viptela.result['response'] = response else: if device: viptela.result['what_changed'].append('delete') if not module.check_mode: viptela.delete_controller(device['uuid']) if viptela.result['what_changed']: viptela.result['changed'] = True viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update( state=dict(type='str', choices=['absent', 'present'], default='present'), name=dict(type='str', alias='templateName'), description=dict(type='str', alias='templateDescription'), definition=dict(type='str', alias='templateDefinition'), type=dict(type='str', alias='templateType'), device_type=dict(type='list', alias='deviceType'), template_min_version=dict(type='str', alias='templateMinVersion'), factory_default=dict(type='bool', alias='factoryDefault'), aggregate=dict(type='dict'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications if module.check_mode: return result feature_templates = viptela.get_feature_templates(factory_default=True) device_templates = viptela.get_device_templates() if viptela.params['aggregate']: if viptela.params['state'] == 'present': for name, data in viptela.params['aggregate'].items(): if name not in device_templates: payload = {} payload['templateName'] = name payload['templateDescription'] = data[ 'templateDescription'] payload['deviceType'] = data['deviceType'] payload['factoryDefault'] = data['factoryDefault'] payload['configType'] = data['configType'] if data['configType'] == 'file': payload['templateConfiguration'] = data[ 'templateConfiguration'] viptela.request('/dataservice/template/device/cli', method='POST', data=json.dumps(payload)) else: generalTemplates = [] for template in data['generalTemplates']: if 'subTemplates' in template: subTemplates = [] for sub_template in template['subTemplates']: subTemplates.append({ 'templateId': feature_templates[sub_template[ 'templateName']]['templateId'], 'templateType': sub_template['templateType'] }) template_item = { 'templateId': feature_templates[template['templateName']] ['templateId'], 'templateType': template['templateType'], 'subTemplates': subTemplates } else: template_item = { 'templateId': feature_templates[template['templateName']] ['templateId'], 'templateType': template['templateType'] } generalTemplates.append(template_item) payload['generalTemplates'] = generalTemplates # payload['policyId'] = '' if 'connectionPreference' in data: payload['connectionPreference'] = data[ 'connectionPreference'] if 'connectionPreferenceRequired' in data: payload['connectionPreferenceRequired'] = data[ 'connectionPreferenceRequired'] payload['featureTemplateUidRange'] = [] # payload['templateType'] = data['templateType'] # payload['templateMinVersion'] = data['templateMinVersion'] viptela.result['y_debug'] = data viptela.result['z_debug'] = payload viptela.request('/dataservice/template/device/feature', method='POST', data=json.dumps(payload)) viptela.result['changed'] = True # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications # FIXME: Work with viptela so they can implement a check mode if module.check_mode: viptela.exit_json(**viptela.result) # execute checks for argument completeness # manipulate or modify the state as needed (this is going to be the # part where your module will do what it needs to do) # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results viptela.exit_json(**viptela.result)
def run_module(): action_id = None action_status = None action_activity = None action_config = None # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update( state=dict(type='str', choices=['absent', 'present', 'query'], default='present'), device_name=dict(type='str', aliases=['device', 'host-name']), device_ip=dict(type='str', aliases=['system_ip']), site_id=dict(type='str'), uuid=dict(type='str'), personality=dict(type='str', choices=['vmanage', 'vsmart', 'vbond', 'vedge'], default='vedge'), template=dict(type='str'), variables=dict(type='dict', default={}), wait=dict(type='bool', default=False), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) viptela.result['what_changed'] = [] if viptela.params['personality'] == 'vedge': device_type = 'vedges' else: device_type = 'controllers' if viptela.params['uuid']: device_data = viptela.get_device_by_uuid(viptela.params['uuid'], type=device_type) if 'uuid' not in device_data: viptela.fail_json(msg='Cannot find device with UUID: {0}.'.format( viptela.params['uuid'])) # If this is a preallocation, we need to set these things. if 'system-ip' not in device_data or len( device_data['system-ip']) == 0: if 'device_ip' in viptela.params: device_data['system-ip'] = viptela.params['device_ip'] else: viptela.fail_json( msg='device_ip is needed when pre-attaching templates') if 'deviceIP' not in device_data or len(device_data['deviceIP']) == 0: if 'device_ip' in viptela.params: device_data['deviceIP'] = viptela.params['device_ip'] else: viptela.fail_json( msg='device_ip is needed when pre-attaching templates') if 'site-id' not in device_data or len(device_data['site-id']) == 0: if 'site_id' in viptela.params: device_data['site-id'] = viptela.params['site_id'] else: viptela.fail_json( msg='site_id is needed when pre-attaching templates') if 'host-name' not in device_data or len( device_data['host-name']) == 0: if 'device_name' in viptela.params: device_data['host-name'] = viptela.params['device_name'] else: viptela.fail_json( msg='device_name is needed when pre-attaching templates') elif viptela.params['device_name']: device_status = viptela.get_device_status( viptela.params['device_name'], key='host-name') if 'uuid' in device_status: device_type = 'controllers' if device_status['personality'] in [ 'vmanage', 'vbond', 'vsmart' ] else 'vedges' device_data = viptela.get_device_by_uuid(device_status['uuid'], type=device_type) else: viptela.fail_json(msg='Cannot find device with name: {0}.'.format( viptela.params['device'])) if viptela.params['state'] == 'present': if ('system-ip' not in device_data): viptela.fail_json(msg='system-ip must be defined for {0}.'.format( viptela.params['device'])) if ('site-id' not in device_data): viptela.fail_json(msg='site-id must be defined for {0}.'.format( viptela.params['device'])) # Get template data and see if it is a real template device_template_dict = viptela.get_device_template_dict( factory_default=True) if viptela.params['template']: if viptela.params['template'] not in device_template_dict: viptela.fail_json(msg='Template {0} not found.'.format( viptela.params['template'])) template_data = device_template_dict[viptela.params['template']] else: viptela.fail_json(msg='Must specify a template with state present') # Check if the template is a cli-template template_iscli = "configType" in template_data and template_data[ "configType"] == "file" # Make sure they passed in the required variables # get_template_variables provides a variable name -> property mapping template_variables = viptela.get_template_variables( device_template_dict[viptela.params['template']]['templateId']) optional_template_variables = viptela.get_template_optional_variables( device_template_dict[viptela.params['template']]['templateId']) mandatory_template_variables = { k: template_variables[k] for k in set(template_variables) - set(optional_template_variables) } if mandatory_template_variables: if viptela.params['variables']: for variable in mandatory_template_variables: if variable not in viptela.params['variables']: viptela.fail_json( msg='Template {0} requires variables: {1}'.format( viptela.params['template'], ', '.join( template_variables))) viptela.result['template_variables'] = template_variables # Construct the variable payload if template_iscli: #CLI-templates don't have the //system/*-Variables by default device_template_variables = { "csv-status": "complete", "csv-deviceId": device_data['uuid'], "csv-deviceIP": device_data['deviceIP'], "csv-host-name": device_data['host-name'], } else: device_template_variables = { "csv-status": "complete", "csv-deviceId": device_data['uuid'], "csv-deviceIP": device_data['deviceIP'], "csv-host-name": device_data['host-name'], '//system/host-name': device_data['host-name'], '//system/system-ip': device_data['system-ip'], '//system/site-id': device_data['site-id'], } # For each of the variables passed in, match them up with the names of the variables requires in the # templates and add them with the corresponding property. The the variables is not in template_variables, # just leave it out since it is not required. for key, value in viptela.params['variables'].items(): if key in template_variables: property = template_variables[key] device_template_variables[property] = value # When dealing with optional parameters if we do not have explicitely set a value for it # we must add the optional parameter to the payload with { key: 'TEMPLATE_IGNORE'} for key, value in optional_template_variables.items(): property = template_variables[key] if property not in device_template_variables: device_template_variables[property] = 'TEMPLATE_IGNORE' attached_uuid_list = viptela.get_template_attachments( template_data['templateId'], key='uuid') if device_data['uuid'] in attached_uuid_list: # Add the template ID to the device's variable payload because we'll need it for comparison and update. # device_template_variables['csv-templateId'] = template_data['templateId'] # The device is already attached to the template. We need to see if any of the input changed, so we make # an API call to get the input on last attach payload = { "templateId": template_data['templateId'], "deviceIds": [device_data['uuid']], "isEdited": "true", "isMasterEdited": "false" } response = viptela.request( '/dataservice/template/device/config/input/', method='POST', payload=payload) if response.json and 'data' in response.json: current_variables = response.json['data'][0] # viptela.result['old'] = current_variables # viptela.result['new'] = device_template_variables # Convert both to a string and compare. For some reason, there can be an int/str # mismatch. It might be indicative of a problem... for property in device_template_variables: if str(device_template_variables[property]) != str( current_variables[property]): viptela.result['changed'] = True else: viptela.result['changed'] = True if not module.check_mode and viptela.result['changed']: payload = { "deviceTemplateList": [{ "templateId": template_data['templateId'], "device": [device_template_variables], "isEdited": False, "isMasterEdited": False }] } if template_iscli: response = viptela.request( '/dataservice/template/device/config/attachcli', method='POST', payload=payload) else: response = viptela.request( '/dataservice/template/device/config/attachfeature', method='POST', payload=payload) if response.json: action_id = response.json['id'] else: viptela.fail_json( msg= 'Did not get action ID after attaching device to template.' ) elif viptela.params['state'] == 'absent': if 'templateId' in device_data: viptela.result['changed'] = True payload = { "deviceType": device_data['deviceType'], "devices": [{ "deviceId": device_data['uuid'], "deviceIP": device_data['deviceIP'] }] } if not module.check_mode: response = viptela.request( '/dataservice/template/config/device/mode/cli', method='POST', payload=payload) if response.json: action_id = response.json['id'] else: viptela.fail_json( msg= 'Did not get action ID after attaching device to template.' ) elif viptela.params['state'] == 'query': # Get template data and see if it is a real template device_template_dict = viptela.get_device_template_dict( factory_default=True) if viptela.params['template']: if viptela.params['template'] not in device_template_dict: viptela.fail_json(msg='Template {0} not found.'.format( viptela.params['template'])) template_data = device_template_dict[viptela.params['template']] else: viptela.fail_json(msg='Must specify a template with state query') # get_template_variables provides a variable name -> property mapping template_variables = viptela.get_template_variables( device_template_dict[viptela.params['template']]['templateId']) optional_template_variables = viptela.get_template_optional_variables( device_template_dict[viptela.params['template']]['templateId']) viptela.result['template_variables'] = template_variables viptela.result[ 'optional_template_variables'] = optional_template_variables viptela.result['mandatory_template_variables'] = { k: template_variables[k] for k in set(template_variables) - set(optional_template_variables) } # If told, wait for the status of the request and report it if viptela.params['wait'] and action_id: viptela.waitfor_action_completion(action_id) viptela.logout() viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update( state=dict(type='str', choices=['csr', 'cert', 'push'], default='cert'), name=dict(type='str'), transport_ip=dict(type='str', aliases=['device_ip', 'system_ip']), cert=dict(type='str', alias='deviceEnterpriseCertificate'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) viptela.result['what_changed'] = [] device = {} # See if we can find the device by deviceIP if viptela.params['transport_ip']: device = viptela.get_device_by_device_ip( viptela.params['transport_ip'], type='controllers') # If we could not find the device by deviceIP, see if we can find it be (host)name if not device: device = viptela.get_device_by_name(viptela.params['name'], type='controllers') if viptela.params['state'] == 'cert': if device: if not viptela.params['cert']: viptela.fail_json( msg= "'cert' is required when added a certificate to a device") if device: if ('deviceEnterpriseCertificate' not in device) or ( viptela.params['cert'] != device['deviceEnterpriseCertificate']): viptela.result['what_changed'].append( 'deviceEnterpriseCertificate') if not module.check_mode: viptela.install_device_cert(viptela.params['cert']) else: viptela.fail_json( msg="Device must exist to add it's certificate") else: viptela.fail_json(msg="Device not found") elif viptela.params['state'] == 'csr': if 'deviceCSR' in device: viptela.result['deviceCSR'] = device['deviceCSR'] else: viptela.result['what_changed'].append('csr') if not module.check_mode: if 'deviceIP' in device: viptela.result['deviceCSR'] = viptela.generate_csr( device['deviceIP']) else: viptela.fail_json(msg="Cannot find deviceIP for {0}". format(viptela.params['name'])) elif viptela.params['state'] == 'push': viptela.push_certificates() if viptela.result['what_changed']: viptela.result['changed'] = True viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(file = dict(type='str', required=True), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # Read in the datafile if not os.path.exists(viptela.params['file']): module.fail_json(msg='Cannot find file {0}'.format(viptela.params['file'])) with open(viptela.params['file']) as f: template_data = json.load(f) # Separate the feature template data from the device template data feature_template_data = template_data['feature_templates'] device_template_data = template_data['device_templates'] # Process the feature templates feature_templates = viptela.get_feature_template_dict(factory_default=True) for data in feature_template_data: if data['templateName'] not in feature_templates: payload = { 'templateName': data['templateName'], 'templateDescription': data['templateDescription'], 'deviceType': data['deviceType'], 'templateDefinition': data['templateDefinition'], 'templateType': data['templateType'], 'templateMinVersion': data['templateMinVersion'], 'factoryDefault': data['factoryDefault'], 'configType': data['configType'], 'feature': data['feature'], } viptela.result['payload'] = payload # Don't make the actual POST if we are in check mode if not module.check_mode: response = viptela.request('/dataservice/template/feature/', method='POST', data=json.dumps(payload)) viptela.result['changed'] = True # Process the device templates device_templates = viptela.get_device_template_dict() for device_template in device_template_data: if device_template['templateName'] not in device_templates: payload = { 'templateName': device_template['templateName'], 'templateDescription': device_template['templateDescription'], 'deviceType': device_template['deviceType'], 'factoryDefault': device_template['factoryDefault'], 'configType': device_template['configType'] } # # File templates are much easier in that they are just a bunch of CLI # if device_template['configType'] == 'file': payload['templateConfiguration'] = device_template['templateConfiguration'] if not module.check_mode: viptela.request('/dataservice/template/device/cli', method='POST', payload=payload) # # Feature based templates are just a list of templates Id that make up a devie template. We are # given the name of the feature templates, but we need to translate that to the template ID # else: if 'generalTemplates' in device_template: payload['generalTemplates'] = viptela.generalTemplates_to_id(device_template['generalTemplates']) else: viptela.fail_json(msg="No generalTemplates found in device template", data=device_template) payload['policyId'] = '' if 'connectionPreference' in device_template: payload['connectionPreference'] = device_template['connectionPreference'] if 'connectionPreferenceRequired' in device_template: payload['connectionPreferenceRequired'] = device_template['connectionPreferenceRequired'] payload['featureTemplateUidRange'] = [] # Don't make the actual POST if we are in check mode if not module.check_mode: viptela.request('/dataservice/template/device/feature', method='POST', payload=payload) viptela.result['changed'] = True viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(state=dict(type='str', choices=['absent', 'present'], default='present'), file=dict(type='str'), aggregate=dict(type='list')) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) if viptela.params['aggregate']: upload_software_list = viptela.params['aggregate'] else: upload_software_list = [{'file': module.params['file']}] # THIS MODULE IS DESIGNED TO UPLOAD UPGRADE IMAGES TO THE VMANAGE # Software in SD-WAN varies depending on what you want to upgrade. # This is a complication for what concern idempotency of this module # Files to upgrade vmanage will look like: vmanage-XX.YY.ZZ-<platform>.tar.gz # Files to upgrade vedge cloud/vedge 5k/vbond/vsmart will look like: viptela-XX.YY.ZZ-<platform>.tar.gz # Physical appliances will NOT have incremental upgrade images # CISCO Physical appliances will be upgraded via a new .bin file # VIPTELA Physical appliances will be upgraded via a new .tar.gz file viptela.result['changed'] = False vManage_software_list = viptela.get_software_images_list() if viptela.params['state'] == 'present': for software_to_upload in upload_software_list: try: present = False path_software_to_be_uploaded = software_to_upload['file'] if not os.path.exists(path_software_to_be_uploaded): module.fail_json(msg="File does not exists") filename_software_to_be_uploaded = os.path.basename( path_software_to_be_uploaded) for software in vManage_software_list: availabe_files_list = software["availableFiles"].split( ', ') if filename_software_to_be_uploaded in availabe_files_list: present = True if not module.check_mode and not present: response = viptela.request( '/dataservice/device/action/software/package', method='POST', files={ 'file': open(path_software_to_be_uploaded, 'rb') }, data={ 'validity': 'valid', 'upload': 'true' }, headers=None) viptela.result['changed'] = True except Exception as e: module.fail_json(msg="General Error {0}".format(e)) else: # absent to be added pass viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update( name=dict(type='str'), device_ip=dict(type='str', alias='deviceIP'), uuid=dict(type='str'), model=dict(type='str'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) viptela.result['what_changed'] = [] viptela.result['bootstrap'] = {} device = {} uuid = None if viptela.params['uuid']: uuid = viptela.params['uuid'] device = viptela.get_device_by_uuid(viptela.params['uuid']) elif viptela.params['device_ip']: # See if we can find the device by deviceIP device = viptela.get_device_by_device_ip(viptela.params['device_ip']) if 'uuid' in device: uuid = device['uuid'] elif viptela.params['name']: device = viptela.get_device_by_name(viptela.params['name']) if 'uuid' in device: uuid = device['uuid'] if uuid: if device['vedgeCertificateState'] in [ 'tokengenerated', 'bootstrapconfiggenerated' ]: # We can only generate bootstrap in these two states. Otherwise, the # device is in-use and cannot be bootstrapped. viptela.result['what_changed'].append('bootstrap') if not module.check_mode: bootstrap = viptela.generate_bootstrap(uuid) viptela.result['uuid'] = uuid viptela.result['bootstrap'] = bootstrap # else: # viptela.fail_json(msg="Could not generate bootstrap for UUID: {0}. 'vedgeCertificateState' not 'tokengenerated' or 'bootstrapconfiggenerated'".format(uuid)) elif viptela.params['model']: # if no uuid was specified, just grab the first free device device = viptela.get_unused_device(viptela.params['model']) if not device: viptela.fail_json(msg="Could not find available device") viptela.result['what_changed'].append('bootstrap') if not module.check_mode: bootstrap = viptela.generate_bootstrap(device['uuid']) viptela.result['bootstrap'] = bootstrap else: viptela.fail_json(msg="Could not find UUID with supplied arguments") if viptela.result['what_changed']: viptela.result['changed'] = True viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(state=dict(type='str', choices=['absent', 'present'], default='present'), name = dict(type='str', alias='templateName'), description = dict(type='str', alias='templateDescription'), templates = dict(type='str', alias='generalTemplates'), device_type = dict(type='str', alias='deviceType'), config_type=dict(type='str', alias='configType'), factory_default=dict(type='bool', alias='factoryDefault'), aggregate=dict(type='list'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # Always as an aggregate... make a list if just given a single entry if viptela.params['aggregate']: device_template_list = viptela.params['aggregate'] else: if viptela.params['state'] == 'present': device_template_list = [ { 'templateName': viptela.params['name'], 'templateDescription': viptela.params['description'], 'deviceType': viptela.params['device_type'], 'configType': viptela.params['config_type'], 'factoryDefault': viptela.params['factory_default'], 'generalTemplates': viptela.params['templates'], 'state': 'present', } ] else: device_template_list = [ { 'templateName': viptela.params['name'], 'state': 'absent' } ] device_template_dict = viptela.get_device_template_dict(factory_default=True, remove_key=False) compare_values = ['templateDescription', 'deviceType', 'configType', 'generalTemplates'] ignore_values = ["lastUpdatedOn", "lastUpdatedBy", "templateId", "createdOn", "createdBy"] print("~~~~~~~~~~~~") for device_template in device_template_list: if viptela.params['state'] == 'present': payload = { 'templateName': device_template['templateName'], 'templateDescription': device_template['templateDescription'], 'deviceType': device_template['deviceType'], 'factoryDefault': device_template['factoryDefault'], 'configType': device_template['configType'], } payload['policyId'] = '' payload['featureTemplateUidRange'] = [] # If a template by that name is already there if payload['templateName'] in device_template_dict: viptela.result['changed'] = False # changed_items = viptela.compare_payloads(payload, device_template_dict[payload['templateName']], compare_values=compare_values) # if changed_items: # viptela.result['changed'] = True # viptela.result['what_changed'] = changed_items # viptela.result['old_payload'] = device_template_dict[payload['templateName']] # if not module.check_mode: # # # # Convert template names to template IDs # # # if payload['configType'] == 'template': # payload['generalTemplates'] = viptela.generalTemplates_to_id(device_template['generalTemplates']) # viptela.request('/dataservice/template/device/feature/{0}'.format(device_template_dict[payload['templateName']]['templateId']), # method='PUT', payload=payload) else: if not module.check_mode: # # Convert template names to template IDs # if device_template['configType'] == 'template': payload['generalTemplates'] = viptela.generalTemplates_to_id(device_template['generalTemplates']) if device_template['configType'] == "cli": # If the template is a cli template, the parameter in which to send the template is called "templateConfiguration" payload['templateConfiguration'] = device_template['generalTemplates'] payload['configType'] = 'file' payload['cliType'] = 'device' if device_template['configType'] == "cli": viptela.request('/dataservice/template/device/cli', method='POST', payload=payload) else: viptela.request('/dataservice/template/device/feature', method='POST', payload=payload) viptela.result['changed'] = True else: if device_template['templateName'] in device_template_dict: if not module.check_mode: viptela.request('/dataservice/template/device/{0}'.format(device_template_dict[device_template['templateName']]['templateId']), method='DELETE') viptela.result['changed'] = True viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(organization=dict(type='str'), vbond=dict(type='str'), vbond_port=dict(type='str', default='12346'), root_cert=dict(type='str'), ca_type=dict(type='str')) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) viptela.result['what_changed'] = [] if viptela.params['organization']: current = viptela.get_vmanage_org() if viptela.params['organization'] != current: viptela.result['what_changed'].append('organization') if not module.check_mode: viptela.set_vmanage_org(viptela.params['organization']) if viptela.params['vbond']: current = viptela.get_vmanage_vbond() if viptela.params['vbond'] != current['vbond'] or viptela.params[ 'vbond_port'] != current['vbond_port']: viptela.result['what_changed'].append('vbond') if not module.check_mode: viptela.set_vmanage_vbond(viptela.params['vbond'], viptela.params['vbond_port']) if viptela.params['ca_type']: current = viptela.get_vmanage_ca_type() if viptela.params['ca_type'] != current: viptela.result['what_changed'].append('ca_type') if not module.check_mode: viptela.set_vmanage_ca_type(viptela.params['ca_type']) if viptela.params['root_cert']: current = viptela.get_vmanage_root_cert() if viptela.params['root_cert'] not in current: viptela.result['what_changed'].append('root_cert') if not module.check_mode: viptela.set_vmanage_root_cert(viptela.params['root_cert']) if viptela.result['what_changed']: viptela.result['changed'] = True viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update( vedge=dict(type='str', required=True), dst_ip=dict(type='str', required=True, alias='host'), vpn=dict(type='int', default=0, required=False), src_interface=dict(type='str', required=False, alias='source'), probe_type=dict(type='str', required=False, default='icmp', alias='probeType'), count=dict(type='str', required=False), size=dict(type='str', required=False), df=dict(type='str', required=False), rapid=dict(type='bool', required=False), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) device_dict = viptela.get_device_dict('vedge') if viptela.params['vedge'] in device_dict: system_ip = device_dict[viptela.params['vedge']]['system-ip'] else: viptela.fail_json( msg="Cannot find vedge {0}".format(viptela.params['vedge'])) payload = { 'host': viptela.params['dst_ip'], 'vpn': str(viptela.params['vpn']), 'probeType': viptela.params['probe_type'], } if viptela.params['src_interface']: payload['source'] = viptela.params['src_interface'] if viptela.params['count']: payload['count'] = viptela.params['count'] if viptela.params['size']: payload['size'] = viptela.params['size'] if viptela.params['df']: payload['df'] = viptela.params['df'] if viptela.params['rapid']: payload['rapid'] = "true" if viptela.params['rapid'] else "false" response = viptela.request( '/dataservice/device/tools/nping/{0}'.format(system_ip), method='POST', payload=payload) if response.json: viptela.result['json'] = response.json viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(state=dict(type='str', choices=['absent', 'present'], default='present'), aggregate=dict(type='list'), name=dict(type='str'), description=dict(type='str'), type=dict(type='str', required=False, choices=[ 'all', 'color', 'vpn', 'site', 'app', 'dataprefix', 'prefix', 'aspath', 'class', 'community', 'extcommunity', 'mirror', 'tloc', 'sla', 'policer', 'ipprefixall', 'dataprefixall' ], default='all'), entries=dict(type='list'), push=dict(type='bool', default=False), force=dict(type='bool', default=False)) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # Always as an aggregate... make a list if just given a single entry if viptela.params['aggregate']: policy_list = viptela.params['aggregate'] else: policy_list = [{ "name": viptela.params['name'], "description": viptela.params['description'], "type": viptela.params['type'], "entries": viptela.params['entries'], }] policy_list_dict = viptela.get_policy_list_dict(viptela.params['type'], remove_key=False) compare_values = ["name", "description", "type", "entries"] # Import site lists for list in policy_list: if viptela.params['state'] == 'present': if list['name'] in policy_list_dict: # FIXME Just compare the entries for now. if (list['entries'] != policy_list_dict[list['name']] ['entries']) or viptela.params['force']: list['listId'] = policy_list_dict[list['name']]['listId'] viptela.result['new_entries'] = list['entries'] viptela.result['existing_entries'] = policy_list_dict[ list['name']]['entries'] # If description is not specified, try to get it from the existing information if not list['description']: list['description'] = policy_list_dict[ list['name']]['description'] viptela.result['changed'] = True if not module.check_mode: viptela.result['put_payload'] = list response = viptela.request( '/dataservice/template/policy/list/{0}/{1}'.format( list['type'].lower(), list['listId']), method='PUT', payload=list) viptela.result['response'] = response.json if response.json: # Updating the policy list returns a `processId` that locks the list and 'masterTemplatesAffected' # that lists the templates affected by the change. if 'processId' in response.json: process_id = response.json['processId'] viptela.result['put_payload'] = response.json[ 'processId'] if viptela.params['push']: # If told to push out the change, we need to reattach each template affected by the change for template_id in response.json[ 'masterTemplatesAffected']: action_id = viptela.reattach_device_template( template_id, process_id=process_id) # Delete the lock on the policy list # FIXME: The list does not seem to update when we unlock too soon, so I think that we need # to wait for the attachment, but need to understand this better. # response = viptela.request('/dataservice/template/lock/{0}'.format(process_id), method='DELETE') # viptela.result['lock_response'] = response.json else: if viptela.params['push']: viptela.fail_json( msg= "Cannot push changes: Did not get a process id when updating policy list" ) else: if not module.check_mode: viptela.request( '/dataservice/template/policy/list/{0}/'.format( list['type'].lower()), method='POST', payload=list) viptela.result['changed'] = True else: if list['name'] in policy_list_dict: if not module.check_mode: viptela.request( '/dataservice/template/policy/list/{0}/{1}'.format( list['type'].lower(), list['listId']), method='DELETE') viptela.result['changed'] = True viptela.logout() viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(devices=dict(type='list'), deviceType=dict(type='str', choices=['controller', 'vedge'], default='vedge'), version=dict(type='str'), activate=dict(type='bool'), set_default=dict(type='bool')) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # It seems to me that the software operations on the vManage are already idempotent # This is why at least in this phase we are not implementing other idempotency checks # In future it might make sense to implement them to avoid generating useless tasks # on the vManage. This would streamline operations. # TODO Add idempotency cheks to this module devices = module.params['devices'] deviceType = module.params['deviceType'] version = module.params['version'] data = [{"family": "vedge-x86", "version": version}] reboot = module.params['activate'] set_default = module.params['set_default'] # Verify if the target software (version) is available in the vManage and set flag vManage_software_list = viptela.get_software_images_list() software_present_on_vManage = False for software in vManage_software_list: if version == software['versionName']: software_present_on_vManage = True # If we have found the software we can move on and perform the operation if software_present_on_vManage: viptela.software_install(devices, deviceType, data, reboot) if set_default: for device in devices: device.update({'version': version}) viptela.set_default_partition(devices, deviceType) # If not, we fail else: module.fail_json(msg="We don't have this software, I am sorry") viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update( state=dict(type='str', choices=['absent', 'present'], default='present'), aggregate=dict(type='list'), name=dict(type='str'), description=dict(type='str'), type=dict(type='str', required=True, choices=[ 'cflowd', 'dnssecurity', 'control', 'hubandspoke', 'acl', 'vpnmembershipgroup', 'mesh', 'rewriterule', 'data', 'rewriterule', 'aclv6' ]), sequences=dict(type='list'), default_action=dict(type='dict', alias='defaultAction'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # Always as an aggregate... make a list if just given a single entry if viptela.params['aggregate']: definition_list = viptela.params['aggregate'] else: definition_list = [{ "name": viptela.params['name'], "description": viptela.params['description'], "type": viptela.params['type'], "sequences": viptela.params['sequences'], "defaultAction": viptela.params['default_action'] }] policy_definition_dict = viptela.get_policy_definition_dict( viptela.params['type'], remove_key=False) compare_values = [ "name", "description", "type", "sequences", "defaultAction" ] # Import site lists for list in definition_list: payload = { "name": list['name'], "description": list['description'], "type": list['type'], "sequences": list['sequences'], "defaultAction": list['defaultAction'] } if viptela.params['state'] == 'present': if list['name'] in policy_definition_dict: changed_items = viptela.compare_payloads( list, policy_definition_dict[list['name']], compare_values=compare_values) if changed_items: viptela.result['changed'] = True viptela.result['what_changed'] = changed_items payload['sequences'] = viptela.convert_sequences_to_id( list['sequences']) if not module.check_mode: viptela.request( '/dataservice/template/policy/definition/{0}/{1}'. format( list['type'], policy_definition_dict[ list['name']]['definitionId']), method='PUT', payload=payload) else: payload['sequences'] = viptela.convert_sequences_to_id( list['sequences']) if not module.check_mode: viptela.request( '/dataservice/template/policy/definition/{0}/'.format( list['type']), method='POST', payload=payload) viptela.result['changed'] = True else: if list['name'] in policy_definition_dict: if not module.check_mode: viptela.request( '/dataservice/template/policy/definition/{0}/{1}'. format(list['type'], list['definitionId']), method='DELETE') viptela.result['changed'] = True viptela.exit_json(**viptela.result)
def run_module(): action_id = None action_status = None action_activity = None action_config = None # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update( state=dict(type='str', choices=['absent', 'present'], default='present'), device=dict(type='str', required=True), template=dict(type='str'), variables=dict(type='dict', default={}), wait=dict(type='bool', default=False), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # First, get the list of vedges and see if this device is in the list devices = viptela.get_device_dict('vedges') if viptela.params['device'] not in devices: # Next, get the list of controllers and see if this device is in the list devices = viptela.get_device_dict('controllers') if viptela.params['device'] not in devices: # This device is neither a vedge or a controller, so error and inform accordingly viptela.fail_json( msg='Device {0} not found.'.format(viptela.params['device'])) device_data = devices[viptela.params['device']] if viptela.params['state'] == 'present': if ('system-ip' not in device_data): viptela.fail_json(msg='system-ip must be defined for {0}.'.format( viptela.params['device'])) if ('site-id' not in device_data): viptela.fail_json(msg='site-id must be defined for {0}.'.format( viptela.params['device'])) # Get template data and see if it is a real template device_template_dict = viptela.get_device_template_dict( factory_default=True) if viptela.params['template']: if viptela.params['template'] not in device_template_dict: viptela.fail_json(msg='Template {0} not found.'.format( viptela.params['template'])) template_data = device_template_dict[viptela.params['template']] else: viptela.fail_json(msg='Must specify a template with state present') # Make sure they passed in the required variables template_variables = viptela.get_template_variables( device_template_dict[viptela.params['template']]['templateId']) if template_variables: if viptela.params['variables']: for variable in template_variables: if variable not in viptela.params['variables']: viptela.fail_json( msg='Template {0} requires variables: {1}'.format( viptela.params['template'], ', '.join( template_variables))) # Construct the variable payload device_template_variables = { "csv-status": "complete", "csv-deviceId": device_data['uuid'], "csv-deviceIP": device_data['deviceIP'], "csv-host-name": viptela.params['device'], # "csv-templateId": template_data['templateId'], '//system/host-name': viptela.params['device'], '//system/system-ip': device_data['system-ip'], '//system/site-id': device_data['site-id'], } # For each of the variables passed in, match them up with the names of the variables requires in the # templates and add them with the corresponding property. The the variables is not in template_variables, # just leave it out since it is not required. for key, value in viptela.params['variables'].items(): if key in template_variables: property = template_variables[key] device_template_variables[property] = viptela.params[ 'variables'][key] if viptela.params['device'] in template_data['attached_devices']: # Add the template ID to the device's variable payload because we'll need it for comparison and update. # device_template_variables['csv-templateId'] = template_data['templateId'] # The device is already attached to the template. We need to see if any of the input changed, so we make # an API call to get the input on last attach payload = { "templateId": template_data['templateId'], "deviceIds": [device_data['uuid']], "isEdited": "true", "isMasterEdited": "false" } response = viptela.request( '/dataservice/template/device/config/input/', method='POST', payload=payload) if response.json and 'data' in response.json: if response.json['data'][0] != device_template_variables: viptela.result['changed'] = True viptela.result['current_variables'] = response.json['data'] viptela.result['new_variables'] = device_template_variables else: viptela.result['changed'] = False else: viptela.result['changed'] = True if not module.check_mode and viptela.result['changed']: payload = { "deviceTemplateList": [{ "templateId": template_data['templateId'], "device": [device_template_variables], "isEdited": False, "isMasterEdited": False }] } response = viptela.request( '/dataservice/template/device/config/attachfeature', method='POST', payload=payload) if response.json: action_id = response.json['id'] else: viptela.fail_json( msg= 'Did not get action ID after attaching device to template.' ) else: if 'templateId' in device_data: viptela.result['changed'] = True payload = { "deviceType": device_data['deviceType'], "devices": [{ "deviceId": device_data['uuid'], "deviceIP": device_data['deviceIP'] }] } if not module.check_mode: response = viptela.request( '/dataservice/template/config/device/mode/cli', method='POST', payload=payload) if response.json: action_id = response.json['id'] else: viptela.fail_json( msg= 'Did not get action ID after attaching device to template.' ) # If told, wait for the status of the request and report it if viptela.params['wait'] and action_id: viptela.waitfor_action_completion(action_id) viptela.logout() viptela.exit_json(**viptela.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update(file=dict(type='str', required=True), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, original_message='', message='' ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications if module.check_mode: return result # manipulate or modify the state as needed (this is going to be the # part where your module will do what it needs to do) # result['original_message'] = module.params['name'] # use whatever logic you need to determine whether or not this module # made any modifications to your target # if module.params['new']: # result['changed'] = True response = viptela.request('/dataservice/system/device/fileupload', method='POST', files={'file': open(module.params['file'])}, data={'validity':'valid', 'upload':'true'}, headers=None) json = response.json() viptela.result['json'] = json viptela.result['msg'] = json['vedgeListUploadStatus'] if 'successfully' in json['vedgeListUploadStatus']: viptela.result['changed'] = True # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications # FIXME: Work with viptela so they can implement a check mode if module.check_mode: viptela.exit_json() # execute checks for argument completeness # manipulate or modify the state as needed (this is going to be the # part where your module will do what it needs to do) # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results viptela.exit_json()
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = viptela_argument_spec() argument_spec.update( state=dict(type='str', choices=['absent', 'present'], default='present'), name=dict(type='str', alias='templateName'), description=dict(type='str', alias='templateDescription'), definition=dict(type='dict', alias='templateDefinition'), template_type=dict(type='str', alias='templateType'), device_type=dict(type='list', alias='deviceType'), template_min_version=dict(type='str', alias='templateMinVersion'), factory_default=dict(type='bool', alias='factoryDefault'), url=dict(type='bool', alias='templateUrl'), aggregate=dict(type='list'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) viptela = viptelaModule(module) # Always as an aggregate... make a list if just given a single entry if viptela.params['aggregate']: feature_template_list = viptela.params['aggregate'] else: if viptela.params['state'] == 'present': try: feature_template_list = [{ 'templateName': viptela.params['name'], 'templateDescription': viptela.params['description'], 'deviceType': viptela.params['device_type'], 'templateDefinition': viptela.params['definition'], 'templateType': viptela.params['template_type'], 'templateMinVersion': viptela.params['template_min_version'], 'factoryDefault': viptela.params['factory_default'] }] except: module.fail_json( msg= "Required values: name, description, device_type, definition, template_type, template_min_version, factory_default" ) else: try: feature_template_list = [{ 'templateName': viptela.params['name'] }] except: module.fail_json(msg='Required values: name') feature_template_dict = viptela.get_feature_template_dict( factory_default=True, remove_key=False) ignore_values = [ "lastUpdatedOn", "lastUpdatedBy", "templateId", "createdOn", "createdBy" ] compare_values = [ 'templateDescription', 'deviceType', 'templateType', 'templateDefinition', 'templateMinVersion' ] for feature_template in feature_template_list: if viptela.params['state'] == 'present': payload = { 'templateName': feature_template['templateName'], 'templateDescription': feature_template['templateDescription'], 'deviceType': feature_template['deviceType'], 'templateType': feature_template['templateType'], 'templateMinVersion': feature_template['templateMinVersion'], 'factoryDefault': feature_template['factoryDefault'], 'templateDefinition': feature_template['templateDefinition'] } # FIXME (Issue #1): This is a temporary workaround for the fact that vManage requires it payload in a specific order template_definition = OrderedDict() if 'if-name' in feature_template['templateDefinition']: template_definition['if-name'] = feature_template[ 'templateDefinition'].pop('if-name') if 'vpn-id' in feature_template['templateDefinition']: template_definition['vpn-id'] = feature_template[ 'templateDefinition'].pop('vpn-id') for key, value in feature_template['templateDefinition'].items(): template_definition[key] = value payload['templateDefinition'] = template_definition if payload['templateName'] in feature_template_dict: viptela.result['changed'] = False # changed_items = viptela.compare_payloads(payload, feature_template_dict[payload['templateName']], compare_values=compare_values) # if changed_items: # viptela.result['changed'] = True # viptela.result['what_changed'] = changed_items # if not module.check_mode: # viptela.request('/dataservice/template/feature/{0}'.format(feature_template_dict[payload['templateName']]['templateId']), # method='PUT', payload=payload) else: if not module.check_mode: viptela.request('/dataservice/template/feature/', method='POST', payload=payload) viptela.result['changed'] = True else: if feature_template['templateName'] in feature_template_dict: if not module.check_mode: viptela.request('/dataservice/template/feature/{0}'.format( feature_template_dict[ feature_template['templateName']]['templateId']), method='DELETE') viptela.result['changed'] = True viptela.exit_json(**viptela.result)