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 + '/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))) module.exit_json(changed=changed, **resp)
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(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() 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() 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() 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( path=dict(type='str', required=True), method=dict(type='str', choices=['get', 'post', 'put', 'patch', 'delete'], default='get'), src=dict(type='str'), content=dict(type='raw'), ) module = AnsibleModule(argument_spec, supports_check_mode=True) vmware_nsx_rest = VMwareNSXTRest(module) vmware_nsx_rest.execute()
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(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() 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() argument_spec.update(display_name=dict(required=True, type='str'), description=dict(required=False, type='str'), host_switch_spec=dict( required=False, type='dict', host_switches=dict(required=True, type='list'), resource_type=dict(required=True, type='str')), resource_type=dict(required=True, type='str'), transport_zone_endpoints=dict(required=False, type='list'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) transport_node_profile_params = get_transport_node_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) transport_node_profile_dict = get_tnp_from_display_name( module, manager_url, mgr_username, mgr_password, validate_certs, display_name) transport_node_profile_id, revision = None, None if transport_node_profile_dict: transport_node_profile_id = transport_node_profile_dict['id'] revision = transport_node_profile_dict['_revision'] if state == 'present': body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, transport_node_profile_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_profile_id: transport_node_profile_id = get_id_from_display_name( module, manager_url, mgr_username, mgr_password, validate_certs, '/transport-node-profiles', display_name, exit_if_not_found=False) if transport_node_profile_id: module.exit_json( changed=False, id=transport_node_profile_id, message= "Transport node profile with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/transport-node-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 transport node 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="transport node 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(body)), id=transport_node_profile_id) body['_revision'] = revision # update current revision request_data = json.dumps(body) id = transport_node_profile_id try: (rc, resp) = request(manager_url + '/transport-node-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 transport node 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="transport node profile with node id %s updated." % id) elif state == 'absent': # delete the array id = transport_node_profile_id if id is None: module.exit_json( changed=False, msg='No transport node profile exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str( json.dumps(transport_node_profile_params)), id=id) try: (rc, resp) = request(manager_url + "/transport-node-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 transport node profile with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json( changed=True, object_name=id, message="transport node profile with node 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'), edge_cluster_member_indices=dict(required=False, type='list'), allocation_profile=dict( required=False, type='dict', allocation_pool=dict(required=False, type='dict', allocation_size=dict(required=True, type='str'), allocation_pool_type=dict(required=True, type='str')), enable_standby_relocation=dict(required=False, type='boolean')), failover_mode=dict(required=False, type='str'), advanced_config=dict( required=False, type='dict', transport_zone_name=dict(required=False, type='str'), internal_transit_networks=dict(required=False, type='list'), internal_routing_network=dict(required=False, type='str'), ha_vip_configs=dict( required=False, type='list', enabled=dict(required=False, type='boolean'), ha_vip_subnets=dict(required=False, type='list', active_vip_addresses=dict(required=False, type='list'), prefix_length=dict(required=False, type='str')), redundant_uplink_port_ids=dict(required=False, type='list'), redundant_uplink_port_names=dict(required=False, type='list')), external_transit_networks=dict(required=False, type='list')), router_type=dict(required=True, type='str'), preferred_edge_cluster_member_index=dict(required=False, type='int'), high_availability_mode=dict(required=False, type='str'), edge_cluster_name=dict(required=False, type='str'), tags=dict(required=False, type='list'), ipv6_profiles=dict(required=False, type='dict', dad_profile_name=dict(required=False, type='str'), ndra_profile_name=dict(required=False, type='str')), resource_type=dict(required=False, type='str', choices=['LogicalRouter']), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) logical_router_params = get_logical_router_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_dict = get_lr_from_display_name(module, manager_url, mgr_username, mgr_password, validate_certs, display_name) logical_router_id, revision = None, None if logical_router_dict: logical_router_id = logical_router_dict['id'] revision = logical_router_dict['_revision'] if state == 'present': body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, logical_router_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 router if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(body)), id='12345') request_data = json.dumps(body) try: if logical_router_id: module.exit_json( changed=False, id=logical_router_id, message= "Logical router with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/logical-routers', 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. 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 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=logical_router_id) body['_revision'] = revision # update current revision request_data = json.dumps(body) id = logical_router_id try: (rc, resp) = request(manager_url + '/logical-routers/%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 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 with id %s updated." % id) elif state == 'absent': # delete the array id = logical_router_id if id is None: module.exit_json( changed=False, msg='No logical router exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(logical_router_params)), id=id) try: (rc, resp) = request(manager_url + "/logical-routers/%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 with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="logical router with 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='list'), description=dict(required=False, type='str'), ip_release_delay=dict(required=False, type='int'), state=dict(required=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'), logical_switch_name=dict(required=True, type='str'), init_state=dict(required=False, type='str'), switching_profiles=dict(required=False, type='list'), attachment=dict(required=False, type='dict', attachment_type=dict(required=False, type='str'), context=dict(required=False, type='dict', tunnel_id=dict(required=True, type='int'), vif_type=dict(required=True, type='str'), parent_vif_id=dict(required=False, type='str'), traffic_tag=dict(required=False, type='int'), app_id=dict(required=False, type='str'), allocate_addresses=dict(required=False, type='str'), resource_type=dict(required=True, type='str'), transport_node_name=dict(required=False, type='str')), id=dict(required=True, type='str')), admin_state=dict(required=True, type='str'), extra_configs=dict(required=False, type='list'), address_bindings=dict(required=False, type='list'), ignore_address_bindings=dict(required=False, type='list'), 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) logical_port_params = get_logical_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) lport_dict = get_logical_port_from_display_name (module, manager_url, mgr_username, mgr_password, validate_certs, display_name) lport_id, revision = None, None if lport_dict: lport_id = lport_dict['id'] revision = lport_dict['_revision'] if state == 'present': body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, logical_port_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 logical_port if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(body)), id='12345') request_data = json.dumps(body) try: if lport_id: module.exit_json(changed=False, id=lport_id, message="Logical port with display_name %s already exist"% module.params['display_name']) (rc, resp) = request(manager_url+ '/logical-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 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 port with displayname %s created." % module.params['display_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(body)), id=lport_id) body['_revision'] = revision # update current revision request_data = json.dumps(body) id = lport_id try: (rc, resp) = request(manager_url+ '/logical-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 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 port with id %s updated." % id) elif state == 'absent': # delete the array id = lport_id if id is None: module.exit_json(changed=False, msg='No logical port exist with display name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(logical_port_params)), id=id) try: (rc, resp) = request(manager_url + "/logical-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 port with id %s. Error[%s]." % (id, to_native(err))) time.sleep(5) module.exit_json(changed=True, object_name=id, message="Logical port with id %s deleted." % 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(module, manager_url, '/upgrade/status-summary', mgr_username, mgr_password, validate_certs) else: wait_for_pre_upgrade_checks_to_execute(module, 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/failures', 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))) # Fail module in case any pre upgrade check fails prechecks_failure = False if 'results' in resp: for result in resp['results']: if 'type' in result and result['type'] == 'FAILURE': prechecks_failure = True if prechecks_failure: module.fail_json(msg='Pre upgrade checks are performed successsfully. Found errors. ' 'Thus, you cannot proceed. To get full report run upgrade groups ' 'facts module. Precheck results: %s' % str(resp)) module.exit_json(changed=changed, message='Pre upgrade checks are performed successfully:' ' Failures are listed. To get full report run upgrade groups ' 'facts module.' + 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'), tags=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(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(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(description=dict(type='str', required=False), display_name=dict(type='str', required=True), enabled=dict(type='bool', required=False, default=True), extended_configuration=dict(type='list', required=False), parallel=dict(type='bool', required=False, default=True), resource_type=dict(type='str', required=False), tags=dict(type='list', required=False), type=dict(type='str', required=True), upgrade_unit_count=dict(type='int', required=False), upgrade_units=dict(type='list', required=False), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) upgrade_group_params = clean_and_get_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'] 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_group_params = update_group_parameters(module, manager_url, mgr_username, mgr_password, validate_certs, upgrade_group_params) upgrade_unit_group_id = get_id_from_display_name_results( module, manager_url, '/upgrade/upgrade-unit-groups', mgr_username, mgr_password, validate_certs, ['display_name'], ['id'], upgrade_group_params['display_name'], False) if state == 'present': # create a new upgrade group or modify the existing one if module.check_mode: module.exit_json( changed=False, debug_out='A new upgrade unit will be created with' ' name: %s' % module.params['display_name']) request_data = json.dumps(upgrade_group_params) if upgrade_unit_group_id is None: try: (rc, resp) = request(manager_url + '/upgrade/upgrade-unit-groups', 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 upgrade group. Error[%s]." % to_native(err)) time.sleep(5) module.exit_json(changed=True, message="Upgrade group is added successfully.") else: try: (rc, resp) = request(manager_url + '/upgrade/upgrade-unit-' 'groups/%s' % upgrade_unit_group_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 modify upgrade group. Error[%s]." % to_native(err)) time.sleep(5) module.exit_json(changed=True, message='Upgrade group with group id ' '%s is updated.' % upgrade_unit_group_id) elif state == 'absent': # remove an existing upgrade group try: (rc, resp) = request(manager_url + '/upgrade/upgrade-unit-groups' '/%s' % upgrade_unit_group_id, data='', headers=headers, 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 while deleting the upgrade' ' group. Error[%s].' % to_native(err)) time.sleep(5) module.exit_json(changed=True, message='Upgrade group with group id ' '%s is deleted.' % upgrade_unit_group_id)
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(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'), 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'), description=dict(required=False, type='str'), server=dict(required=True, type='str'), set_as_oidc_provider=dict(required=False, type='bool'), 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(display_name=dict(required=True, type='str'), description=dict(required=False, type='str'), resource_type=dict(required=True, type='str'), bfd_allowed_hops=dict(required=False, type='int'), bfd_declare_dead_multiple=dict(required=False, type='int'), bfd_probe_interval=dict(required=False, type='int'), standby_relocation_config=dict( required=False, type=dict, standby_relocation_threshold=dict(required=False, type='int')), tags=dict(required=False, type='list'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) cluster_profile_params = get_cluster_profiles_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) cluster_profiles_dict = get_cluster_profiles_from_display_name( module, manager_url, mgr_username, mgr_password, validate_certs, display_name) cluster_profile_id, revision = None, None if cluster_profiles_dict: cluster_profile_id = cluster_profiles_dict['id'] revision = cluster_profiles_dict['_revision'] if state == 'present': body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, cluster_profile_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 cluster_profile_id: module.exit_json( changed=False, id=cluster_profile_id, message= "Cluster profile with display_name %s already exist." % module.params['display_name']) (rc, resp) = request(manager_url + '/cluster-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 cluster profile. Request body [%s]. Error[%s]." % (request_data, to_native(err))) module.exit_json( changed=True, id=resp["id"], body=str(resp), message="Cluster 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(edge_cluster_params)), id=cluster_profile_id) body['_revision'] = revision # update current revision request_data = json.dumps(body) id = cluster_profile_id try: (rc, resp) = request(manager_url + '/cluster-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 cluster profile 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="Cluster profile with id %s updated." % id) elif state == 'absent': # delete the edge cluster id = cluster_profile_id if id is None: module.exit_json( changed=False, msg='No cluster profile 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 + "/cluster-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 cluster profile with id %s. Error[%s]." % (id, to_native(err))) module.exit_json(changed=True, id=id, message="Cluster profile with id %s deleted." % id)
def main(): argument_spec = vmware_argument_spec() argument_spec.update(paused_upgrade=dict(type='bool', required=True)) 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'] paused_upgrade = module.params['paused_upgrade'] 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 module.check_mode: if paused_upgrade: module.exit_json(changed=False, debug_out='NSX-T will upgrade with pauses.') else: module.exit_json(changed=False, debug_out='NSX-T will upgrade without pauses.') # If paused_upgrade is not true i.e auto mode if not paused_upgrade: while True: upgrade_status = get_upgrade_status(module, manager_url, mgr_username, mgr_password, validate_certs) if upgrade_status == 'NOT_STARTED': try: (rc, resp) = request(manager_url+ '/upgrade/plan?action=start', 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 upgrading. Error[%s]." % to_native(err)) else: try: (rc, resp) = request(manager_url+ '/upgrade/plan?action=continue', 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 upgrading. Error[%s]." % to_native(err)) time.sleep(10) while True: try: can_continue, is_failed = check_continuity(module, manager_url, mgr_username, mgr_password, validate_certs) decide_next_step(module, manager_url, mgr_username, mgr_password, validate_certs, can_continue, is_failed) if can_continue and not is_failed: break time.sleep(10) except Exception as err: module.fail_json(msg='Upgrade failed. Error: [%s]' % to_native(err)) else: # Paused upgrade i.e manual mode upgrade_status = get_upgrade_status(module, manager_url, mgr_username, mgr_password, validate_certs) if upgrade_status == 'NOT_STARTED': try: (rc, resp) = request(manager_url+ '/upgrade/plan?action=start', 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 upgrading. Error[%s]." % to_native(err)) else: try: (rc, resp) = request(manager_url+ '/upgrade/plan?action=continue', 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 upgrading. Error[%s]." % to_native(err)) time.sleep(10) while True: try: can_continue, is_failed = check_continuity(module, manager_url, mgr_username, mgr_password, validate_certs) decide_next_step(module, manager_url, mgr_username, mgr_password, validate_certs, can_continue, is_failed) if can_continue and not is_failed: break time.sleep(10) except Exception as err: module.fail_json(msg='Upgrade failed. Error: [%s]' % to_native(err)) module.exit_json(changed=True, message='A component has been upgraded successfully.' ' Whole system is not. Please run the module' ' again till the time whole system is' ' not upgraded.')
def main(): argument_spec = vmware_argument_spec() argument_spec.update(deployment_requests=dict(required=True, type='list'), node_name=dict(required=False, type='str'), node_id=dict(required=False, type='str'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) 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 = None if module.params['node_id']: id = module.params['node_id'] elif module.params['node_name']: node_name = module.params['node_name'] else: module.fail_json( msg= "Failed to delete manager node as non of node_id, node_name is provided." ) if not id: id = get_node_id_from_name(module, manager_url, mgr_username, mgr_password, validate_certs, '/cluster/nodes/deployments', node_name) 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(display_name=dict(required=True, type='str'), id=dict(required=True, type='str'), 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')), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) local_manager_params = get_local_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'] id = module.params['id'] manager_url = 'https://{}/global-manager/api/v1'.format(mgr_hostname) sites_api_url = manager_url + '/global-infra/sites/' local_manager_dict = get_local_manager_by_id(module, sites_api_url, mgr_username, mgr_password, validate_certs, id) local_manager_id, revision = None, None if local_manager_dict: local_manager_id = local_manager_dict['id'] revision = local_manager_dict['_revision'] if state == 'present': headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' updated = check_for_update(module, sites_api_url, mgr_username, mgr_password, validate_certs, local_manager_params) if not updated: # add the local_manager request_data = json.dumps(local_manager_params) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id='12345') try: if local_manager_id: module.exit_json( changed=False, id=local_manager_id, message="Local manager with id %s already exist." % module.params['id']) (rc, resp) = request(sites_api_url + '%s' % module.params['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 add local_manager. Request body [%s]. Error[%s]." % (request_data, to_native(err))) module.exit_json(changed=True, id=None, body=str(resp), message="Local manager with id %s created." % module.params['id']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str( json.dumps(local_manager_params)), id=local_manager_id) local_manager_params[ '_revision'] = revision # update current revision request_data = json.dumps(local_manager_params) id = local_manager_id try: (rc, resp) = request(sites_api_url + '%s' % id, data=request_data, headers=headers, method='PATCH', 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 local_manager with id %s. Request body [%s]. Error[%s]." % (id, request_data, to_native(err))) module.exit_json(changed=True, id=None, body=str(resp), message="Local manager with id %s updated." % id) elif state == 'absent': # delete the array id = local_manager_id if id is None: module.exit_json(changed=False, msg='No local manager exist with id %s' % id) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(local_manager_params)), id=id) try: (rc, resp) = request(sites_api_url + "%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 local manager with id %s. Error[%s]." % (id, to_native(err))) module.exit_json(changed=True, id=id, message="Local manager with 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 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" % virtual_ip_params, id='12345') updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, virtual_ip_params) if not updated: module.exit_json(changed=False, message="Virtual IP %s already set." % virtual_ip_address) else: 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) time.sleep(5) if not is_virtual_ip_set: module.exit_json(changed=False, object_name="Virtual IP was not set before.", message="Cleared cluster 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)) module.exit_json(changed=True, object_name=virtual_ip_address, message="Cleared cluster virtual IP address.")
def main(): argument_spec = vmware_argument_spec() argument_spec.update( display_name=dict(required=True, type='str'), description=dict(required=False, type='str'), ndra_profile_id=dict(required=False, type='str'), enable_multicast=dict(required=False, type='bool'), routing_policies=dict(required=False, type='list'), ndra_prefix_config=dict(required=False, type='list'), pim_config=dict(required=False, type='dict', enabled=dict(required=False, type='bool', default=False)), 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', profile_type=dict(required=True, type='str'), selected=dict(required=True, type='bool'), 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='bool'), 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='bool'), 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', profile_type=dict(required=True, type='str'), selected=dict(required=True, type='bool'), 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='bool'), target_id=dict(required=False, type='str'), target_type=dict(required=False, type='str')), logical_router_name=dict(required=True, type='str'), service_bindings=dict(required=False, type='list'), resource_type=dict(required=True, 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) 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' body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, logical_router_port_params) updated = check_for_update(module, manager_url, mgr_username, mgr_password, validate_certs, body) 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'), enabled=dict(required=False, type='bool'), advertise_static_routes=dict(required=False, type='bool'), advertise_dns_forwarder=dict(required=False, type='bool'), advertise_lb_snat_ip=dict(required=False, type='bool'), advertise_lb_vip=dict(required=False, type='bool'), advertise_nat_routes=dict(required=False, type='bool'), advertise_nsx_connected_routes=dict(required=False, type='bool')) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) advertise_params = get_advertise_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'] display_name = module.params['display_name'] manager_url = 'https://{}/api/v1'.format(mgr_hostname) logical_router_dict = get_lr_from_display_name(module, manager_url, mgr_username, mgr_password, validate_certs, display_name) logical_router_id = None if logical_router_dict: logical_router_id = logical_router_dict['id'] advertise_params['_revision'] = get_revision( module, manager_url, mgr_username, mgr_password, validate_certs, logical_router_id) # update current revision headers = dict(Accept="application/json") headers['Content-Type'] = 'application/json' # add the pool if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(advertise_params)), id='12345') request_data = json.dumps(advertise_params) try: (rc, resp) = request( manager_url + '/logical-routers/%s/routing/advertisement' % logical_router_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 toggle config. 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="Router advertisement set for display name %s." % module.params['display_name'])
def main(): argument_spec = vmware_argument_spec() argument_spec.update(display_name=dict(required=True, type='str'), description=dict(required=True, type='str'), resource_type=dict(required=True, type='str'), transport_node_profile_name=dict(required=True, type='str'), compute_manager_name=dict(required=False, type='str'), cluster_name=dict(required=False, type='str'), state=dict(required=True, choices=['present', 'absent'])) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) transport_node_collections_params = get_transport_node_collections_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_collections_dict = get_transport_node_collection_from_display_name (module, manager_url, mgr_username, mgr_password, validate_certs, display_name) transport_node_collection_id, revision = None, None if transport_node_collections_dict: transport_node_collection_id = transport_node_collections_dict['id'] revision = transport_node_collections_dict['_revision'] if state == 'present': body = update_params_with_id(module, manager_url, mgr_username, mgr_password, validate_certs, transport_node_collections_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 transport_node_collections request_data = json.dumps(transport_node_collections_params) if module.check_mode: module.exit_json(changed=True, debug_out=str(request_data), id='12345') try: if transport_node_collection_id: module.exit_json(changed=False, id=transport_node_collection_id, message="transport-node-collection with display_name %s already exist on cluster %s." % (module.params['display_name'], module.params['cluster_name'])) (rc, resp) = request(manager_url+ '/transport-node-collections', 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_collections. Request body [%s]. Error[%s]." % (request_data, to_native(err))) module.exit_json(changed=True, id=resp["id"], body= str(resp), message="transport-node-collection created for cluster %s." % module.params['cluster_name']) else: if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(transport_node_collections_params)), id=transport_node_collection_id) transport_node_collections_params['_revision'] = revision # update current revision request_data = json.dumps(transport_node_collections_params) id = transport_node_collection_id try: (rc, resp) = request(manager_url+ '/transport-node-collections/%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_collections 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="transport-node-collection with Compute collection fabric template id %s updated." % id) elif state == 'absent': # delete the array id = transport_node_collection_id if id is None: module.exit_json(changed=False, msg='No transport-node-collection exist with display_name %s' % display_name) if module.check_mode: module.exit_json(changed=True, debug_out=str(json.dumps(transport_node_collections_params)), id=id) try: (rc, resp) = request(manager_url + "/transport-node-collections/%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-collection with name %s. Error[%s]." % (display_name, to_native(err))) wait_till_delete(id, module, manager_url, mgr_username, mgr_password, validate_certs) module.exit_json(changed=True, id=id, message="transport-node-collection with name %s deleted." % display_name)