def main(): argument_spec = vmware_argument_spec() module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) changed = False try: (rc, resp) = request(manager_url + '/logical-ports', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg='Error accessing list of logical ports. Error [%s]' % (to_native(err))) module.exit_json(changed=changed, **resp)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(wait_time=dict(required=False, type='int')) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) changed = False wait_time = 10 # wait till 30 min while wait_time < (module.params['wait_time'] * 60): try: current_time = datetime.now() (rc, resp) = request(manager_url + '/cluster/nodes/deployments', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) module.exit_json(changed=changed, msg=" NSX manager is UP") except Exception as err: time_diff = datetime.now() - current_time time.sleep(10) wait_time = time_diff.seconds + wait_time + 10 module.fail_json(changed=changed, msg=" Error accessing nsx manager. Timeed out")
def get_logical_switches(module, manager_url, mgr_username, mgr_password, validate_certs): try: (rc, resp) = request(manager_url+ '/logical-switches', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg='Error accessing logical switches. Error [%s]' % (to_native(err))) return resp
def wait_till_create(id, module, manager_url, mgr_username, mgr_password, validate_certs): DEPLOYMENT_PROGRESS = [ 'INSTALL_IN_PROGRESS', 'VM_DEPLOYMENT_IN_PROGRESS', 'VM_DEPLOYMENT_QUEUED', 'VM_POWER_ON_IN_PROGRESS', 'NODE_NOT_READY', 'REGISTRATION_PENDING' ] DEPLOYMENT_SUCCESS = ['NODE_READY', 'INSTALL_SUCCESSFUL'] try: while True: (rc, resp) = request(manager_url + '/fabric/nodes/%s/status' % id, headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) if resp['host_node_deployment_status'] in DEPLOYMENT_PROGRESS: time.sleep(10) elif resp['host_node_deployment_status'] in DEPLOYMENT_SUCCESS: time.sleep(5) return else: module.fail_json(msg='Error in fabric node status: %s' % (str(resp['host_node_deployment_status']))) except Exception as err: module.fail_json(msg='Error accessing fabric node status. Error [%s]' % (to_native(err)))
def main(): argument_spec = vmware_argument_spec() argument_spec.update(license_key=dict(required=True, type='str', no_log=True), state=dict(reauired=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) license_params = get_license_params(module.params.copy()) state = module.params['state'] mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' request_data = json.dumps(license_params) if state == 'present': # add the license if check_license_exist(module, manager_url, mgr_username, mgr_password, validate_certs): module.exit_json(changed=False, message="license with license key %s already exist."% module.params['license_key']) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id=module.params['license_key']) try: (rc, resp) = request(manager_url+ '/licenses', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg="Failed to add license. Request body [%s]. Error[%s]." % (request_data, to_native(err))) time.sleep(5) module.exit_json(changed=True, result=resp, message="license with license key %s created." % module.params['license_key'])
def get_compute_collecting_id(module, manager_url, mgr_username, mgr_password, validate_certs, manager_name, cluster_name): try: (rc, resp) = request(manager_url + '/fabric/compute-collections', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) compute_manager_id = get_id_from_display_name( module, manager_url, mgr_username, mgr_password, validate_certs, "/fabric/compute-managers", manager_name) except Exception as err: module.fail_json( msg= 'Error accessing compute collection id for manager %s, cluster %s. Error [%s]' % (manager_name, cluster_name, to_native(err))) for result in resp['results']: if result.__contains__('display_name') and result['display_name'] == cluster_name and \ result['origin_id'] == compute_manager_id: return result['external_id'] module.fail_json( msg= 'No compute collection id exist with cluster name %s for compute manager %s' % (cluster_name, manager_name))
def get_compute_collection_transport_templates(module, manager_url, mgr_username, mgr_password, validate_certs): try: (rc, resp) = request(manager_url+ '/compute-collection-transport-node-templates', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg='Error accessing transport compute collection transport template. Error [%s]' % (to_native(err))) return resp
def get_id_from_display_name(module, manager_url, mgr_username, mgr_password, validate_certs, endpoint, display_name, exit_if_not_found=True): try: (rc, resp) = request(manager_url + endpoint, headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg='Error accessing id for display name %s. Error [%s]' % (display_name, to_native(err))) for result in resp['results']: if result.__contains__( 'display_name') and result['display_name'] == display_name: return result['id'] if exit_if_not_found: module.fail_json(msg='No id exist with display name %s' % display_name)
def get_fabric_compute_managers(module, manager_url, mgr_username, mgr_password, validate_certs): try: (rc, resp) = request(manager_url+ '/fabric/compute-managers', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg='Error accessing fabric compute manager. Error [%s]' % (to_native(err))) return resp
def check_license_exist(module, manager_url, mgr_username, mgr_password, validate_certs): id = module.params['license_key'] try: (rc, resp) = request(manager_url+ '/licenses/%s' % id, headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: return False return True
def wait_till_delete(id, module, manager_url, mgr_username, mgr_password, validate_certs): try: while True: (rc, resp) = request(manager_url+ '/fabric/compute-managers/%s/status'% id, headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) time.sleep(10) except Exception as err: time.sleep(5) return
def get_certificate_id_with_name(module,manager_url, NSX_USER, NSX_PASSWORD, validate_certs,cert_name ): try: (rc, resp) = request(manager_url+'/trust-management/certificates', headers=dict(Accept='application/json'), url_username=NSX_USER, url_password=NSX_PASSWORD, validate_certs=validate_certs, ignore_errors=True) except Exception as err: print err for result in resp['results']: if result.__contains__('display_name') and result['display_name'] == cert_name: return result['id']
def import_certificate(module,manager_url, NSX_USER, NSX_PASSWORD, validate_certs,request_data,cert_id): request_obj = json.dumps(request_data) if cert_id is None: try: if cert_id: print("Certificate Already Exists with that name %s" %(cert_id)) (rc, resp) = request(manager_url+ '/trust-management/certificates?action=import', data=request_obj, headers=headers, method='POST', url_username=NSX_USER, url_password=NSX_PASSWORD, validate_certs=validate_certs, ignore_errors=True) logger.info("Successfully Imported the Certificate") except Exception as err: module.fail_json(changed=False,msg="Failed to Import the Certificate.Error:%s." % (to_native(err)))
def get_controllers(module, manager_url, mgr_username, mgr_password, validate_certs): try: (rc, resp) = request(manager_url + '/cluster/nodes/deployments', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg='Error accessing controller. Error [%s]' % (to_native(err))) return resp
def get_certificate_id_with_name(manager_url, NSX_USER, NSX_PASSWORD, validate_certs, PI_NAME): try: (rc, resp) = request(manager_url + '/trust-management/certificates', headers=dict(Accept='application/json'), url_username=NSX_USER, url_password=NSX_PASSWORD, validate_certs=validate_certs, ignore_errors=True) except Exception as err: logger.info(err) module.fail_json(msg="Error at getting Certificate") for result in resp['results']: if result.__contains__( 'display_name') and result['display_name'] == PI_NAME: return result['id']
def get_logical_router_static_routes(module, manager_url, mgr_username, mgr_password, validate_certs, logical_router_id): try: (rc, resp) = request( manager_url + '/logical-routers/%s/routing/static-routes' % logical_router_id, headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg='Error accessing logical router ports. Error [%s]' % (to_native(err))) return resp
def get_logical_router_id_from_router_name(module, manager_url, mgr_username, mgr_password, validate_certs, router_name): try: (rc, resp) = request(manager_url + '/logical-routers', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg='Error No Logical Routers %s. Error: [%s]' % (router_name, to_native(err))) for result in resp['results']: if result.__contains__( 'display_name') and result['display_name'] == router_name: return result['id'] module.fail_json(msg='No id exist with router name%s' % router_name)
def get_routing_advertisement(module, manager_url, mgr_username, mgr_password, validate_certs, router_name): logical_router_id = get_logical_router_id_from_router_name( module, manager_url, mgr_username, mgr_password, validate_certs, router_name) try: (rc, resp) = request(manager_url + '/logical-routers/' + logical_router_id + '/routing/advertisement', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg='Error accessing Router Advertisment. Error [%s]' % (to_native(err))) return resp
def wait_till_create(id, module, manager_url, mgr_username, mgr_password, validate_certs): try: while True: (rc, resp) = request(manager_url+ '/fabric/compute-managers/%s/status'% id, headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) if resp['registration_status'] == "REGISTERING": time.sleep(10) elif resp['registration_status'] == "REGISTERED": if resp["connection_status"] == "CONNECTING": time.sleep(10) elif resp["connection_status"] == "UP": time.sleep(5) return else: module.fail_json(msg= 'Error connecting to compute manager. Connection status : %s'%(str(resp["connection_status"]))) else: module.fail_json(msg= 'Error in compute manager status: %s'%(str(resp['registration_status']))) except Exception as err: module.fail_json(msg='Error accessing compute manager status. Error [%s]' % (to_native(err)))
def get_edge_profile_id(module, manager_url, mgr_username, mgr_password, validate_certs, edge_profile): try: (rc, resp) = request(manager_url + '/cluster-profiles', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) logger.info(resp) except Exception as err: module.fail_json(msg='Error accessing logical routers. Error [%s]' % (to_native(err))) for result in resp['results']: if result.__contains__( 'display_name') and result['display_name'] == edge_profile: return result['id'] module.fail_json(msg="No name exist with that cluster profile name - %s" % edge_profile)
def get_nat_rules(module, manager_url, mgr_username, mgr_password, validate_certs, router_name): router_id = get_router_id_from_router_name(module, manager_url, mgr_username, mgr_password, validate_certs, router_name) logger.info(router_id) try: (rc, resp) = request(manager_url + '/logical-routers/' + router_id + '/nat/rules', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) logger.info(resp) except Exception as err: module.fail_json( msg='Error accessing while getting the data. Error [%s]' % (to_native(err))) return resp
def wait_till_create(vm_id, module, manager_url, mgr_username, mgr_password, validate_certs): try: while True: (rc, resp) = request(manager_url + '/transport-nodes/%s/state' % vm_id, headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) if any(resp['state'] in progress_status for progress_status in IN_PROGRESS_STATES): time.sleep(10) elif any(resp['state'] in progress_status for progress_status in SUCCESS_STATES): time.sleep(5) return else: module.fail_json(msg='Error creating transport node: %s' % (str(resp['state']))) except Exception as err: module.fail_json(msg='Error accessing transport node. Error [%s]' % (to_native(err)))
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), compute_manager_name=dict(required=False, type='str'), cluster_name=dict(required=False, type='str'), auto_install_nsx=dict(required=False, type='bool'), state=dict(reauired=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_if=[['state', 'present', ['compute_manager_name', 'cluster_name', 'auto_install_nsx']]]) compute_collection_templates_params = get_compute_collection_templates_params(module.params.copy()) state = module.params['state'] mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] display_name = module.params['display_name'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) compute_collection_templates_dict = get_compute_collection_templates_from_display_name (module, manager_url, mgr_username, mgr_password, validate_certs, display_name) compute_collection_templates_id, revision = None, None if compute_collection_templates_dict: compute_collection_templates_id = compute_collection_templates_dict['id'] revision = compute_collection_templates_dict['_revision'] if state == 'present': body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, compute_collection_templates_params) updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, body) headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' if not updated: # add the compute_collection_templates request_data = json.dumps(compute_collection_templates_params) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id='12345') try: if compute_collection_templates_id: module.exit_json(changed=False, id=compute_collection_templates_id, message="Compute collection fabric template with display_name %s already exist."% module.params['display_name']) (rc, resp) = request(manager_url+ '/fabric/compute-collection-fabric-templates', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg="Failed to add compute_collection_templates. Request body [%s]. Error[%s]." % (request_data, to_native(err))) module.exit_json(changed=True, id=resp["id"], body= str(resp), message="Compute collection fabric template created for cluster %s." % module.params['cluster_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(compute_collection_templates_params)), id=compute_collection_templates_id) compute_collection_templates_params['_revision'] = revision # update current revision request_data = json.dumps(compute_collection_templates_params) id = compute_collection_templates_id try: (rc, resp) = request(manager_url+ '/fabric/compute-collection-fabric-templates/%s' % id, data=request_data, headers=headers, method='PUT', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg="Failed to update compute_collection_templates with id %s. Request body [%s]. Error[%s]." % (id, request_data, to_native(err))) module.exit_json(changed=True, id=resp["id"], body= str(resp), message="fabric compute collection fabric template with Compute collection fabric template id %s updated." % id) elif state == 'absent': # delete the array id = compute_collection_templates_id if id is None: module.exit_json(changed=False, msg='No Compute collection fabric template exist with display_name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(compute_collection_templates_params)), id=id) try: (rc, resp) = request(manager_url + "/fabric/compute-collection-fabric-templates/%s" % id, method='DELETE', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs) except Exception as err: module.fail_json(msg="Failed to delete fabric compute collection fabric template with id %s. Error[%s]." % (id, to_native(err))) wait_till_delete(id, module, manager_url, mgr_username, mgr_password, validate_certs) module.exit_json(changed=True, id=id, message="Compute collection fabric template id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update( display_name=dict(required=True, type='str'), subnets=dict(required=False, type='list'), urpf_mode=dict(required=False, type='str'), mac_address=dict(required=False, type='str'), linked_logical_switch_port_id=dict( required=False, type='dict', selected=dict(required=True, type='boolean'), service=dict(required=False, type='dict', ether_type=dict(required=True, type='int'), destination_ports=dict(required=False, type='list'), l4_protocol=dict(required=True, type='str'), source_ports=dict(required=False, type='list'), icmp_code=dict(required=False, type='int'), icmp_type=dict(required=False, type='int'), protocol=dict(required=True, type='str'), protocol_number=dict(required=True, type='int'), alg=dict(required=True, type='str'), resource_type=dict(required=True, type='str')), target_display_name=dict(required=False, type='str'), is_valid=dict(required=False, type='boolean'), target_id=dict(required=False, type='str'), target_type=dict(required=False, type='str')), admin_state=dict(required=False, type='str'), vpn_session_id=dict(required=False, type='str'), enable_netx=dict(required=False, type='boolean'), edge_cluster_member_index=dict(required=False, type='list'), mtu=dict(required=False, type='int'), linked_logical_router_port_id=dict( required=False, type='dict', selected=dict(required=True, type='boolean'), service=dict(required=False, type='dict', ether_type=dict(required=True, type='int'), destination_ports=dict(required=False, type='list'), l4_protocol=dict(required=True, type='str'), source_ports=dict(required=False, type='list'), icmp_code=dict(required=False, type='int'), icmp_type=dict(required=False, type='int'), protocol=dict(required=True, type='str'), protocol_number=dict(required=True, type='int'), alg=dict(required=True, type='str'), resource_type=dict(required=True, type='str')), target_display_name=dict(required=False, type='str'), is_valid=dict(required=False, type='boolean'), target_id=dict(required=False, type='str'), target_type=dict(required=False, type='str')), logical_router_id=dict(required=True, type='str'), service_bindings=dict(required=False, type='list'), resource_type=dict(required=True, type='str'), state=dict(reauired=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) logical_router_port_params = get_logical_router_port_params( module.params.copy()) state = module.params['state'] mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] display_name = module.params['display_name'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) logical_router_port_dict = get_lr_port_from_display_name( module, manager_url, mgr_username, mgr_password, validate_certs, display_name) logical_router_port_id, revision = None, None if logical_router_port_dict: logical_router_port_id = logical_router_port_dict['id'] revision = logical_router_port_dict['_revision'] if state == 'present': headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, logical_router_port_params) if not updated: # add the logical_router_port if module.check_mode: module.exit_json(changed=True, debug_out=str( json.dumps(logical_router_port_params)), id='12345') request_data = json.dumps(logical_router_port_params) try: if logical_router_port_id: module.exit_json( changed=False, id=logical_router_port_id, message= "Logical router port with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/logical-router-ports', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg= "Failed to add logical router port. Request body [%s]. Error[%s]." % (request_data, to_native(err))) time.sleep(5) module.exit_json( changed=True, id=resp["id"], body=str(resp), message="Logical router port with displayname %s created." % module.params['display_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str( json.dumps(logical_router_port_params)), id=logical_router_port_id) logical_router_port_params[ '_revision'] = revision # update current revision request_data = json.dumps(logical_router_port_params) id = logical_router_port_id try: (rc, resp) = request(manager_url + '/logical-router-ports/%s' % id, data=request_data, headers=headers, method='PUT', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg= "Failed to update logical router port with id %s. Request body [%s]. Error[%s]." % (id, request_data, to_native(err))) time.sleep(5) module.exit_json( changed=True, id=resp["id"], body=str(resp), message="logical router port with id %s updated." % id) elif state == 'absent': # delete the array id = logical_router_port_id if id is None: module.exit_json( changed=False, msg='No logical router port exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str( json.dumps(logical_router_port_params)), id=id) try: (rc, resp) = request(manager_url + "/logical-router-ports/%s" % id, method='DELETE', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs) except Exception as err: module.fail_json( msg= "Failed to delete logical router port with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="Logical router port with id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), description=dict(required=False, type='str'), resource_type=dict(required=False, choices=['TransportNode']), host_switch_spec=dict( required=False, type='dict', host_switches=dict(required=True, type='list'), resource_type=dict(required=True, type='str')), fabric_node_name=dict(required=True, type='str'), host_switches=dict(required=False, type='list'), transport_zone_endpoints=dict(required=False, type='list'), state=dict(reauired=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) transport_node_params = get_transport_node_params(module.params.copy()) state = module.params['state'] mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] display_name = module.params['display_name'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) transport_node_dict = get_tn_from_display_name(module, manager_url, mgr_username, mgr_password, validate_certs, display_name) transport_node_id, revision = None, None if transport_node_dict: transport_node_id = transport_node_dict['id'] revision = transport_node_dict['_revision'] if state == 'present': body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, transport_node_params) updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, body) headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' if not updated: # add the node if module.check_mode: module.exit_json(changed=True, debug_out=str( json.dumps(logical_switch_params)), id='12345') request_data = json.dumps(body) try: if not transport_node_id: transport_node_id = get_id_from_display_name( module, manager_url, mgr_username, mgr_password, validate_certs, '/transport-nodes', display_name, exit_if_not_found=False) if transport_node_id: module.exit_json( changed=False, id=transport_node_id, message= "Transport node with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/transport-nodes', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg= "Failed to add transport node. Request body [%s]. Error[%s]." % (request_data, to_native(err))) wait_till_create(resp['id'], module, manager_url, mgr_username, mgr_password, validate_certs) time.sleep(5) module.exit_json( changed=True, id=resp["id"], body=str(resp), message="Transport node with display name %s created." % module.params['display_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(body)), id=transport_node_id) body['_revision'] = revision # update current revision request_data = json.dumps(body) id = transport_node_id try: (rc, resp) = request(manager_url + '/transport-nodes/%s' % id, data=request_data, headers=headers, method='PUT', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg= "Failed to update transport node with id %s. Request body [%s]. Error[%s]." % (id, request_data, to_native(err))) time.sleep(5) module.exit_json( changed=True, id=resp["id"], body=str(resp), message="Transport node with node id %s updated." % id) elif state == 'absent': # delete the array id = transport_node_id if id is None: module.exit_json( changed=False, msg='No transport node exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(transport_node_params)), id=id) try: (rc, resp) = request(manager_url + "/transport-nodes/%s" % id, method='DELETE', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs) except Exception as err: module.fail_json( msg="Failed to delete transport node with id %s. Error[%s]." % (id, to_native(err))) wait_till_delete(id, module, manager_url, mgr_username, mgr_password, validate_certs) time.sleep(5) module.exit_json(changed=True, object_name=id, message="Transport node with node id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), subnets=dict(required=False, type='list'), tags=dict(required=False, type='str'), state=dict(reauired=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) ip_pool_params = get_ip_pool_params(module.params.copy()) state = module.params['state'] mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] display_name = module.params['display_name'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) pool_dict = get_ip_pool_from_display_name(module, manager_url, mgr_username, mgr_password, validate_certs, display_name) pool_id, revision = None, None if pool_dict: pool_id = pool_dict['id'] revision = pool_dict['_revision'] if state == 'present': headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, ip_pool_params) if not updated: # add the pool if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(ip_pool_params)), id='12345') request_data = json.dumps(ip_pool_params) try: if pool_id: module.exit_json( changed=False, id=pool_id, message="IP pool with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/pools/ip-pools', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg="Failed to add ip pool. Request body [%s]. Error[%s]." % (request_data, to_native(err))) time.sleep(5) module.exit_json(changed=True, id=resp["id"], body=str(resp), message="IP pool with display name %s created." % module.params['display_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(ip_pool_params)), id=pool_id) ip_pool_params['_revision'] = revision # update current revision request_data = json.dumps(ip_pool_params) id = pool_id try: (rc, resp) = request(manager_url + '/pools/ip-pools/%s' % id, data=request_data, headers=headers, method='PUT', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg= "Failed to update ip pool with id %s. Request body [%s]. Error[%s]." % (id, request_data, to_native(err))) time.sleep(5) module.exit_json(changed=True, id=resp["id"], body=str(resp), message="ip pool with pool id %s updated." % id) elif state == 'absent': # delete the array id = pool_id if id is None: module.exit_json(changed=False, msg='No ip pool exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(ip_pool_params)), id=id) try: (rc, resp) = request(manager_url + "/pools/ip-pools/%s" % id, method='DELETE', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs) except Exception as err: module.fail_json( msg="Failed to delete ip pool with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="ip pool with pool id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), credential=dict(required=False, type='dict', username=dict(required=False, type='str'), password=dict(required=False, type='str', no_log=True), thumbprint=dict(required=False, type='str', no_log=True), asymmetric_credential=dict(required=False, type='str'), credential_verifier=dict(required=False, type='str'), credential_key=dict(required=False, type='str', no_log=True), credential_type=dict(required=True, type='str')), origin_type=dict(required=True, type='str'), server=dict(required=True, type='str'), state=dict(reauired=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) fabric_compute_manager_params = get_fabric_compute_manager_params(module.params.copy()) state = module.params['state'] mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] display_name = module.params['display_name'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) if not fabric_compute_manager_params['credential'].__contains__('thumbprint'): fabric_compute_manager_params['credential']['thumbprint'] = get_thumb(module) compute_manager_dict = get_compute_manager_from_display_name (module, manager_url, mgr_username, mgr_password, validate_certs, display_name) compute_manager_id, revision = None, None if compute_manager_dict: compute_manager_id = compute_manager_dict['id'] revision = compute_manager_dict['_revision'] if state == 'present': headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, fabric_compute_manager_params) if not updated: # add the compute_manager request_data = json.dumps(fabric_compute_manager_params) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id='12345') try: if compute_manager_id: module.exit_json(changed=False, id=compute_manager_id, message="Compute manager with display_name %s already exist."% module.params['display_name']) (rc, resp) = request(manager_url+ '/fabric/compute-managers', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg="Failed to add compute_manager. Request body [%s]. Error[%s]." % (request_data, to_native(err))) wait_till_create(resp['id'], module, manager_url, mgr_username, mgr_password, validate_certs) module.exit_json(changed=True, id=resp["id"], body= str(resp), message="fabric compute manager with ip %s created." % module.params['server']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(fabric_compute_manager_params)), id=compute_manager_id) fabric_compute_manager_params['_revision'] = revision # update current revision request_data = json.dumps(fabric_compute_manager_params) id = compute_manager_id try: (rc, resp) = request(manager_url+ '/fabric/compute-managers/%s' % id, data=request_data, headers=headers, method='PUT', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg="Failed to update compute_manager with id %s. Request body [%s]. Error[%s]." % (id, request_data, to_native(err))) module.exit_json(changed=True, id=resp["id"], body= str(resp), message="fabric compute manager with compute manager id %s updated." % id) elif state == 'absent': # delete the array id = compute_manager_id if id is None: module.exit_json(changed=False, msg='No compute manager exist with display_name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(fabric_compute_manager_params)), id=id) try: (rc, resp) = request(manager_url + "/fabric/compute-managers/%s" % id, method='DELETE', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs) except Exception as err: module.fail_json(msg="Failed to delete fabric compute manager with id %s. Error[%s]." % (id, to_native(err))) wait_till_delete(id, module, manager_url, mgr_username, mgr_password, validate_certs) module.exit_json(changed=True, id=id, message="fabric compute manager with compute manager id %s deleted." % id)
if check_license_exist(module, manager_url, mgr_username, mgr_password, validate_certs): module.exit_json(changed=False, message="license with license key %s already exist."% module.params['license_key']) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id=module.params['license_key']) try: (rc, resp) = request(manager_url+ '/licenses', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg="Failed to add license. Request body [%s]. Error[%s]." % (request_data, to_native(err))) time.sleep(5) module.exit_json(changed=True, result=resp, message="license with license key %s created." % module.params['license_key']) elif state == 'absent': # delete the license key id = module.params['license_key'] if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id=id) try: (rc, resp) = request(manager_url+ '/licenses?action=delete', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg="Failed to delete license with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=license_key, message="license with license key %s deleted." % id) if __name__ == '__main__': main()
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), mtu=dict(required=False, type='int'), teaming=dict(required=True, type='dict', name=dict(required=True, type='str'), policy=dict(required=True, type='str'), standby_list=dict(required=False, type='list'), active_list=dict(required=True, type='list')), transport_vlan=dict(required=False, type='int'), named_teamings=dict(required=False, type='list'), lags=dict(required=False, type='list'), resource_type=dict( required=True, type='str', choices=['UplinkHostSwitchProfile']), state=dict(reauired=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) profile_params = get_profile_params(module.params.copy()) state = module.params['state'] mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] display_name = module.params['display_name'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) host_switch_profile_dict = get_uplink_profile_from_display_name( module, manager_url, mgr_username, mgr_password, validate_certs, display_name) host_switch_profile_id, revision = None, None if host_switch_profile_dict: host_switch_profile_id = host_switch_profile_dict['id'] revision = host_switch_profile_dict['_revision'] if state == 'present': headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, profile_params) if not updated: # add the block if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(profile_params)), id='12345') request_data = json.dumps(profile_params) try: if host_switch_profile_id: module.exit_json( changed=False, id=host_switch_profile_id, message= "Uplink profile with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/host-switch-profiles', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg= "Failed to add host profile. Request body [%s]. Error[%s]." % (request_data, to_native(err))) time.sleep(5) module.exit_json( changed=True, id=resp["id"], body=str(resp), message="host profile with display name %s created." % module.params['display_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(profile_params)), id=host_switch_profile_id) profile_params['_revision'] = revision # update current revision request_data = json.dumps(profile_params) id = host_switch_profile_id try: (rc, resp) = request(manager_url + '/host-switch-profiles/%s' % id, data=request_data, headers=headers, method='PUT', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json( msg= "Failed to update host profile with id %s. Request body [%s]. Error[%s]." % (id, request_data, to_native(err))) time.sleep(5) module.exit_json(changed=True, id=resp["id"], body=str(resp), message="host profile with id %s updated." % id) elif state == 'absent': # delete the array id = host_switch_profile_id if id is None: module.exit_json( changed=False, msg='No host switch profile exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(profile_params)), id=id) try: (rc, resp) = request(manager_url + "/host-switch-profiles/%s" % id, method='DELETE', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs) except Exception as err: module.fail_json( msg="Failed to delete host profile with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="host profile with id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), replication_mode=dict(required=False, type='str'), extra_configs=dict(required=False, type='list'), uplink_teaming_policy_name=dict(required=False, type='str'), transport_zone_name=dict(required=True, type='str'), ip_pool_name=dict(required=False, type='str'), vlan=dict(required=False, type='int'), mac_pool_id=dict(required=False, type='str'), vni=dict(required=False, type='int'), vlan_trunk_spec=dict(required=False, type='dict', vlan_ranges=dict(required=True, type='list')), admin_state=dict(required=True, type='str'), address_bindings=dict(required=False, type='list'), switching_profiles=dict(required=False, type='list'), lswitch_id=dict(required=False, type='str'), state=dict(reauired=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) logical_switch_params = get_logical_switch_params(module.params.copy()) state = module.params['state'] mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] display_name = module.params['display_name'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) changed = True lswitch_dict = get_lswitch_from_display_name (module, manager_url, mgr_username, mgr_password, validate_certs, display_name) lswitch_id, revision = None, None if lswitch_dict: lswitch_id = lswitch_dict['id'] revision = lswitch_dict['_revision'] if state == 'present': body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, logical_switch_params) updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, body) headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' if lswitch_id is None: # add the logical_switch if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(body)), id=lswitch_id) request_data = json.dumps(body) try: if lswitch_id: module.exit_json(changed=False, id=lswitch_id, message="Logical switch with display_name %s already exist."% module.params['display_name']) (rc, resp) = request(manager_url+ '/logical-switches', data=request_data, headers=headers, method='POST', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg="Failed to add logical switch. Request body [%s]. Error[%s]." % (request_data, to_native(err))) time.sleep(5) module.exit_json(changed=True, id=resp["id"], body= str(resp), message="Logical switch with display name %s created." % module.params['display_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(body)), id=lswitch_id) body['_revision'] = revision # update current revision request_data = json.dumps(body) id = lswitch_id try: (rc, resp) = request(manager_url+ '/logical-switches/%s' % id, data=request_data, headers=headers, method='PUT', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) except Exception as err: module.fail_json(msg="Failed to update logical switch with id %s. Request body [%s]. Error[%s]." % (id, request_data, to_native(err))) time.sleep(5) module.exit_json(changed=True, id=resp["id"], body= str(resp), message="logical switch with lswitch id %s updated." % id) elif state == 'absent': # delete the array id = lswitch_id if id is None: module.exit_json(changed=False, msg='No logical switch exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(logical_switch_params)), id=id) try: (rc, resp) = request(manager_url + "/logical-switches/%s" % id, method='DELETE', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs) except Exception as err: module.fail_json(msg="Failed to delete logical switch with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="Logical switch with zone id %s deleted." % id)