def run_ansible(params, check_mode=False): '''run the oc_obj module''' ocobj = OCList(params['namespace'], params['selector'], oc_binary=params['oc_binary'], verbose=params['debug'], field_selector=params['field_selector']) state = params['state'] if state == 'present': ######## # Create ######## if check_mode: return { 'changed': True, 'msg': 'CHECK_MODE: Would have performed a create' } # Create it here api_rval = ocobj.create(params['files'], params['content']) if api_rval['returncode'] != 0: return {'failed': True, 'msg': api_rval} # Remove files if params['files'] and params['delete_after']: Utils.cleanup(params['files']) return {'changed': True, 'results': api_rval, 'state': state} # catch all return {'failed': True, 'msg': "Unknown State passed"}
def run_ansible(params, check_mode): '''run the oc_secret module''' ocsecret = OCSecret(params['namespace'], params['name'], params['type'], params['decode'], oc_binary=params['oc_binary'], verbose=params['debug']) state = params['state'] api_rval = ocsecret.get() ##### # Get ##### if state == 'list': return { 'changed': False, 'ansible_module_results': api_rval, state: 'list' } if not params['name']: return { 'failed': True, 'msg': 'Please specify a name when state is absent|present.' } ######## # Delete ######## if state == 'absent': if not Utils.exists(api_rval['results'], params['name']): return {'changed': False, 'state': 'absent'} if check_mode: return { 'changed': True, 'msg': 'Would have performed a delete.' } api_rval = ocsecret.delete() return { 'changed': True, 'ansible_module_results': api_rval, 'state': 'absent' } if state == 'present': if params['files']: files = params['files'] elif params['contents']: files = Utils.create_tmp_files_from_contents( params['contents']) else: files = [{'name': 'null', 'path': os.devnull}] ######## # Create ######## if not Utils.exists(api_rval['results'], params['name']): if check_mode: return { 'changed': True, 'msg': 'Would have performed a create.' } api_rval = ocsecret.create(files, params['contents'], params['from_literal'], params['cert'], params['key'], force=params['force']) # Remove files if files and params['delete_after']: Utils.cleanup([ftmp['path'] for ftmp in files]) if api_rval['returncode'] != 0: return {'failed': True, 'msg': api_rval} return { 'changed': True, 'ansible_module_results': api_rval, 'state': 'present' } ######## # Update ######## # if update is set to false, return update = params['update'] if not update: return { 'changed': False, 'ansible_module_results': api_rval, 'state': state } secret = ocsecret.prep_secret(params['files'], params['contents'], force=params['force']) if secret['returncode'] != 0: return {'failed': True, 'msg': secret} if Utils.check_def_equal(secret['results'], api_rval['results'][0]): # Remove files if files and params['delete_after']: Utils.cleanup([ftmp['path'] for ftmp in files]) return { 'changed': False, 'ansible_module_results': secret['results'], 'state': 'present' } if check_mode: return { 'changed': True, 'msg': 'Would have performed an update.' } api_rval = ocsecret.update(files, force=params['force']) # Remove files if secret and params['delete_after']: Utils.cleanup([ftmp['path'] for ftmp in files]) if api_rval['returncode'] != 0: return {'failed': True, 'msg': api_rval} return { 'changed': True, 'ansible_module_results': api_rval, 'state': 'present' } return { 'failed': True, 'changed': False, 'msg': 'Unknown state passed. %s' % state, 'state': 'unknown' }
def run_ansible(params, check_mode=False): '''run the oc_obj module''' ocobj = OCObject(params['kind'], params['namespace'], params['name'], params['selector'], oc_binary=params['oc_binary'], verbose=params['debug'], all_namespaces=params['all_namespaces'], field_selector=params['field_selector']) state = params['state'] api_rval = ocobj.get() ##### # Get ##### if state == 'list': if api_rval['returncode'] != 0: return {'changed': False, 'failed': True, 'msg': api_rval} return {'changed': False, 'results': api_rval, 'state': state} ######## # Delete ######## if state == 'absent': # verify it's not in our results if (params['name'] is not None or params['selector'] is not None) and \ (len(api_rval['results']) == 0 or \ (not api_rval['results'][0]) or \ ('items' in api_rval['results'][0] and len(api_rval['results'][0]['items']) == 0)): return {'changed': False, 'state': state} if check_mode: return { 'changed': True, 'msg': 'CHECK_MODE: Would have performed a delete' } api_rval = ocobj.delete() if api_rval['returncode'] != 0: return {'failed': True, 'msg': api_rval} return {'changed': True, 'results': api_rval, 'state': state} # create/update: Must define a name beyond this point if not params['name']: return { 'failed': True, 'msg': 'Please specify a name when state is present.' } if state == 'present': ######## # Create ######## if not Utils.exists(api_rval['results'], params['name']): if check_mode: return { 'changed': True, 'msg': 'CHECK_MODE: Would have performed a create' } # Create it here api_rval = ocobj.create(params['files'], params['content']) if api_rval['returncode'] != 0: return {'failed': True, 'msg': api_rval} # return the created object api_rval = ocobj.get() if api_rval['returncode'] != 0: return {'failed': True, 'msg': api_rval} # Remove files if params['files'] and params['delete_after']: Utils.cleanup(params['files']) return {'changed': True, 'results': api_rval, 'state': state} ######## # Update ######## # if update is set to false, return update = params['update'] if not update: if params['files'] and params['delete_after']: Utils.cleanup(params['files']) return { 'changed': False, 'results': api_rval['results'][0], 'state': state } # if a file path is passed, use it. needs_update = ocobj.needs_update(params['files'], params['content']) if not isinstance(needs_update, bool): return {'failed': True, 'msg': update} # No changes if not needs_update: if params['files'] and params['delete_after']: Utils.cleanup(params['files']) return { 'changed': False, 'results': api_rval['results'][0], 'state': state } if check_mode: return { 'changed': True, 'msg': 'CHECK_MODE: Would have performed an update.' } api_rval = ocobj.update(params['files'], params['content'], params['force']) if api_rval['returncode'] != 0: return {'failed': True, 'msg': api_rval} # return the created object api_rval = ocobj.get() if api_rval['returncode'] != 0: return {'failed': True, 'msg': api_rval} return {'changed': True, 'results': api_rval, 'state': state}