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'] extra_vars = args['extra_vars'] do_update_git_repo = args['update_git_repo'] curr_user = auth.username() return runPlaybook(True, curr_user, playbook_dir, playbook, inventory, extra_vars, 1, 1, become)
def submitPlaybook(): # selected playbook curPB = request.form['playbook'] varlist = FetchYamlVars(curPB) vars = {} # Playbook variables for val in varlist: vars[val] = request.form[val] pbmessage = { 'playbook': curPB, 'playbook_dir': playbook_root, 'extra_vars': vars } # Runtime variables try: inventory = request.form['inventory'] except: inventory = None try: become = request.form['become'] except: become = None if inventory: pbmessage['inventory'] = inventory if become: pbmessage['become'] = become curr_user = auth.username() cmdres = runPlaybook(False, curr_user, pbmessage['playbook_dir'], pbmessage['playbook'], inventory, pbmessage['extra_vars'], 1, become) if 'task_id' in cmdres: return redirect('/api/ansibletaskoutput/' + cmdres['task_id']) #return redirect('/status/' + cmdres['task_id']) return cmdres
def get(self): curr_user = auth.username() yamlfiles = [] print("listing playbooks in " + playbook_root) for root, dirs, files in os.walk(playbook_root): for name in files: if (get_playbook_repo_access(curr_user, root)): if name.endswith((".yaml", ".yml")): fileobj = {'playbook': name, 'playbook_dir': root} yamlfiles.append(fileobj) returnedfiles = [] for fileobj in yamlfiles: if 'group_vars' in fileobj['playbook_dir']: pass elif fileobj['playbook_dir'].endswith('handlers'): pass elif fileobj['playbook_dir'].endswith('vars'): pass else: returnedfiles.append(fileobj) return returnedfiles
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) 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
def post(self): parser = reqparse.RequestParser() parser.add_argument('host_pattern', type=str, help='need to specify host_pattern', required=True) parser.add_argument('module', type=str, help='module name', required=True) parser.add_argument('module_args', type=dict, help='module_args', required=False) parser.add_argument('extra_vars', type=dict, help='extra_vars', required=False) parser.add_argument('inventory', type=str, help='path to inventory', 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('become_method', type=str, help='become method', required=False) parser.add_argument('become_user', type=str, help='become user', required=False) args = parser.parse_args() host_pattern = args['host_pattern'] req_module = args['module'] module_args = args['module_args'] extra_vars = args['extra_vars'] inventory = args['inventory'] forks = args['forks'] verbose_level = args['verbose_level'] become = args['become'] become_method = args['become_method'] become_user = args['become_user'] module_args_string = '' extra_vars_string = '' curr_user = auth.username() if module_args: counter = 1 module_args_string += '-a"' for key in module_args.keys(): if counter < len(module_args): spacer = " " else: spacer = "" opt_string = str.format("{0}={1}{2}", key, module_args[key], spacer) module_args_string += opt_string counter += 1 module_args_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 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 forks: fork_string = str.format('-f {0}', str(forks)) else: fork_string = '' if verbose_level and verbose_level != 0: verb_counter = 1 verb_string = " -" while verb_counter <= verbose_level: verb_string += "v" verb_counter += 1 else: verb_string = '' if become: become_string = ' --become' else: become_string = '' if become_method: become_method_string = str.format(' --become-method={0}', become_method) else: become_method_string = '' if become_user: become_user_string = str.format(' --become-user={0}', become_user) else: become_user_string = '' command = str.format("ansible {9} -m {0} {1} {2} {3}{4}{5}{6}{7}{8}", req_module, module_args_string, fork_string, verb_string, become_string, become_method_string, become_user_string, inventory, extra_vars_string, host_pattern ) 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('host_pattern', type=str, help='need to specify host_pattern', required=True) parser.add_argument('module', type=str, help='module name', required=True) parser.add_argument('module_args', type=dict, help='module_args', required=False) parser.add_argument('extra_vars', type=dict, help='extra_vars', required=False) parser.add_argument('inventory', type=str, help='path to inventory', 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('become_method', type=str, help='become method', required=False) parser.add_argument('become_user', type=str, help='become user', required=False) args = parser.parse_args() host_pattern = args['host_pattern'] req_module = args['module'] module_args = args['module_args'] extra_vars = args['extra_vars'] inventory = args['inventory'] forks = args['forks'] verbose_level = args['verbose_level'] become = args['become'] become_method = args['become_method'] become_user = args['become_user'] module_args_string = '' extra_vars_string = '' curr_user = auth.username() if module_args: counter = 1 module_args_string += '-a"' for key in module_args.keys(): if counter < len(module_args): spacer = " " else: spacer = "" opt_string = str.format("{0}={1}{2}",key,module_args[key], spacer) module_args_string += opt_string counter += 1 module_args_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 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 forks: fork_string = str.format('-f {0}', str(forks)) else: fork_string = '' if verbose_level and verbose_level != 0: verb_counter = 1 verb_string = " -" while verb_counter <= verbose_level: verb_string += "v" verb_counter += 1 else: verb_string = '' if become: become_string = ' --become' else: become_string = '' if become_method: become_method_string = str.format(' --become-method={0}', become_method) else: become_method_string = '' if become_user: become_user_string = str.format(' --become-user={0}', become_user) else: become_user_string = '' command = str.format("ansible {9} -m {0} {1} {2} {3}{4}{5}{6}{7}{8}", req_module, module_args_string, fork_string, verb_string, become_string, become_method_string, become_user_string, inventory, extra_vars_string ,host_pattern) 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