def main(): module = AnsibleModule( argument_spec=dict( name=dict(required=True), description=dict(), inventory=dict(required=True), enabled=dict(type='bool', default=True), variables=dict(), tower_host=dict(), tower_username=dict(), tower_password=dict(no_log=True), tower_verify_ssl=dict(type='bool', default=True), tower_config_file=dict(type='path'), state=dict(choices=['present', 'absent'], default='present'), ), supports_check_mode=True ) if not HAS_TOWER_CLI: module.fail_json(msg='ansible-tower-cli required for this module') 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:]) variables = module.contents_from_file(filename) 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) 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(): module = AnsibleModule(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), tower_host=dict(), tower_username=dict(), tower_password=dict(no_log=True), tower_verify_ssl=dict(type='bool', default=True), tower_config_file=dict(type='path'), state=dict(choices=['present', 'absent'], default='present'), ), supports_check_mode=True) if not HAS_TOWER_CLI: module.fail_json(msg='ansible-tower-cli required for this module') name = module.params.get('name') inventory = module.params.get('inventory') credential = module.params.get('credential') state = module.params.get('state') variables = module.params.get('variables') if variables: if variables.startswith('@'): filename = os.path.expanduser(variables[1:]) variables = module.contents_from_file(filename) 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) 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(): module = AnsibleModule( 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), tower_host=dict(), tower_username=dict(), tower_password=dict(no_log=True), tower_verify_ssl=dict(type='bool', default=True), tower_config_file=dict(type='path'), state=dict(choices=['present', 'absent'], default='present'), ), supports_check_mode=True ) if not HAS_TOWER_CLI: module.fail_json(msg='ansible-tower-cli required for this module') name = module.params.get('name') inventory = module.params.get('inventory') credential = module.params.get('credential') state = module.params.get('state') variables = module.params.get('variables') if variables: if variables.startswith('@'): filename = os.path.expanduser(variables[1:]) variables = module.contents_from_file(filename) 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) 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)