def post(self): parser = reqparse.RequestParser() parser.add_argument('playbook_dir', type=str, help='folder where playbook file resides', required=True) parser.add_argument('remote_name', type=str, help='remote name', required=False) parser.add_argument('branch_name', type=str, help='branch name', required=False) parser.add_argument('reset', type=bool, help='hard reset', required=False) args = parser.parse_args() playbook_dir = args['playbook_dir'] remote_name = args['remote_name'] branch_name = args['branch_name'] reset = args['reset'] if not remote_name: remote_name = 'origin' if not branch_name: branch_name = 'master' if not reset: reset = False task_result = FlansibleGit.update_git_repo(playbook_dir, remote_name, branch_name, reset) result = {'task_id': task_result.id} return result
def post(self): parser = reqparse.RequestParser() parser.add_argument('playbook_dir', type=str, help='folder where playbook file resides', required=True) parser.add_argument('playbook', type=str, help='name of the playbook', required=True) parser.add_argument('inventory', type=str, help='path to inventory', required=False,) parser.add_argument('extra_vars', type=dict, help='extra vars', required=False) parser.add_argument('forks', type=int, help='forks', required=False) parser.add_argument('verbose_level', type=int, help='verbose level, 1-4', required=False) parser.add_argument('vault_file', type=str, help='vault file location', required=False) parser.add_argument('become', type=bool, help='run with become', required=False) parser.add_argument('update_git_repo', type=bool, help='Set to true to update git repo prior to executing', required=False) args = parser.parse_args() playbook_dir = args['playbook_dir'] playbook = args['playbook'] become = args['become'] inventory = args['inventory'] extra_vars = args['extra_vars'] vault_file = args['vault_file'] do_update_git_repo = args['update_git_repo'] if do_update_git_repo is True: result = FlansibleGit.update_git_repo(playbook_dir) task = celery_runner.do_long_running_task.AsyncResult(result.id) while task.state == "PENDING" or task.state == "PROGRESS": #waiting for finish task = celery_runner.do_long_running_task.AsyncResult(result.id) if task.result['returncode'] != 0: #git update failed resp = app.make_response((str.format("Failed to update git repo: {0}", playbook_dir), 404)) return resp curr_user = auth.username() playbook_full_path = playbook_dir + "/" + playbook playbook_full_path = playbook_full_path.replace("//", "/") if not os.path.exists(playbook_dir): resp = app.make_response((str.format("Directory not found: {0}", playbook_dir), 404)) return resp if not os.path.isdir(playbook_dir): resp = app.make_response((str.format("Not a directory: {0}", playbook_dir), 404)) return resp if not os.path.exists(playbook_full_path): resp = app.make_response((str.format("Playbook not found in folder. Path does not exist: {0}", playbook_full_path), 404)) return resp if not inventory: inventory = ansible_default_inventory has_inv_access = get_inventory_access(curr_user, inventory) if not has_inv_access: resp = app.make_response((str.format("User does not have access to inventory {0}", inventory), 403)) return resp else: if not os.path.exists(inventory): resp = app.make_response((str.format("Inventory path not found: {0}", inventory), 404)) return resp inventory = str.format(" -i {0}", inventory) if become: become_string = ' --become' else: become_string = '' extra_vars_string = '' if extra_vars: #extra_vars_string = str.format(" --extra-vars \'{0}\'", (json.dumps(extra_vars))) extra_vars_string = " --extra-vars '%s'" % (json.dumps(extra_vars).replace("'", "'\\''")) if vault_file: vault_file_string = " --vault-password-file '%s'" % (vault_file) command = str.format("cd {0};ansible-playbook {1}{2}{3}{4}{5}", playbook_dir, playbook, become_string, inventory, extra_vars_string, vault_file_string) task_result = celery_runner.do_long_running_task.apply_async([command], soft=task_timeout, hard=task_timeout) result = {'task_id': task_result.id} return result
def post(self): parser = reqparse.RequestParser() parser.add_argument('playbook_dir', type=str, help='folder where playbook file resides', required=True) parser.add_argument('playbook', type=str, help='name of the playbook', required=True) parser.add_argument( 'inventory', type=str, help='path to inventory', required=False, ) parser.add_argument('extra_vars', type=dict, help='extra vars', required=False) parser.add_argument('forks', type=int, help='forks', required=False) parser.add_argument('verbose_level', type=int, help='verbose level, 1-4', required=False) parser.add_argument('become', type=bool, help='run with become', required=False) parser.add_argument( 'update_git_repo', type=bool, help='Set to true to update git repo prior to executing', required=False) args = parser.parse_args() playbook_dir = args['playbook_dir'] playbook = args['playbook'] become = args['become'] inventory = args['inventory'] update_git_repo = args['update_git_repo'] if update_git_repo is True: playbook_dir, playbook = FlansibleGit.update_git_repo( playbook_dir, playbook) curr_user = auth.username() playbook_full_path = playbook_dir + "/" + playbook playbook_full_path = playbook_full_path.replace("//", "/") if not os.path.exists(playbook_dir): resp = app.make_response((str.format("Directory not found: {0}", playbook_dir), 404)) return resp if not os.path.isdir(playbook_dir): resp = app.make_response((str.format("Not a directory: {0}", playbook_dir), 404)) return resp if not os.path.exists(playbook_full_path): resp = app.make_response((str.format( "Playbook not found in folder. Path does not exist: {0}", playbook_full_path), 404)) return resp if not inventory: inventory = ansible_default_inventory has_inv_access = get_inventory_access(curr_user, inventory) if not has_inv_access: resp = app.make_response( (str.format("User does not have access to inventory {0}", inventory), 403)) return resp inventory = str.format(" -i {0}", inventory) if become: become_string = ' --become' else: become_string = '' command = str.format("cd {0};ansible-playbook {1}{2}{3}", playbook_dir, playbook, become_string, inventory) task_result = do_long_running_task.apply_async([command], soft=task_timeout, hard=task_timeout) result = {'task_id': task_result.id} return result
def post(self): parser = reqparse.RequestParser() parser.add_argument('playbook_dir', type=str, help='folder where playbook file resides', required=True) parser.add_argument('playbook', type=str, help='name of the playbook', required=True) parser.add_argument('inventory', type=str, help='path to inventory', required=False,) parser.add_argument('extra_vars', type=dict, help='extra vars', required=False) parser.add_argument('forks', type=int, help='forks', required=False) parser.add_argument('verbose_level', type=int, help='verbose level, 1-4', required=False) parser.add_argument('become', type=bool, help='run with become', required=False) parser.add_argument('update_git_repo', type=bool, help='Set to true to update git repo prior to executing', required=False) parser.add_argument('limit', type=str, help='limit playbook target', required=False) args = parser.parse_args() playbook_dir = args['playbook_dir'] playbook = args['playbook'] become = args['become'] inventory = args['inventory'] extra_vars = args['extra_vars'] do_update_git_repo = args['update_git_repo'] limit = args['limit'] if do_update_git_repo is True: result = FlansibleGit.update_git_repo(playbook_dir) task = celery_runner.do_long_running_task.AsyncResult(result.id) while task.state == "PENDING" or task.state == "PROGRESS": #waiting for finish task = celery_runner.do_long_running_task.AsyncResult(result.id) if task.result['returncode'] != 0: #git update failed resp = app.make_response((str.format("Failed to update git repo: {0}", playbook_dir), 404)) return resp curr_user = auth.username() playbook_full_path = playbook_dir + "/" + playbook playbook_full_path = playbook_full_path.replace("//","/") if not os.path.exists(playbook_dir): resp = app.make_response((str.format("Directory not found: {0}", playbook_dir), 404)) return resp if not os.path.isdir(playbook_dir): resp = app.make_response((str.format("Not a directory: {0}", playbook_dir), 404)) return resp if not os.path.exists(playbook_full_path): resp = app.make_response((str.format("Playbook not found in folder. Path does not exist: {0}", playbook_full_path), 404)) return resp if not inventory: inventory = ansible_default_inventory has_inv_access = get_inventory_access(curr_user, inventory) if not has_inv_access: resp = app.make_response((str.format("User does not have access to inventory {0}", inventory), 403)) return resp inventory = str.format(" -i {0}", inventory) if become: become_string = ' --become' else: become_string = '' extra_vars_string = '' if extra_vars: counter = 1 extra_vars_string += ' -e "' for key in extra_vars.keys(): if counter < len(extra_vars): spacer = " " else: spacer = "" opt_string = str.format("{0}={1}{2}",key,extra_vars[key], spacer) extra_vars_string += opt_string counter += 1 extra_vars_string += '"' if limit: limit_str = ' --limit ' + limit else: limit_str = '' command = str.format("cd {0}; ansible-playbook {1}{2}{3}{4}{5}", playbook_dir, playbook, become_string, inventory, extra_vars_string, limit_str) task_result = celery_runner.do_long_running_task.apply_async([command], soft=task_timeout, hard=task_timeout) result = {'task_id': task_result.id} return result