def main(): argument_spec = dict( name=dict(required=True), value=dict(required=True), ) module = TowerModule( argument_spec=argument_spec, supports_check_mode=False ) json_output = {} name = module.params.get('name') value = module.params.get('value') tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) try: setting = tower_cli.get_resource('setting') result = setting.modify(setting=name, value=value) json_output['id'] = result['id'] json_output['value'] = result['value'] except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json(msg='Failed to modify the setting: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(required=True), description=dict(), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) name = module.params.get('name') description = module.params.get('description') state = module.params.get('state') json_output = {'organization': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) organization = tower_cli.get_resource('organization') try: if state == 'present': result = organization.modify(name=name, description=description, create_on_missing=True) json_output['id'] = result['id'] elif state == 'absent': result = organization.delete(name=name) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json(msg='Failed to update the organization: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( job_id=dict(type='int', required=True), fail_if_not_running=dict(type='bool', default=False), ) module = TowerModule( argument_spec=argument_spec, supports_check_mode=True, ) job_id = module.params.get('job_id') json_output = {} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) job = tower_cli.get_resource('job') params = module.params.copy() try: result = job.cancel(job_id, **params) json_output['id'] = job_id except (exc.ConnectionError, exc.BadRequest, exc.TowerCLIError, exc.AuthError) as excinfo: module.fail_json(msg='Unable to cancel job_id/{0}: {1}'.format( job_id, excinfo), changed=False) json_output['changed'] = result['changed'] json_output['status'] = result['status'] module.exit_json(**json_output)
def main(): argument_spec = dict( status=dict(choices=[ 'pending', 'waiting', 'running', 'error', 'failed', 'canceled', 'successful' ]), page=dict(type='int'), all_pages=dict(type='bool', default=False), query=dict(type='dict'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) json_output = {} query = module.params.get('query') status = module.params.get('status') page = module.params.get('page') all_pages = module.params.get('all_pages') tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) try: job = tower_cli.get_resource('job') params = {'status': status, 'page': page, 'all_pages': all_pages} if query: params['query'] = query.items() json_output = job.list(**params) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json(msg='Failed to list jobs: {0}'.format(excinfo), changed=False) module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(required=True), description=dict(), inventory=dict(required=True), enabled=dict(type='bool', default=True), variables=dict(), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) name = module.params.get('name') description = module.params.get('description') inventory = module.params.get('inventory') enabled = module.params.get('enabled') state = module.params.get('state') variables = module.params.get('variables') if variables: if variables.startswith('@'): filename = os.path.expanduser(variables[1:]) with open(filename, 'r') as f: variables = f.read() json_output = {'host': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) host = tower_cli.get_resource('host') try: inv_res = tower_cli.get_resource('inventory') inv = inv_res.get(name=inventory) if state == 'present': result = host.modify(name=name, inventory=inv['id'], enabled=enabled, variables=variables, description=description, create_on_missing=True) json_output['id'] = result['id'] elif state == 'absent': result = host.delete(name=name, inventory=inv['id']) except (exc.NotFound) as excinfo: module.fail_json( msg='Failed to update host, inventory not found: {0}'.format( excinfo), changed=False) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json(msg='Failed to update host: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(required=True), description=dict(required=False), kind=dict(required=False, choices=KIND_CHOICES.keys()), inputs=dict(type='dict', required=False), injectors=dict(type='dict', required=False), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=False) name = module.params.get('name') kind = module.params.get('kind') state = module.params.get('state') json_output = {'credential_type': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) credential_type_res = tower_cli.get_resource('credential_type') params = {} params['name'] = name params['kind'] = kind params['managed_by_tower'] = False if module.params.get('description'): params['description'] = module.params.get('description') if module.params.get('inputs'): params['inputs'] = module.params.get('inputs') if module.params.get('injectors'): params['injectors'] = module.params.get('injectors') try: if state == 'present': params['create_on_missing'] = True result = credential_type_res.modify(**params) json_output['id'] = result['id'] elif state == 'absent': params['fail_on_missing'] = False result = credential_type_res.delete(**params) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json( msg='Failed to update credential type: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( username=dict(required=True), first_name=dict(), last_name=dict(), password=dict(no_log=True), email=dict(required=True), superuser=dict(type='bool', default=False), auditor=dict(type='bool', default=False), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) username = module.params.get('username') first_name = module.params.get('first_name') last_name = module.params.get('last_name') password = module.params.get('password') email = module.params.get('email') superuser = module.params.get('superuser') auditor = module.params.get('auditor') state = module.params.get('state') json_output = {'username': username, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) user = tower_cli.get_resource('user') try: if state == 'present': result = user.modify(username=username, first_name=first_name, last_name=last_name, email=email, password=password, is_superuser=superuser, is_system_auditor=auditor, create_on_missing=True) json_output['id'] = result['id'] elif state == 'absent': result = user.delete(username=username) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json( msg='Failed to update the user: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( job_id=dict(type='int', required=True), timeout=dict(type='int'), min_interval=dict(type='float', default=1), max_interval=dict(type='float', default=30), ) module = TowerModule(argument_spec, supports_check_mode=True) json_output = {} fail_json = None tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) job = tower_cli.get_resource('job') params = module.params.copy() # tower-cli gets very noisy when monitoring. # We pass in our our outfile to suppress the out during our monitor call. outfile = StringIO() params['outfile'] = outfile job_id = params.get('job_id') try: result = job.monitor(job_id, **params) except exc.Timeout as excinfo: result = job.status(job_id) result['id'] = job_id json_output['msg'] = 'Timeout waiting for job to finish.' json_output['timeout'] = True except exc.NotFound as excinfo: fail_json = dict( msg='Unable to wait, no job_id {0} found: {1}'.format( job_id, excinfo), changed=False) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: fail_json = dict(msg='Unable to wait for job: {0}'.format(excinfo), changed=False) if fail_json is not None: module.fail_json(**fail_json) json_output['success'] = True for k in ('id', 'status', 'elapsed', 'started', 'finished'): json_output[k] = result.get(k) module.exit_json(**json_output)
def main(): argument_spec = dict( job_template=dict(required=True, type='str'), job_type=dict(choices=['run', 'check', 'scan']), inventory=dict(type='str', default=None), credential=dict(type='str', default=None), limit=dict(), tags=dict(type='list'), extra_vars=dict(type='list'), ) module = TowerModule(argument_spec, supports_check_mode=True) json_output = {} tags = module.params.get('tags') tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) try: params = module.params.copy() if isinstance(tags, list): params['tags'] = ','.join(tags) job = tower_cli.get_resource('job') lookup_fields = ('job_template', 'inventory', 'credential') for field in lookup_fields: try: name = params.pop(field) if name: result = tower_cli.get_resource(field).get(name=name) params[field] = result['id'] except exc.NotFound as excinfo: module.fail_json( msg='Unable to launch job, {0}/{1} was not found: {2}'. format(field, name, excinfo), changed=False) result = job.launch(no_input=True, **params) json_output['id'] = result['id'] json_output['status'] = result['status'] except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json(msg='Unable to launch job: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(required=True), description=dict(), organization=dict(required=True), variables=dict(), kind=dict(choices=['', 'smart'], default=''), host_filter=dict(), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) name = module.params.get('name') description = module.params.get('description') organization = module.params.get('organization') variables = module.params.get('variables') state = module.params.get('state') kind = module.params.get('kind') host_filter = module.params.get('host_filter') json_output = {'inventory': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) inventory = tower_cli.get_resource('inventory') try: org_res = tower_cli.get_resource('organization') org = org_res.get(name=organization) if state == 'present': result = inventory.modify(name=name, organization=org['id'], variables=variables, description=description, kind=kind, host_filter=host_filter, create_on_missing=True) json_output['id'] = result['id'] elif state == 'absent': result = inventory.delete(name=name, organization=org['id']) except (exc.NotFound) as excinfo: module.fail_json(msg='Failed to update inventory, organization not found: {0}'.format(excinfo), changed=False) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json(msg='Failed to update inventory: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(), description=dict(), organization=dict(), scm_type=dict(choices=['manual', 'git', 'hg', 'svn'], default='manual'), scm_url=dict(), scm_branch=dict(), scm_credential=dict(), scm_clean=dict(type='bool', default=False), scm_delete_on_update=dict(type='bool', default=False), scm_update_on_launch=dict(type='bool', default=False), scm_update_cache_timeout=dict(type='int', default=0), job_timeout=dict(type='int', default=0), custom_virtualenv=dict(), local_path=dict(), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) name = module.params.get('name') description = module.params.get('description') organization = module.params.get('organization') scm_type = module.params.get('scm_type') if scm_type == "manual": scm_type = "" scm_url = module.params.get('scm_url') local_path = module.params.get('local_path') scm_branch = module.params.get('scm_branch') scm_credential = module.params.get('scm_credential') scm_clean = module.params.get('scm_clean') scm_delete_on_update = module.params.get('scm_delete_on_update') scm_update_on_launch = module.params.get('scm_update_on_launch') scm_update_cache_timeout = module.params.get('scm_update_cache_timeout') job_timeout = module.params.get('job_timeout') custom_virtualenv = module.params.get('custom_virtualenv') state = module.params.get('state') json_output = {'project': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) project = tower_cli.get_resource('project') try: if state == 'present': try: org_res = tower_cli.get_resource('organization') org = org_res.get(name=organization) except (exc.NotFound) as excinfo: module.fail_json( msg= 'Failed to update project, organization not found: {0}' .format(organization), changed=False) if scm_credential: try: cred_res = tower_cli.get_resource('credential') try: cred = cred_res.get(name=scm_credential) except (tower_cli.exceptions.MultipleResults ) as multi_res_excinfo: module.warn( 'Multiple credentials found for {0}, falling back looking in project organization' .format(scm_credential)) cred = cred_res.get(name=scm_credential, organization=org['id']) scm_credential = cred['id'] except (exc.NotFound) as excinfo: module.fail_json( msg= 'Failed to update project, credential not found: {0}' .format(scm_credential), changed=False) if (scm_update_cache_timeout is not None) and (scm_update_on_launch is not True): module.warn( 'scm_update_cache_timeout will be ignored since scm_update_on_launch was not set to true' ) result = project.modify( name=name, description=description, organization=org['id'], scm_type=scm_type, scm_url=scm_url, local_path=local_path, scm_branch=scm_branch, scm_clean=scm_clean, credential=scm_credential, scm_delete_on_update=scm_delete_on_update, scm_update_on_launch=scm_update_on_launch, scm_update_cache_timeout=scm_update_cache_timeout, job_timeout=job_timeout, custom_virtualenv=custom_virtualenv, create_on_missing=True) json_output['id'] = result['id'] elif state == 'absent': result = project.delete(name=name) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json( msg='Failed to update project: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(required=True), description=dict(), inventory=dict(required=True), variables=dict(), credential=dict(), source=dict(choices=["manual", "file", "ec2", "rax", "vmware", "gce", "azure", "azure_rm", "openstack", "satellite6", "cloudforms", "custom"], default="manual"), source_regions=dict(), source_vars=dict(), instance_filters=dict(), group_by=dict(), source_script=dict(), overwrite=dict(type='bool', default=False), overwrite_vars=dict(), update_on_launch=dict(type='bool', default=False), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) name = module.params.get('name') inventory = module.params.get('inventory') credential = module.params.get('credential') state = module.params.pop('state') variables = module.params.get('variables') if variables: if variables.startswith('@'): filename = os.path.expanduser(variables[1:]) with open(filename, 'r') as f: variables = f.read() json_output = {'group': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) group = tower_cli.get_resource('group') try: params = module.params.copy() params['create_on_missing'] = True params['variables'] = variables inv_res = tower_cli.get_resource('inventory') inv = inv_res.get(name=inventory) params['inventory'] = inv['id'] if credential: cred_res = tower_cli.get_resource('credential') cred = cred_res.get(name=credential) params['credential'] = cred['id'] if state == 'present': result = group.modify(**params) json_output['id'] = result['id'] elif state == 'absent': result = group.delete(**params) except (exc.NotFound) as excinfo: module.fail_json(msg='Failed to update the group, inventory not found: {0}'.format(excinfo), changed=False) except (exc.ConnectionError, exc.BadRequest, exc.NotFound, exc.AuthError) as excinfo: module.fail_json(msg='Failed to update the group: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(required=True), description=dict(default=''), job_type=dict(choices=['run', 'check', 'scan'], required=True), inventory=dict(default=''), project=dict(required=True), playbook=dict(required=True), credential=dict(default=''), vault_credential=dict(default=''), forks=dict(type='int'), limit=dict(default=''), verbosity=dict(type='int', choices=[0, 1, 2, 3, 4], default=0), extra_vars_path=dict(type='path', required=False), job_tags=dict(default=''), force_handlers_enabled=dict(type='bool', default=False), skip_tags=dict(default=''), start_at_task=dict(default=''), timeout=dict(type='int', default=0), fact_caching_enabled=dict(type='bool', default=False), host_config_key=dict(default=''), ask_diff_mode=dict(type='bool', default=False), ask_extra_vars=dict(type='bool', default=False), ask_limit=dict(type='bool', default=False), ask_tags=dict(type='bool', default=False), ask_skip_tags=dict(type='bool', default=False), ask_job_type=dict(type='bool', default=False), ask_verbosity=dict(type='bool', default=False), ask_inventory=dict(type='bool', default=False), ask_credential=dict(type='bool', default=False), survey_enabled=dict(type='bool', default=False), survey_spec=dict(type='dict', required=False), become_enabled=dict(type='bool', default=False), diff_mode_enabled=dict(type='bool', default=False), concurrent_jobs_enabled=dict(type='bool', default=False), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) name = module.params.get('name') state = module.params.pop('state') json_output = {'job_template': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) jt = tower_cli.get_resource('job_template') params = update_resources(module, module.params) params = update_fields(params) params['create_on_missing'] = True try: if state == 'present': result = jt.modify(**params) json_output['id'] = result['id'] elif state == 'absent': result = jt.delete(**params) except (exc.ConnectionError, exc.BadRequest, exc.NotFound, exc.AuthError) as excinfo: module.fail_json( msg='Failed to update job template: {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(required=True), description=dict(required=False), organization=dict(required=False), notification_type=dict(required=True, choices=[ 'email', 'slack', 'twilio', 'pagerduty', 'hipchat', 'webhook', 'irc' ]), notification_configuration=dict(required=False), username=dict(required=False), sender=dict(required=False), recipients=dict(required=False, type='list'), use_tls=dict(required=False, type='bool'), host=dict(required=False), use_ssl=dict(required=False, type='bool'), password=dict(required=False, no_log=True), port=dict(required=False, type='int'), channels=dict(required=False, type='list'), token=dict(required=False, no_log=True), account_token=dict(required=False, no_log=True), from_number=dict(required=False), to_numbers=dict(required=False, type='list'), account_sid=dict(required=False), subdomain=dict(required=False), service_key=dict(required=False, no_log=True), client_name=dict(required=False), message_from=dict(required=False), api_url=dict(required=False), color=dict( required=False, choices=['yellow', 'green', 'red', 'purple', 'gray', 'random']), rooms=dict(required=False, type='list'), notify=dict(required=False, type='bool'), url=dict(required=False), headers=dict(required=False, type='dict', default={}), server=dict(required=False), nickname=dict(required=False), targets=dict(required=False, type='list'), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) name = module.params.get('name') description = module.params.get('description') organization = module.params.get('organization') notification_type = module.params.get('notification_type') notification_configuration = module.params.get( 'notification_configuration') username = module.params.get('username') sender = module.params.get('sender') recipients = module.params.get('recipients') use_tls = module.params.get('use_tls') host = module.params.get('host') use_ssl = module.params.get('use_ssl') password = module.params.get('password') port = module.params.get('port') channels = module.params.get('channels') token = module.params.get('token') account_token = module.params.get('account_token') from_number = module.params.get('from_number') to_numbers = module.params.get('to_numbers') account_sid = module.params.get('account_sid') subdomain = module.params.get('subdomain') service_key = module.params.get('service_key') client_name = module.params.get('client_name') message_from = module.params.get('message_from') api_url = module.params.get('api_url') color = module.params.get('color') rooms = module.params.get('rooms') notify = module.params.get('notify') url = module.params.get('url') headers = module.params.get('headers') server = module.params.get('server') nickname = module.params.get('nickname') targets = module.params.get('targets') state = module.params.get('state') json_output = {'notification': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) notification_template = tower_cli.get_resource('notification_template') try: org_res = tower_cli.get_resource('organization') org = org_res.get(name=organization) if state == 'present': result = notification_template.modify( name=name, description=description, organization=org['id'], notification_type=notification_type, notification_configuration=notification_configuration, username=username, sender=sender, recipients=recipients, use_tls=use_tls, host=host, use_ssl=use_ssl, password=password, port=port, channels=channels, token=token, account_token=account_token, from_number=from_number, to_numbers=to_numbers, account_sid=account_sid, subdomain=subdomain, service_key=service_key, client_name=client_name, message_from=message_from, api_url=api_url, color=color, rooms=rooms, notify=notify, url=url, headers=headers, server=server, nickname=nickname, targets=targets, create_on_missing=True) json_output['id'] = result['id'] elif state == 'absent': result = notification_template.delete(name=name) except (exc.NotFound) as excinfo: module.fail_json( msg= 'Failed to update notification template, organization not found: {0}' .format(excinfo), changed=False) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json( msg='Failed to update notification template: {0}'.format( excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(required=True), description=dict(required=False), extra_vars=dict(required=False), organization=dict(required=False), allow_simultaneous=dict(type='bool', required=False), schema=dict(required=False), survey=dict(required=False), survey_enabled=dict(type='bool', required=False), inventory=dict(required=False), ask_inventory=dict(type='bool', required=False), ask_extra_vars=dict(type='bool', required=False), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=False) name = module.params.get('name') state = module.params.get('state') schema = None if module.params.get('schema'): schema = module.params.get('schema') if schema and state == 'absent': module.fail_json( msg='Setting schema when state is absent is not allowed', changed=False) json_output = {'workflow_template': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) wfjt_res = tower_cli.get_resource('workflow') params = {} params['name'] = name if module.params.get('description'): params['description'] = module.params.get('description') if module.params.get('organization'): organization_res = tower_cli.get_resource('organization') try: organization = organization_res.get( name=module.params.get('organization')) params['organization'] = organization['id'] except exc.NotFound as excinfo: module.fail_json(msg='Failed to update organization source,' 'organization not found: {0}'.format(excinfo), changed=False) if module.params.get('survey'): params['survey_spec'] = module.params.get('survey') if module.params.get('ask_extra_vars'): params['ask_variables_on_launch'] = module.params.get( 'ask_extra_vars') if module.params.get('ask_inventory'): params['ask_inventory_on_launch'] = module.params.get( 'ask_inventory') for key in ('allow_simultaneous', 'extra_vars', 'inventory', 'survey_enabled', 'description'): if module.params.get(key): params[key] = module.params.get(key) try: if state == 'present': params['create_on_missing'] = True result = wfjt_res.modify(**params) json_output['id'] = result['id'] if schema: wfjt_res.schema(result['id'], schema) elif state == 'absent': params['fail_on_missing'] = False result = wfjt_res.delete(**params) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json(msg='Failed to update workflow template: \ {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)
def main(): argument_spec = dict( name=dict(required=True), description=dict(required=False), inventory=dict(required=True), source=dict(required=True, choices=SOURCE_CHOICES.keys()), credential=dict(required=False), source_vars=dict(required=False), timeout=dict(type='int', required=False), source_project=dict(required=False), source_path=dict(required=False), update_on_project_update=dict(type='bool', required=False), source_regions=dict(required=False), instance_filters=dict(required=False), group_by=dict(required=False), source_script=dict(required=False), overwrite=dict(type='bool', required=False), overwrite_vars=dict(type='bool', required=False), update_on_launch=dict(type='bool', required=False), update_cache_timeout=dict(type='int', required=False), state=dict(choices=['present', 'absent'], default='present'), ) module = TowerModule(argument_spec=argument_spec, supports_check_mode=True) name = module.params.get('name') inventory = module.params.get('inventory') source = module.params.get('source') state = module.params.get('state') json_output = {'inventory_source': name, 'state': state} tower_auth = tower_auth_config(module) with settings.runtime_values(**tower_auth): tower_check_mode(module) inventory_source = tower_cli.get_resource('inventory_source') try: params = {} params['name'] = name params['source'] = source if module.params.get('description'): params['description'] = module.params.get('description') if module.params.get('credential'): credential_res = tower_cli.get_resource('credential') try: credential = credential_res.get( name=module.params.get('credential')) params['credential'] = credential['id'] except (exc.NotFound) as excinfo: module.fail_json( msg='Failed to update credential source,' 'credential not found: {0}'.format(excinfo), changed=False ) if module.params.get('source_project'): source_project_res = tower_cli.get_resource('project') try: source_project = source_project_res.get( name=module.params.get('source_project')) params['source_project'] = source_project['id'] except (exc.NotFound) as excinfo: module.fail_json( msg='Failed to update source project,' 'project not found: {0}'.format(excinfo), changed=False ) if module.params.get('source_script'): source_script_res = tower_cli.get_resource('inventory_script') try: script = source_script_res.get( name=module.params.get('source_script')) params['source_script'] = script['id'] except (exc.NotFound) as excinfo: module.fail_json( msg='Failed to update source script,' 'script not found: {0}'.format(excinfo), changed=False ) try: inventory_res = tower_cli.get_resource('inventory') params['inventory'] = inventory_res.get(name=inventory)['id'] except (exc.NotFound) as excinfo: module.fail_json( msg='Failed to update inventory source, ' 'inventory not found: {0}'.format(excinfo), changed=False ) for key in ('source_vars', 'timeout', 'source_path', 'update_on_project_update', 'source_regions', 'instance_filters', 'group_by', 'overwrite', 'overwrite_vars', 'update_on_launch', 'update_cache_timeout'): if module.params.get(key) is not None: params[key] = module.params.get(key) if state == 'present': params['create_on_missing'] = True result = inventory_source.modify(**params) json_output['id'] = result['id'] elif state == 'absent': params['fail_on_missing'] = False result = inventory_source.delete(**params) except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: module.fail_json(msg='Failed to update inventory source: \ {0}'.format(excinfo), changed=False) json_output['changed'] = result['changed'] module.exit_json(**json_output)