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 + '/reverse-proxy/node/health', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) if 'healthy' in resp.keys() and resp['healthy'] == True: module.exit_json(changed=changed, msg="NSX Manager is Up") else: raise Exception("NSX Manager is not healthy") 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. Timed out")
def main(): argument_spec = vmware_argument_spec() argument_spec.update(url=dict(type='str'), file=dict(type='str'), timeout=dict(type='int', required=False)) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_one_of=[('url', 'file')]) upgrade_params = get_upload_mub_params(module.params.copy()) mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] timeout = module.params['timeout'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' request_data = json.dumps(upgrade_params) node_ip_address = get_mgr_ip_upgrade_enabled(module, manager_url, mgr_username, mgr_password, headers, validate_certs) update_node_url = 'https://{}/api/v1'.format(node_ip_address) if timeout is not None: upload_mub(module, update_node_url, mgr_username, mgr_password, validate_certs, request_data, headers, node_ip_address, timeout) else: upload_mub(module, update_node_url, mgr_username, mgr_password, validate_certs, request_data, headers, node_ip_address)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(site_connection_info=dict(required=False, type='dict', no_log=True, username=dict(required=False, type='str'), password=dict(required=False, type='str'), thumbprint=dict(required=False, type='str'), fqdn=dict(required=True, type='str'))) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) local_manager_params = get_local_manager_params(module.params.copy()) mgr_hostname = module.params['hostname'] mgr_username = module.params['username'] mgr_password = module.params['password'] validate_certs = module.params['validate_certs'] manager_url = 'https://{}/global-manager/api/v1'.format(mgr_hostname) check_copmatibility_api_url = manager_url + '/global-infra/onboarding-check-compatibility' headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' request_data = json.dumps(local_manager_params['site_connection_info']) try: (rc, resp) = request(check_copmatibility_api_url, 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='Error accessing local manager. Error [%s]' % (to_native(err))) module.exit_json(changed=False, **resp)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(t0_gw=dict(required=True, type='str'), t1_gw=dict(required=True, type='str')) 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://{}/policy/api/v1'.format(mgr_hostname) t0_gateway = module.params['t0_gw'] t1_gateway = module.params['t1_gw'] try: changed = False current_config = get_t1_config(manager_url, mgr_username, mgr_password, validate_certs, t1_gateway) t1_state = check_config_state(current_config, t0_gateway) if t1_state == True: response = attach_t0(manager_url, mgr_username, mgr_password, validate_certs, t1_gateway, t0_gateway) changed = True module.exit_json(changed=changed) else: changed = False module.exit_json(changed=changed) except Exception as err: module.fail_json(msg='Error accessing NSX Manager. Error [%s]' % (to_native(err))) module.exit_json(changed=changed)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(node_name=dict(required=True, type='str')) 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_node_name = module.params['node_name'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) manager_node_id = get_id_from_display_name_results(module, manager_url, '/cluster/nodes/deployments', mgr_username, mgr_password, validate_certs, ['deployment_config','hostname'], ['vm_id'], manager_node_name) changed = False try: (rc, resp) = request(manager_url + '/cluster/nodes/%s/repo_sync/status' % manager_node_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 manager node repo sync ' 'status. Error [%s]' % (to_native(err))) module.exit_json(changed=changed, **resp)
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'] headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' mgr_hostname = get_upgrade_orchestrator_node(module, mgr_hostname, mgr_username, mgr_password, headers, validate_certs) manager_url = 'https://{}/api/v1'.format(mgr_hostname) # Accept the upgrade EULA if module.check_mode: module.exit_json(changed=False, debug_out='Upgrade EULA will be' ' accepted.', id=mgr_hostname) try: (rc, resp) = request(manager_url+ '/upgrade/eula/accept', 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 accept end user license' ' agreement. Error[%s].' % to_native(err)) time.sleep(5) module.exit_json(changed=True, result=resp, message='End user license agreement' ' is accepted.')
def main(): argument_spec = vmware_argument_spec() argument_spec.update(component_type=dict( required=True, type='str', choices=['host', 'edge', 'mp'])) 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'] if module.params['component_type'] is None: module.fail_json(msg='Error: parameter component_type not provided') else: component_type = module.params['component_type'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) changed = False try: (rc, resp) = request( manager_url + '/upgrade/plan/%s/settings' % component_type.upper(), 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 while retrieving bundle information. Error [%s]' % (to_native(err))) module.exit_json(changed=changed, **resp)
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) headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' # Synchronize the repository data between nsx managers if module.check_mode: module.exit_json(changed=False, debug_out='The repository data between NSX' ' managers will be synchronized.', id=mgr_hostname) try: (rc, resp) = request(manager_url+ '/cluster/node?action=repo_sync', 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 synchronize repositories of NSX ' 'managers. Error[%s].' % to_native(err)) time.sleep(5) module.exit_json(changed=True, result=resp, message='NSX Manager repositories' ' synchronization started.')
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 + '/transport-node-profiles', 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 node profiles. Error [%s]' % (to_native(err))) module.exit_json(changed=changed, **resp)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(timeout=dict(type='int', required=False), component_type=dict(required=True, choices=['mp', 'host', 'edge'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) 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'] timeout = module.params['timeout'] component_type= module.params['component_type'] headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' mgr_hostname = get_upgrade_orchestrator_node(module, mgr_hostname, mgr_username, mgr_password, headers, validate_certs) manager_url = 'https://{}/api/v1'.format(mgr_hostname) #if state == 'present': # Runs pre upgrade checks if module.check_mode: module.exit_json(changed=False, debug_out='Post upgrade checks will be executed.', id='Pre upgrade checks') try: (rc, resp) = request(manager_url + '/upgrade/%s?action=execute_post_upgrade_' 'checks' % component_type.upper(), 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 execute post upgrade checks. Error[%s]." % to_native(err)) try: if timeout is None: wait_for_post_upgrade_checks_to_execute(manager_url, '/upgrade/upgrade-unit-groups' '/aggregate-info', mgr_username, mgr_password, validate_certs, component_type) else: wait_for_post_upgrade_checks_to_execute(manager_url, '/upgrade/upgrade-unit-groups' '/aggregate-info', mgr_username, mgr_password, validate_certs, component_type, timeout) except Exception as err: module.fail_json(msg='Error while polling for execution of post upgrade' ' checks. Error [%s]' % to_native(err)) time.sleep(5) changed = True try: (rc, resp) = request(manager_url+ '/upgrade/upgrade-unit-groups/aggregate-info', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs) except Exception as err: module.fail_json(msg='Post upgrade checks were executed successfully but error' ' occured while retrieving the results. Error [%s]' % (to_native(err))) module.exit_json(changed=changed, message='Post upgrade checks are performed successfully:\n' '----------------------------\n' + str(resp))
def main(): argument_spec = vmware_argument_spec() argument_spec.update(component_type=dict(type='str', required=True, choices=['host', 'edge', 'mp']), parallel=dict(type='bool', required=False), pause_after_each_group=dict(type='bool', required=False), pause_on_error=dict(type='bool', required=False), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) upgrade_plan_params = clean_and_get_params(module.params.copy(), ['component_type']) 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'] component_type = module.params['component_type'] headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' mgr_hostname = get_upgrade_orchestrator_node(module, mgr_hostname, mgr_username, mgr_password, headers, validate_certs) manager_url = 'https://{}/api/v1'.format(mgr_hostname) if state == 'present': # update the default upgrade plan if module.check_mode: module.exit_json(changed=False, debug_out='Upgrade Plan will be modified.' ' parallel: %s, pause_after_each_group: %s, pause_on_error: %s' % (module.params['parallel'], module.params['pause_after_each_group'], module.params['pause_on_error']), id=module.params['component_type']) request_data = json.dumps(upgrade_plan_params) try: (rc, resp) = request(manager_url+ '/upgrade/plan/%s/settings' % component_type.upper(), 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 upgrade plan. Error[%s]." % to_native(err)) time.sleep(5) module.exit_json(changed=True, message="Upgrade plan is updated.") elif state == 'absent': # reset to default upgrade plan try: (rc, resp) = request(manager_url+ '/upgrade/plan?action=reset&' 'component_type=%s' % component_type.upper(), 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 while reseting the upgrade plan. Error[%s]." % to_native(err)) time.sleep(5) module.exit_json(changed=True, message="Upgrade plan is reset.")
def main(): argument_spec = vmware_argument_spec() argument_spec.update(id=dict(required=True, type='str')) 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'] id = module.params['id'] manager_url = 'https://{}/global-manager/api/v1'.format(mgr_hostname) global_manager_url = manager_url + '/global-infra/global-managers' switchover_api_url = 'https://{}/api/v1/sites/switchover-status'.format(mgr_hostname) existing_global_manager = get_global_manager_from_id(module, global_manager_url, mgr_username, mgr_password, validate_certs, id) global_manager_id, revision = None, None if existing_global_manager is None: module.fail_json(msg="Global_manager with id [%s] not found." % id) global_manager_id = existing_global_manager['id'] revision = existing_global_manager['_revision'] existing_global_manager["display_name"] if existing_global_manager["mode"] == "ACTIVE": module.exit_json(changed=False, id=global_manager_id, message="Global manager with id %s is already in ACTIVE mode." % existing_global_manager["id"]) else: headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' request_data_dict = existing_global_manager request_data_dict["mode"] = "ACTIVE" request_data = json.dumps(request_data_dict) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id=global_manager_id) try: (rc, resp) = request(global_manager_url + '/%s' % global_manager_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 set global_manager as active. Request body [%s]. Error[%s]." % ( request_data, to_native(err))) wait_till_switchover_complete(module, switchover_api_url, mgr_username, mgr_password, validate_certs) module.exit_json(changed=True, id=resp["id"], body=str(resp), message="Global manager with id %s was made active." % module.params[ 'id'])
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'] headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' mgr_hostname = get_upgrade_orchestrator_node(module, mgr_hostname, mgr_username, mgr_password, headers, validate_certs) manager_url = 'https://{}/api/v1'.format(mgr_hostname) # Upgrade UC if module.check_mode: module.exit_json(changed=False, debug_out='Upgrade Coordinator ' 'will be upgraded.', id=mgr_hostname) try: (rc, resp) = request(manager_url + '/upgrade?action=upgrade_uc', 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 upgrade UC. Error[%s].' % to_native(err)) time.sleep(5) try: wait_for_operation_to_execute(manager_url, '/upgrade/uc-upgrade-status', mgr_username, mgr_password, validate_certs, ['state'], ['SUCCESS'], ['FAILED']) except Exception as err: module.fail_json(msg='Error while upgrading UC. Error [%s]' % to_native(err)) module.exit_json(changed=True, result=resp, message='UC is upgraded' ' successfully.')
def main(): argument_spec = vmware_argument_spec() argument_spec.update(required_info=dict( required=True, type='str', choices=['acceptance', 'contents'])) 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'] required_info = module.params['required_info'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) if required_info == 'acceptance': try: (rc, resp) = request(manager_url + '/upgrade/eula/acceptance', 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 upgrade EULA acceptance ' 'status. Error [%s]' % (to_native(err))) module.exit_json(changed=False, **resp) elif required_info == 'contents': try: (rc, resp) = request(manager_url + '/upgrade/eula/content', 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 upgrade EULA contents ' 'status. Error [%s]' % (to_native(err))) module.exit_json(changed=False, **resp) else: module.fail_json(msg='Invalid value passed for required_info.')
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-manager/status', headers=dict(Accept='application/json'), url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs, ignore_errors=True) if "overall_status" in resp and resp["overall_status"] == "STABLE": module.exit_json(changed=changed, msg=" NSX manager is UP") else: time_diff = datetime.now() - current_time time.sleep(10) wait_time = time_diff.seconds + wait_time + 10 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. Timed out")
def main(): argument_spec = vmware_argument_spec() argument_spec.update( display_name=dict(required=True, type='str'), description=dict(required=False, type='str'), cluster_profile_bindings=dict(required=False, type='list'), members=dict(required=False, type='list'), # tranpost_node_name allocation_rules=dict(required=False, type='list'), deployment_type=dict(required=False, type='dict', EdgeDeploymentType=dict(required=False, type='str')), enable_inter_site_forwarding=dict(required=False, type='bool'), member_node_type=dict(required=False, type='dict', EdgeClusterNodeType=dict(required=False, type='str')), node_rtep_ips=dict(required=False, type='str'), tags=dict(required=False, type='list'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) edge_cluster_params = get_edge_cluster_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) edge_cluster_dict = get_edge_clusters_from_display_name( module, manager_url, mgr_username, mgr_password, validate_certs, display_name) edge_cluster_id, revision = None, None if edge_cluster_dict: edge_cluster_id = edge_cluster_dict['id'] revision = edge_cluster_dict['_revision'] if state == 'present': body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, edge_cluster_params) body = update_params_with_profile_id(module, manager_url, mgr_username, mgr_password, validate_certs, edge_cluster_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 edge_cluster request_data = json.dumps(body) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id='12345') try: if edge_cluster_id: module.exit_json( changed=False, id=edge_cluster_id, message= "Edge Cluster with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/edge-clusters', 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 edge cluster. Request body [%s]. Error[%s]." % (request_data, to_native(err))) module.exit_json( changed=True, id=resp["id"], body=str(resp), message="edge cluster with display name %s created." % module.params['display_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str( json.dumps(edge_cluster_params)), id=edge_cluster_id) body['_revision'] = revision # update current revision request_data = json.dumps(body) id = edge_cluster_id try: (rc, resp) = request(manager_url + '/edge-clusters/%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 edge cluster 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="Edge Cluster with edge cluster id %s updated." % id) elif state == 'absent': # delete the edge cluster id = edge_cluster_id if id is None: module.exit_json(changed=False, msg='No edge cluster exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(edge_cluster_params)), id=id) try: (rc, resp) = request(manager_url + "/edge-clusters/%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 edge cluster with id %s. Error[%s]." % (id, to_native(err))) module.exit_json( changed=True, id=id, message="edge cluster with edge cluster id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), cidr=dict(required=True, type='str'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) ip_block_params = get_ip_block_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) block_dict = get_ip_block_from_display_name (module, manager_url, mgr_username, mgr_password, validate_certs, display_name) block_id, revision = None, None if block_dict: block_id = block_dict['id'] revision = block_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_block_params) if not updated: # add the block if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(ip_block_params)), id='12345') request_data = json.dumps(ip_block_params) try: if block_id: module.exit_json(changed=False, id=block_id, message="IP block with display_name %s already exist."% module.params['display_name']) (rc, resp) = request(manager_url+ '/pools/ip-blocks', 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 block. 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 block 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_block_params)), id=block_id) ip_block_params['_revision'] = revision # update current revision request_data = json.dumps(ip_block_params) id = block_id try: (rc, resp) = request(manager_url+ '/pools/ip-blocks/%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 block 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 block with block id %s updated." % id) elif state == 'absent': # delete the array id = block_id if id is None: module.exit_json(changed=False, msg='No ip block exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(ip_block_params)), id=id) try: (rc, resp) = request(manager_url + "/pools/ip-blocks/%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 block with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="ip block with block id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(deployment_requests=dict(required=True, type='list'), node_id=dict(required=False, type='str'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_if=[['state', 'absent', ['node_id']]]) node_params = get_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'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' inject_vcenter_info(module, manager_url, mgr_username, mgr_password, validate_certs, node_params) update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, node_params) request_data = json.dumps(node_params) results = get_nodes(module, manager_url, mgr_username, mgr_password, validate_certs) is_node_exist, hostname = check_node_exist(results, module) if state == 'present': # add Manager Controller node if is_node_exist: module.exit_json( changed=False, message= "Controller-manager node with hostname %s already exist." % hostname) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data)) try: (rc, resp) = request(manager_url + '/cluster/nodes/deployments', 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 controller-manager node. Request body [%s]. Error[%s]." % (request_data, to_native(err))) for node in resp['results']: wait_till_create(node['vm_id'], module, manager_url, mgr_username, mgr_password, validate_certs) time.sleep(5) module.exit_json(changed=True, body=str(resp), message="Controller-manager node deployed.") elif state == 'absent': id = module.params['node_id'] if is_node_exist: # delete node if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data)) try: (rc, resp) = request( manager_url + '/cluster/nodes/deployments/%s?action=delete' % id, 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 controller-manager node with id %s. Error[%s]." % (id, to_native(err))) else: module.fail_json( msg="Controller-manager node with id %s does not exist." % id) wait_till_delete(id, module, manager_url, mgr_username, mgr_password, validate_certs) time.sleep(5) module.exit_json( changed=True, id=id, message="Controller-manager node with node id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(virtual_ip_address=dict(required=True, type='str'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) virtual_ip_params = get_virtual_ip_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' if state == 'present': # add virtual IP address if not virtual_ip_params.__contains__('virtual_ip_address'): module.fail_json(msg="Field virtual_ip_address is not provided") else: virtual_ip_address = virtual_ip_params['virtual_ip_address'] if not check_if_valid_ip(virtual_ip_address): module.fail_json(msg="Virtual IP provided is invalid.") if module.check_mode: module.exit_json( changed=False, debug_out="Cluster virtual IP would have been updated to %s" % module.params['virtual_ip_address'], id=module.params['virtual_ip_address']) try: (rc, resp) = request( manager_url + '/cluster/api-virtual-ip?action=set_virtual_ip&ip_address=%s' % virtual_ip_address, 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 virtual IP address. Error[%s]." % to_native(err)) time.sleep(5) module.exit_json( changed=True, result=resp, message="Virtual IP address is set with ip address: %s " % virtual_ip_address) elif state == 'absent': # delete virtual IP address is_virtual_ip_set = True virtual_ip_address = get_attribute_from_endpoint( module, manager_url, '/cluster/api-virtual-ip', mgr_username, mgr_password, validate_certs, 'ip_address') if virtual_ip_address is None or virtual_ip_address == '0.0.0.0': virtual_ip_address = "Virtual IP address is not set" is_virtual_ip_set = False if module.check_mode: if not is_virtual_ip_set: module.exit_json(changed=True, debug_out='Virtual IP address is not set', id=virtual_ip_address) else: module.exit_json( changed=True, debug_out= 'Virtual IP address is set to %s. Will be removed.' % virtual_ip_address, id=virtual_ip_address) try: (rc, resp) = request(manager_url + '/cluster/api-virtual-ip?action=clear_virtual_ip', 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 clear virtual IP address. Error[%s]." % to_native(err)) time.sleep(5) if is_virtual_ip_set: module.exit_json(changed=True, object_name=virtual_ip_address, message="Cleared cluster virtual IP address.") else: module.exit_json(changed=False, object_name="Virtual IP was not set before.", message="Cleared cluster virtual IP address.")
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(required=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'), transport_type=dict(required=True, type='str'), host_switch_mode=dict(required=False, type='str'), host_switch_id=dict(required=False, type='str'), host_switch_name=dict(required=False, type='str'), nested_nsx=dict(required=False, type='bool'), uplink_teaming_policy_names=dict(required=False, type='list'), transport_zone_profile_ids=dict(required=False, type='list'), is_default=dict(required=False, type='bool'), resource_type=dict(required=False, type='str'), description=dict(required=False, type='str'), tags=dict(required=False, type='list'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) transport_zone_params = get_transport_zone_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) zone_dict = get_tz_from_display_name(module, manager_url, mgr_username, mgr_password, validate_certs, display_name) zone_id, revision = None, None if zone_dict: zone_id = zone_dict['id'] revision = zone_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, transport_zone_params) if not updated: # add the node if module.check_mode: module.exit_json(changed=True, debug_out=str( json.dumps(transport_zone_params)), id='12345') request_data = json.dumps(transport_zone_params) try: if zone_id: module.exit_json( changed=False, id=zone_id, message= "Transport zone with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/transport-zones', 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 zone. Request body [%s]. Error[%s]." % (request_data, to_native(err))) #dict_resp = json.loads(resp) time.sleep(5) module.exit_json( changed=True, id=resp["id"], body=str(resp), message="Transport zone with display name %s created. " % (module.params['display_name'])) else: if module.check_mode: module.exit_json(changed=True, debug_out=str( json.dumps(transport_zone_params)), id=zone_id) transport_zone_params[ '_revision'] = revision # update current revision request_data = json.dumps(transport_zone_params) id = zone_id try: (rc, resp) = request(manager_url + '/transport-zones/%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 zone 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 zone with zone id %s updated." % id) elif state == 'absent': # delete the array id = zone_id if id is None: module.exit_json( changed=False, msg='No transport zone exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(transport_zone_params)), id=id) try: (rc, resp) = request(manager_url + "/transport-zones/%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 zone with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="Transport zone with zone id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(ip_address=dict(required=True, type='str', no_log=False), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) cert_params = get_cluster_vip_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(cert_params) if state == 'present': # set the vip if check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, cert_params): module.exit_json(changed=False, msg="VIP with IP %s already exist." % module.params['ip_address']) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id=module.params['ip_address']) try: (rc, resp) = request( manager_url + '/cluster/api-virtual-ip?action=set_virtual_ip&ip_address=' + module.params['ip_address'], 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 set cluster VIP. Request body [%s]. Error[%s]." % (request_data, to_native(err))) time.sleep(5) module.exit_json(changed=True, result=resp, message="Manger cluster VIP with IP %s created." % module.params['ip_address']) elif state == 'absent': # delete the vip id = module.params['ip_address'] if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id=id) try: (rc, resp) = request(manager_url + '/cluster/api-virtual-ip?action=clear_virtual_ip', 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 manager cluster VIP %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="VIP with IP %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'), nsservice_element=dict( required=True, type='dict', alg=dict(required=False, type='str', choices=[ 'ORACLE_TNS', 'FTP', 'SUN_RPC_TCP', 'SUN_RPC_UDP', 'MS_RPC_TCP', 'MS_RPC_UDP', 'NBNS_BROADCAST', 'NBDG_BROADCAST', 'TFTP' ]), destination_ports=dict(required=False, type='list'), icmp_code=dict(required=False, type='int'), icmp_type=dict(required=False, type='int'), l4_protocol=dict(required=False, type='str'), protocol=dict(required=False, type='str', choices=['ICMPv4', 'ICMPv6']), protocol_number=dict(required=False, type='int'), resource_type=dict(required=True, type='str', choices=[ 'ALGTypeNSService', 'IPProtocolNSService', 'L4PortSetNSService', 'ICMPTypeNSService', 'IGMPTypeNSService' ]), source_ports=dict(required=False, type='list')), resource_type=dict(required=False, type='str', default='NSService'), state=dict(required=True, choices=['present', 'absent']), tags=dict(required=False, type='list', tag=dict(required=False, type='str'), scope=dict(required=False, type='str'))) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) service_params = get_service_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) service_dict = get_service_from_display_name(module, manager_url, mgr_username, mgr_password, validate_certs, display_name) service_id, revision = None, None if service_dict: service_id = service_dict['id'] revision = service_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, service_params) if not updated: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(service_params)), id='12345') request_data = json.dumps(service_params) try: if service_id: module.exit_json( changed=False, id=service_id, message="Service with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/ns-services', 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 service. 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="Service with display name %s created." % module.params['display_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(service_params)), id=service_id) service_params['_revision'] = revision # update current revision request_data = json.dumps(service_params) id = service_id try: (rc, resp) = request(manager_url + '/ns-services/%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 service 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="Service with id %s updated." % id) elif state == 'absent': # delete the array id = service_id if id is None: module.exit_json(changed=False, msg='No service exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(service_params)), id=id) try: (rc, resp) = request(manager_url + "/ns-services/%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 service with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="Service with id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), host_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)), applied_tos=dict(required=False, type='list', default=list([]), target_display_name=dict(required=True, type='str'), # Will insert target_id a runtime target_type=dict(required=True, type='str', choices=['LogicalPort', 'LogicalSwitch', 'NSGroup', 'LogicalRouter'])), description=dict(required=False, type='str'), rules=dict(required=True, type='list', display_name=dict(required=True, type='str'), # API does not enforce, but added to all later management. description=dict(required=False, type='str'), action=dict(required=True, type='str', choices=['ALLOW', 'DROP', 'REJECT', 'REDIRECT', 'DO_NOT_REDIRECT']), applied_tos=dict(required=False, type='list', default=[], target_display_name=dict(required=True, type='str'), # Will insert target_id a runtime target_type=dict(required=True, type='str', choices=['LogicalPort', 'LogicalSwitch', 'NSGroup'])), context_profiles=dict(required=False, type='list', default=[], target_display_name=dict(required=True, type='str'), # Will insert target_id a runtime target_type=dict(required=True, type='str', choices=['NSProfile'])), destinations=dict(required=False, type='list', default=[], target_display_name=dict(required=True, type='str'), # Will insert target_id a runtime target_type=dict(required=True, type='str', choices=['IPSet', 'LogicalPort', 'LogicalSwitch', 'NSGroup'])), direction=dict(required=False, type='str', choices=['IN', 'OUT', 'IN_OUT']), disabled=dict(required=False, type='bool'), ip_protocol=dict(required=False, type='str', choices=['IPV4', 'IPV6', 'IPV4_IPV6']), logged=dict(required=False, type='bool'), notes=dict(required=False, type='str'), resource_type=dict(required=False, type='str', choices=['FirewallRule']), rule_tag=dict(required=False, type='str'), services=dict(required=False, type='list', default=[], target_display_name=dict(required=False, type='str'), # Will insert target_id a runtime target_type=dict(required=False, type='str', choices=['NSService', 'NSServiceGroup']), service=dict(required=False, type='list', default=[], alg=dict(required=False, type='str', choices=['ORACLE_TNS', 'FTP', 'SUN_RPC_TCP', 'SUN_RPC_UDP', 'MS_RPC_TCP', 'MS_RPC_UDP', 'NBNS_BROADCAST', 'NBDG_BROADCAST', 'TFTP']), destination_ports=dict(required=False, type='list'), icmp_code=dict(required=False, type='int'), icmp_type=dict(required=False, type='int'), l4_protocol=dict(required=False, type='str'), protocol=dict(required=False, type='str', choices=['ICMPv4', 'ICMPv6']), protocol_number=dict(required=False, type='int'), resource_type=dict(required=True, type='str', choices=['ALGTypeNSService', 'IPProtocolNSService', 'L4PortSetNSService', 'ICMPTypeNSService', 'IGMPTypeNSService']), source_ports=dict(required=False, type='list')),), sources=dict(required=False, type='list', default=[], target_display_name=dict(required=True, type='str'), # Will insert target_id a runtime target_type=dict(required=True, type='str', choices=['IPSet', 'LogicalPort', 'LogicalSwitch', 'NSGroup'])),), resource_type=dict(required=False, choices=['FirewallSectionRuleList'], default='FirewallSectionRuleList'), section_placement=dict(required=False, type='dict', operation=dict(required=True, type='str', choices=['insert_top', 'insert_bottom', 'insert_after', 'insert_before']), display_name=dict(required=True, type='str')), section_type=dict(required=False, choices=['LAYER2', 'LAYER3'], default='LAYER3'), state=dict(required=True, choices=['present', 'absent']), stateful=dict(required=True, type='bool'), tags=dict(required=False, type='list', tag=dict(required=False, type='str'), scope=dict(required=False, type='str')), modify_placement=dict(required=False, type='bool', default=False)) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) dfw_section_params = get_dfw_section_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) section_placement = dfw_section_params.pop('section_placement', None) modify_placement = dfw_section_params.pop('modify_placement', None) check_rules_have_unique_names(module, dfw_section_params) insert_lists_if_missing(dfw_section_params, ['applied_tos', 'tags']) section_dict = get_dfw_section_from_display_name(module, manager_url, mgr_username, mgr_password, validate_certs, display_name) section_id, revision = None, None if section_dict: section_id = section_dict['id'] revision = section_dict['_revision'] if state == 'present': headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' add_backwards_compatibilty(module, manager_url, mgr_username, mgr_password, validate_certs, dfw_section_params) existing_config_lookup, duplicated_objects = collect_all_existing_config(module, manager_url, mgr_username, mgr_password, validate_certs) update_param_list_with_ids(module, dfw_section_params['applied_tos'], existing_config_lookup, duplicated_objects, 'DFW section ' + dfw_section_params['display_name'], 'applied_tos') update_rules_list_with_ids(module, dfw_section_params['rules'], existing_config_lookup, ['applied_tos', 'context_profiles', 'destinations', 'services', 'sources'], duplicated_objects) updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, dfw_section_params) place_updated = check_if_section_moved(module, manager_url, mgr_username, mgr_password, validate_certs, section_placement, existing_config_lookup, dfw_section_params, modify_placement) query_params = generate_query_params(module, dfw_section_params, manager_url, mgr_username, mgr_password, validate_certs, section_placement, updated, existing_config_lookup, duplicated_objects, modify_placement, place_updated) updated = updated or place_updated if not dfw_section_params['rules']: dfw_section_params.pop('rules', None) if not updated: request_data = json.dumps(dfw_section_params) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id='12345') try: if section_id: module.exit_json(changed=False, id=section_id, message="Firewall Section with display_name [%s] already exist and has not changed." % module.params['display_name']) (rc, resp) = request(manager_url+ '/firewall/sections%s' % query_params, 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 node. Request body [%s]. Error[%s]." % (request_data, to_native(err))) #TODO consult VMWare NSBU on invokation rates and build dynamic delay between calls. time.sleep(5) module.exit_json(changed=True, id=resp["id"], body= str(resp), message="Firewall Section with display_name %s created succcessfully." % module.params['display_name']) else: id = section_id if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(dfw_section_params)), id=id) dfw_section_params['_revision'] = revision # update current revision request_data = json.dumps(dfw_section_params) try: (rc, resp) = request(manager_url+ '/firewall/sections/%s%s' % (id, query_params), 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 update Section with display_name %s. Request body [%s]. Error[%s]." % (module.params['display_name'], request_data, to_native(err))) time.sleep(5) module.exit_json(changed=True, id=resp["id"], body= str(resp), message="Firewall Section with display_name [%s] updated with params [%s]." % (module.params['display_name'], query_params)) elif state == 'absent': id = section_id if id is None: module.exit_json(changed=False, msg='No Firewall Section exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(dfw_section_params)), id=id) try: (rc, resp) = request(manager_url + "/firewall/sections/%s?cascade=true" % 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 Firewall Section with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, id=id, message="NG Group with node id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(license_key=dict(required=True, type='str', no_log=True), state=dict(required=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']) 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/' + id, method='DELETE', 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=id, message="license with license key %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), set_as_oidc_provider=dict(required=False, type='bool', default=True), credential=dict(required=False, type='dict', no_log=True, username=dict(required=False, type='str'), password=dict(required=False, type='str'), thumbprint=dict(required=False, type='str'), 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(required=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)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(client_id=dict(required=True, type='str'), client_secret=dict(required=False, type='str'), host_name=dict(required=True, type='str'), lb_enable=dict(required=False, type='bool'), node_host_name=dict(required=True, type='str'), thumbprint=dict(required=True, type='str'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) vidm_params = get_vidm_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'] client_id = module.params['client_id'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) vidm_api_url = manager_url + '/node/aaa/providers/vidm' existing_vidm = get_vidm_from_client_id(module, vidm_api_url, mgr_username, mgr_password, validate_certs, vidm_params['client_id']) headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' if state == 'present': vidm_params["vidm_enable"] = True if existing_vidm is not None: updated = check_for_update(existing_vidm, vidm_params) if not updated: module.exit_json(changed=False, id=vidm_params['client_id'], message="vIDM with id %s is already enabled." % vidm_params['client_id']) # vIDM not present or update. So call PUT API which is same for add and update vIDM request_data = json.dumps(vidm_params) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id='12345') try: (rc, resp) = request(vidm_api_url, 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 register vIDM. Request body [%s]. Error[%s]." % (request_data, to_native(err))) wait_till_create(module, vidm_api_url, mgr_username, mgr_password, validate_certs) module.exit_json(changed=True, id=resp["client_id"], body=str(resp), message="vIDM with client id %s registered." % resp["client_id"]) elif state == 'absent': if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(vidm_params)), id=vidm_params['client_id']) if existing_vidm is None: module.exit_json(changed=False, id=vidm_params['client_id'], message="vIDM with client id %s was not registered." % vidm_params['client_id']) # vIDM with given client_id is registered so unregister it vidm_params['vidm_enable'] = False request_data = json.dumps(vidm_params) try: (rc, resp) = request(vidm_api_url, 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 un-register vIDM. Request body [%s]. Error[%s]." % (request_data, to_native(err))) wait_till_delete(module, vidm_api_url, mgr_username, mgr_password, validate_certs) module.exit_json(changed=True, id=vidm_params['client_id'], body=str(resp), message="vIDM with id %s is unregistered." % vidm_params['client_id'])
def main(): argument_spec = vmware_argument_spec() argument_spec.update(timeout=dict(type='int', required=False), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) upgrade_prechecks_params = clean_and_get_params(module.params.copy(), ['timeout']) 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'] timeout = module.params['timeout'] headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' mgr_hostname = get_upgrade_orchestrator_node(module, mgr_hostname, mgr_username, mgr_password, headers, validate_certs) manager_url = 'https://{}/api/v1'.format(mgr_hostname) if state == 'present': # Runs pre upgrade checks if module.check_mode: module.exit_json(changed=False, debug_out='Pre upgrade checks will be executed.', id='Pre upgrade checks') request_data = json.dumps(upgrade_prechecks_params) try: (rc, resp) = request(manager_url + '/upgrade?action=execute_pre_upgrade_checks', 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 execute pre upgrade checks. Error[%s]." % to_native(err)) try: if timeout is None: wait_for_pre_upgrade_checks_to_execute(manager_url, '/upgrade/status-summary', mgr_username, mgr_password, validate_certs) else: wait_for_pre_upgrade_checks_to_execute(manager_url, '/upgrade/status-summary', mgr_username, mgr_password, validate_certs, timeout) except Exception as err: module.fail_json(msg='Error while polling for execution of pre upgrade' ' checks. Error [%s]' % to_native(err)) time.sleep(5) changed = False try: (rc, resp) = request(manager_url+ '/upgrade/pre-upgrade-checks?format=csv', url_username=mgr_username, url_password=mgr_password, validate_certs=validate_certs) except Exception as err: module.fail_json(msg='Pre upgrade checks were executed successfully but error' ' occured while retrieving the results. Error [%s]' % (to_native(err))) module.exit_json(changed=changed, message='Pre upgrade checks are performed successfully:\n' '----------------------------\n' + str(resp)) elif state == 'absent': # Aborts pre upgrade checks try: (rc, resp) = request(manager_url + '/upgrade?action=abort_pre_upgrade_checks', 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 abort running pre upgrade checks. Error[%s]." % to_native(err)) time.sleep(5) module.exit_json(changed=True, message="Upgrade prechecks are aborted.")
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), transport_vlan=dict(required=False, type='int'), description=dict(required=False, type='str'), enabled=dict(required=False, type='boolean'), host_infra_traffic_res=dict(required=False, type='list'), overlay_encap=dict(required=False, type='str'), named_teamings=dict(required=False, type='list'), mtu=dict(required=False, type='int'), required_capabilities=dict(required=False, type='list'), send_enabled=dict(required=False, type='boolean'), extra_configs=dict(required=False, type='list'), teaming=dict(required=True, type='dict', policy=dict(required=True, type='str'), standby_list=dict(required=False, type='list'), active_list=dict(required=True, type='list')), lags=dict(required=False, type='list'), resource_type=dict( required=True, type='str', choices=['UplinkHostSwitchProfile']), state=dict(required=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'), host_switch_spec=dict(required=False, type='dict', host_switches=dict(required=True, type='list'), resource_type=dict(required=True, type='str')), node_deployment_info=dict( required=False, type='dict', discovered_node_id=dict(required=False, type='str'), deployment_config=dict( required=False, type='dict', node_user_settings=dict(required=True, type='dict', cli_username=dict(required=False, type='str'), audit_username=dict(required=False, type='str'), root_password=dict(required=False, type='str'), cli_password=dict(required=False, type='str'), audit_password=dict(required=False, type='str')), vm_deployment_config=dict( required=True, type='dict', data_network_ids=dict(required=True, type='list'), dns_servers=dict(required=False, type='list'), ntp_servers=dict(required=False, type='list'), management_network_id=dict(required=True, type='str'), enable_ssh=dict(required=False, type='boolean'), allow_ssh_root_login=dict(required=False, type='boolean'), placement_type=dict(required=True, type='str'), compute_id=dict(required=True, type='str'), search_domains=dict(required=False, type='list'), vc_id=dict(required=True, type='str'), storage_id=dict(required=True, type='str'), default_gateway_addresses=dict(required=False, type='list'), management_port_subnets=dict(required=False, type='list'), host_id=dict(required=False, type='str'), hostname=dict(required=True, type='str')), form_factor=dict(required=False, type='str')), discovered_ip_addresses=dict(required=False, type='list'), ip_addresses=dict(required=False, type='list'), fqdn=dict(required=False, type='str'), os_version=dict(required=False, type='str'), managed_by_server=dict(required=False, type='str'), host_credential=dict(required=False, type='dict', username=dict(required=False, type='str'), password=dict(required=False, type='str'), thumbprint=dict(required=False, type='str')), allocation_list=dict(required=False, type='list'), os_type=dict(required=True, type='str'), external_id=dict(required=False, type='str'), resource_type=dict(required=True, type='str'), deployment_type=dict(required=False, type='str')), maintenance_mode=dict(required=False, type='str'), transport_zone_endpoints=dict(required=False, type='list'), node_id=dict(required=False, type='str'), state=dict(required=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['node_id'], module, manager_url, mgr_username, mgr_password, validate_certs) time.sleep(5) module.exit_json( changed=True, id=resp["node_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["node_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)