def exec_playbook(): # Leveraging the Python 2.0 API for Executing ansible-playbook loader = DataLoader() inventory = InventoryManager(loader=loader, sources="") variable_manager = VariableManager(loader=loader, inventory=inventory) # Set playbook path # If current OS is Mac if platform.system() == 'Darwin': playbook_path = 'brewplay.yml' # If current OS is Linux elif platform.system() == 'Linux': playbook_path = 'packageplay.yml' # If current OS is windows elif platform.system() == 'Windows': playbook_path = 'winplay.yml' # Print to error if playbook doesnt exist at the path required if not os.path.exists(playbook_path): print '[INFO] The playbook does not exist' sys.exit() Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'host_key_checking', 'check', 'diff' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='local', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=False, become_method='sudo', become_user='******', verbosity=0, host_key_checking=False, check=False, diff=False) variable_manager.extra_vars = { 'hosts': 'mywebserver' } # This can accomodate various other command line arguments.` passwords = {} pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run()
variable_manager = VariableManager() loader = DataLoader() options = Options(connection='local', forks=100, become=None, become_method=None, become_user=None, check=False, listhosts=False, listtasks=False, listtags=False, syntax=False, module_path="") passwords = dict(vault_pass='******') inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost') variable_manager.set_inventory(inventory) playbooks = ["./test.yaml"] executor = PlaybookExecutor(playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) executor.run()
def ansible(): """Run Ansible playbook""" # initialize needed objects loader = DataLoader() # create inventory, use path to host config file as source or hosts in a comma separated string inventory = InventoryManager(loader=loader, sources='hosts') # variable manager takes care of merging all the different sources to give you a unifed view of variables available in each context variable_manager = VariableManager(loader=loader, inventory=inventory) # variable_manager.extra_vars = {'ansible_user': '******', 'ansible_port': '5986', 'ansible_connection': 'local', # 'ansible_password': '******'} # Here are the variables used in the playbook passwords = dict(vault_pass='******') # since API is constructed for CLI it expects certain options to always be set, named tuple 'fakes' the args parsing options object Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'diff' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='local', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False, diff=False) # Instantiate our ResultCallback for handling results as they come in. Ansible expects this to be one of its main display outlets results_callback = ResultCallback() playbook_path = 'myplaybook.yml' if not os.path.exists(playbook_path): raise AnsibleFileNotFound("Playbook %s does not exist" % playbook_path) pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) # Use our json callback instead of the ``default`` callback plugin, which prints to stdout pbex._tqm._stdout_callback = results_callback try: results = pbex.run() except AnsibleParserError as e: raise AnsibleError(e) # Return a json representation of the playbook run return jsonify( {'Playbook Results': [Task for Task in results_callback.results]})
def ansiblePlaybook(mainTfDir, baseCWD, providerName, kubeconfig, noTerraform, test, configs, usePrivateIPs, freeMaster): """Runs ansible-playbook with the given playbook. Parameters: mainTfDir (str): Path where the .tf file is. baseCWD (str): Path to go back. providerName (str): Provider name. kubeconfig (str): Path to kubeconfig file. noTerraform (bool): Specifies whether current run uses terraform. test (str): Cluster identification. configs (dict): Content of configs.yaml. usePrivateIPs (bool): Indicates whether private IPs should be used. freeMaster (bool): If True, pods can't run on the master node. Returns: int: 0 for success, 1 for failure """ btspMsg = "...bootstraping Kubernetes cluster..." writeToFile("src/logging/%s" % test, btspMsg, True) hostsFilePath = "%s/hosts" % mainTfDir createHostsFile(mainTfDir, baseCWD, providerName, hostsFilePath, configs, usePrivateIPs, noTerraform=noTerraform, test=test) loader = DataLoader() masterIP = getMasterIP(hostsFilePath) context.CLIARGS = ImmutableDict( tags={}, private_key_file=configs["pathToKey"], connection='ssh', timeout=120, remote_user=tryTakeFromYaml(configs, "openUser", "root"), become_method='sudo', ssh_common_args='-o StrictHostKeyChecking=no', extra_vars=[{ 'kubeconfig': kubeconfig, 'masterIP': masterIP }], forks=100, verbosity=4, #True, listtags=False, listtasks=False, listhosts=False, syntax=False, check=False, start_at_task=None) inventory = InventoryManager(loader=loader, sources=hostsFilePath) variable_manager = VariableManager( loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False)) # ----- to hide ansible logs if aggregateLogs: p = Process(target=subprocPrint, args=(test, )) p.start() with open(ansibleLogs % test, 'a') as f: # from now on, logs go to ansibleLogs with contextlib.redirect_stdout(f): with contextlib.redirect_stderr(f): playbooksArray = [playbookPath] if not freeMaster: playbooksArray.append( "src/provisionment/playbooks/allowMasterRuns.yaml") # --------------- GPU support if test in ("dlTest", "proGANTest"): playbooksArray.append( "src/provisionment/playbooks/gpuSupport.yaml") # --------------- MPI support if test in ("dlTest", "hpcTest"): playbooksArray.append( "src/provisionment/playbooks/kubeflow_mpiOperator.yaml" ) # --------------- OCI's Grow File System if providerName == "oci": playbooksArray.append( "src/provisionment/playbooks/oci_growfs.yaml") res = PlaybookExecutor(playbooks=playbooksArray, inventory=inventory, variable_manager=variable_manager, loader=loader, passwords=None).run(), masterIP if aggregateLogs: p.terminate() return res
def run_playbook(): with open('config_nodes.yml') as f: conf = yaml.load(f) mykey = conf['master']['key'] + '.key' import inventory as inventory master_public = inventory.get_master_ip() os.system('ssh-keygen -R {}'.format(master_public)) for i in range(1000): time.sleep(1) check = os.system( "ssh -i {0} -o StrictHostKeyChecking=no ubuntu@{1} 'exit' ".format( mykey, master_public)) if check == 0: os.system('ssh-keyscan -H {} >> ~/.ssh/known_hosts'.format( master_public)) break if check != 0: print('\nHostname {} is not up!\n'.format(master_public)) sys.exit() variable_manager = VariableManager() loader = DataLoader() passwords = {} inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='inventory.conf') playbook_path = 'master.yml' Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=mykey, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='******', verbosity=None, check=False) variable_manager.set_inventory(inventory) pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run() return results
def __init__( self, hosts=C.DEFAULT_HOST_LIST, # a list or dynamic-hosts, # default is /etc/ansible/hosts playbook_path=None, # * a playbook file forks=C.DEFAULT_FORKS, listtags=False, listtasks=False, listhosts=False, syntax=False, module_path=None, remote_user='******', timeout=C.DEFAULT_TIMEOUT, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user="******", verbosity=None, extra_vars=None, connection_type="ssh", passwords=None, private_key_file=C.DEFAULT_PRIVATE_KEY_FILE, remote_pass=C.DEFAULT_REMOTE_PASS, remote_port=None, check=False): C.RETRY_FILES_ENABLED = False self.callbackmodule = CallbackModule() if playbook_path is None or not os.path.exists(playbook_path): raise AnsibleError("Not Found the playbook file: %s." % playbook_path) self.playbook_path = playbook_path self.loader = DataLoader() self.variable_manager = VariableManager() self.passwords = passwords or {} # save host to file if not os.path.exists(os.path.join(base_dir, "tmp_inventory")): os.mkdir(os.path.join(base_dir, "tmp_inventory")) self.file_name = os.path.join(base_dir, "tmp_inventory", "tmp_host_{0}".format(time.time())) with open(self.file_name, 'w') as f: f.write("\n".join(hosts)) self.passwords = passwords or {} self.inventory = InventoryManager(loader=self.loader, sources=self.file_name) self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) self.options = Options(listtags=listtags, listtasks=listtasks, listhosts=listhosts, syntax=syntax, timeout=timeout, connection=connection_type, module_path=module_path, forks=forks, remote_user=remote_user, remote_pass=remote_pass, remote_port=remote_port, private_key_file=private_key_file, ssh_common_args=ssh_common_args or "", ssh_extra_args=ssh_extra_args or "", sftp_extra_args=sftp_extra_args, scp_extra_args=scp_extra_args, become=become, become_method=become_method, become_user=become_user, verbosity=verbosity, extra_vars=extra_vars or [], check=check, diff=False) self.runner = PlaybookExecutor(playbooks=[self.playbook_path], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=self.passwords) if self.runner._tqm: self.runner._tqm._stdout_callback = self.callbackmodule
ask_value_pass=False, verbosity=None, check=False, listhosts=False, listtasks=False, listtags=False, syntax=False, diff=True) # Playbook 方式执行 passwords = dict() # 创建playbook对象 playbook = PlaybookExecutor( playbooks=[ playbook_path, ], inventory=Inventory, variable_manager=Variable, loader=Loader, options=options, passwords=passwords, ) from callback import PlayBookResultsCollector callback = PlayBookResultsCollector() playbook._tqm._stdout_callback = callback playbook.run() all_result = { 'success': {}, 'failed': {}, 'unreachable': {}, 'skipped': {}, 'status': {},
def process_playbooks(playbooks_to_run, bitbucket_user, target_ip, hostname, ansible_ssh_pass): playbooks = set(playbooks_to_run) bitbucket = "https://" + bitbucket_user + "@stash.veritas.com/scm/iac/" ansible_user = "******" directory = "/home/" + getpass.getuser() + '/runPlaybooks' + str(os.getpid()) inventory = "jenkins.inv" run_command = "ansible-playbook -i " + inventory + " local.yml" pull_command = "ansible-galaxy install -r requirements.yml" clone_command = "git clone " pattern_mint = "playbook-mint" prog = re.compile(pattern_mint) create a folder to run multiple playbooks try: os.system("mkdir " + directory) os.chdir(directory) except Exception as e: raise Exception("Failed to create diretory: %s" % str(e)) sys.exit() # pull playbooks try: for play in playbooks: clone_playbook = bitbucket + play + ".git" os.system(clone_command + clone_playbook) except Exception as e: raise Exception("Failed to clone playbooks: %s" % str(e)) sys.exit() # pull roles for playbooks try: for play in playbooks: os.chdir(play) print("in dir: " + play) os.system(pull_command) os.chdir(directory) except Exception as e: raise Exception("Failed to pull roles: %s" % str(e)) sys.exit() # invertory and run playbooks Options = namedtuple('Options', ['connection', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'module_path']) variable_manager = VariableManager() loader = DataLoader() options = Options(connection='local', forks=100, become=None, become_method=None, become_user=None, check=False, listhosts=False, listtasks=False, listtags=False, syntax=False, module_path="") passwords = ansible_ssh_pass #dict(vault_pass='******') inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=target_ip)#'localhost') variable_manager.set_inventory(inventory) playbooks = playbooks_to_run #["./test.yaml"] executor = PlaybookExecutor( playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) executor.run()
def test_playbook_executor__get_serialized_batches(self): fake_loader = DictDataLoader({ 'no_serial.yml': ''' - hosts: all gather_facts: no tasks: - debug: var=inventory_hostname ''', 'serial_int.yml': ''' - hosts: all gather_facts: no serial: 2 tasks: - debug: var=inventory_hostname ''', 'serial_pct.yml': ''' - hosts: all gather_facts: no serial: 20% tasks: - debug: var=inventory_hostname ''', }) mock_inventory = MagicMock() mock_var_manager = MagicMock() # fake out options to use the syntax CLI switch, which will ensure # the PlaybookExecutor doesn't create a TaskQueueManager mock_options = MagicMock() mock_options.syntax.value = True pbe = PlaybookExecutor( playbooks=['no_serial.yml', 'serial_int.yml', 'serial_pct.yml'], inventory=mock_inventory, variable_manager=mock_var_manager, loader=fake_loader, options=mock_options, passwords=[], ) playbook = Playbook.load(pbe._playbooks[0], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] mock_inventory.get_hosts.return_value = [ 'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9' ] self.assertEqual(pbe._get_serialized_batches(play), [[ 'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9' ]]) playbook = Playbook.load(pbe._playbooks[1], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] mock_inventory.get_hosts.return_value = [ 'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9' ] self.assertEqual( pbe._get_serialized_batches(play), [['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9']]) playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] mock_inventory.get_hosts.return_value = [ 'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9' ] self.assertEqual( pbe._get_serialized_batches(play), [['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9']])
def run_playbook(self, pb, kolla, action): cluster_id = self.json_entity[0]["cluster_id"] parameters = self.json_entity[0]["parameters"] self.pbook_path = parameters[pb] pbook_dir = os.path.dirname(self.pbook_path) inv_dir = pbook_dir + '/inventory/' ev = None try: if kolla: inv_file = inv_dir + cluster_id + "_kolla.inv" inv_dict = parameters["kolla_inv"] kolla_pwds = parameters['kolla_passwords'] kolla_vars = parameters['kolla_globals'] self.create_kolla_param_files(kolla_pwds, kolla_vars, pbook_dir) ev = {'action': action} with open(pbook_dir + '/../etc/kolla/globals.yml') as info: ev.update(yaml.load(info)) with open(pbook_dir + '/../etc/kolla/passwords.yml') as info: ev.update(yaml.load(info)) else: inv_file = inv_dir + cluster_id + ".inv" inv_dict = parameters["inventory"] self.current_status = self.validate_provision_params( inv_dict, self.args) Options = namedtuple('Options', [ 'connection', 'forks', 'module_path', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'verbosity', 'extra_vars' ]) self.options = Options(connection='ssh', forks=100, module_path=None, become=True, become_method='sudo', become_user='******', check=False, listhosts=None, listtasks=None, listtags=None, syntax=None, verbosity=None, extra_vars=ev) self.logger.log( self.logger.INFO, "Creating inventory %s for playbook %s" % (inv_file, self.pbook_path)) SMAnsibleUtils(None).create_inv_file(inv_file, inv_dict) self.logger.log( self.logger.INFO, "Created inventory %s for playbook %s" % (inv_file, self.pbook_path)) self.var_mgr = VariableManager() self.inventory = Inventory(loader=DataLoader(), variable_manager=self.var_mgr, host_list=inv_file) self.var_mgr.set_inventory(self.inventory) if kolla: self.var_mgr.extra_vars = ev self.pb_executor = PlaybookExecutor(playbooks=[self.pbook_path], inventory=self.inventory, variable_manager=self.var_mgr, loader=DataLoader(), options=self.options, passwords={}) self.logger.log(self.logger.INFO, "Starting playbook %s" % self.pbook_path) # Update status before every playbook run if kolla: self.current_status = "openstack_" + action else: self.current_status = action self.update_status(kolla) rv = self.pb_executor.run() if rv != 0: self.current_status = STATUS_FAILED self.update_status(kolla) self.logger.log(self.logger.ERROR, "Playbook Failed: %s" % self.pbook_path) rv = None else: rv = self.pb_executor._tqm._stats except Exception as e: self.logger.log(self.logger.ERROR, e) self.current_status = STATUS_FAILED self.update_status(kolla) rv = None return rv
def execute_playbook(self, playbook_info): output = None try: loader = DataLoader() inventory = InventoryManager(loader=loader, sources=['localhost']) variable_manager = VariableManager(loader=loader, inventory=inventory) Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'diff' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user=None, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False, diff=False) variable_manager.extra_vars = playbook_info['extra_vars'] pbex = PlaybookExecutor(playbooks=[playbook_info['uri']], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None) ret_val = pbex.run() output = self.get_plugin_output(pbex) if ret_val != 0: msg = MsgBundle.getMessage( MsgBundle.PLAYBOOK_RETURN_WITH_ERROR) raise Exception(msg) if output is None or output.get('status') is None: msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_OUTPUT_MISSING) raise Exception(msg) if output.get('status').lower() == "failure": msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_STATUS_FAILED) raise Exception(msg) return output except Exception as exp: msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_EXECUTE_ERROR, playbook_uri=playbook_info['uri'], execution_id=playbook_info['extra_vars'] ['playbook_input']['job_execution_id'], exc_msg=repr(exp)) if exp.message: msg = msg + "\n" + exp.message JM_LOGGER.error(msg) # after handling exception, write an END # to stop listening to the file if created unique_pb_id = playbook_info['extra_vars']['playbook_input'][ 'unique_pb_id'] exec_id = playbook_info['extra_vars']['playbook_input'][ 'job_execution_id'] self._job_file_write.write_to_file(exec_id, unique_pb_id, JobFileWrite.PLAYBOOK_OUTPUT, json.dumps(output)) with open("/tmp/" + exec_id, "a") as f: f.write(unique_pb_id + 'END' + PLAYBOOK_EOL_PATTERN) sys.exit(msg)
def ansibleFuntion(): global peticion global nombre_path loader = DataLoader() inventory = InventoryManager( loader=loader, sources= '/home/labicsc02-65961/Documentos/Asinble-server/Inventory/inventory') variable_manager = VariableManager(loader=loader, inventory=inventory) # si lo que se quiere trabajar es un archivo se debe especificar el nombre del file y la extension del file if (peticion == 'eliminarCarpeta'): playbook_path = '/home/labicsc02-65961/Documentos/Asinble-server/Playbooks/playbook_eliminarCarpeta.yml' elif (peticion == 'eliminarArchivo'): playbook_path = '/home/labicsc02-65961/Documentos/Asinble-server/Playbooks/playbook_eliminarFile.yml' elif (peticion == 'crearArchivoTXT'): playbook_path = '/home/labicsc02-65961/Documentos/Asinble-server/Playbooks/playbook_crearArchivoTXT.yml' elif (peticion == 'crearCarpeta'): playbook_path = '/home/labicsc02-65961/Documentos/Asinble-server/Playbooks/playbook_crearCarpeta.yml' if not os.path.exists(playbook_path): print('[INFO] The playbook does not exist') sys.exit() Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'diff' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file='/home/labicsc02-65961/.ssh/id_rsa', ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method='sudo', become_user='******', verbosity=None, check=False, diff=False) variable_manager.extra_vars = { 'hosts': 'all', 'path': nombre_path } # This can accomodate various other command line arguments.` passwords = dict(vault_pass='******') pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run()
def __init__(self, options={}, playbooks=[], loader=DataLoader(), result_callback=None, passwords=None, inventory=None): self._Options = namedtuple('Options', self.option_fields) self._options = None self.set_options(**options) if ((self._options.su or self._options.su_user) and (self._options.sudo or self._options.sudo_user) or (self._options.su or self._options.su_user) and (self._options.become or self._options.become_user) or (self._options.sudo or self._options.sudo_user) and (self._options.become or self._options.become_user)): raise Exception( "Sudo options ('sudo', 'sudo_user', and 'ask_sudo_pass') " "and su options ('su', 'su_user', and 'ask_su_pass') " "and become options ('become', 'become_user', and 'ask_become_pass')" " are exclusive of each other" ) if (self._options.ask_vault_pass and self._options.vault_password_file): raise Exception( "'ask_vault_pass' and 'vault_password_file' are mutually exclusive" ) if self._options.forks < 1: raise Exception('The number of processes (--forks) must be >= 1') display.verbosity = self._options.verbosity self._loader = loader self._variable_manager = VariableManager() self._variable_manager.extra_vars = load_extra_vars( loader=self._loader, options=self._options) print(self._variable_manager.extra_vars) self._variable_manager.option_vars = load_options_vars(self._options) self._result_callback = result_callback self._inventory = inventory self._passwords = passwords self._playbooks = [] for pb in playbooks: if os.path.isabs(pb): self._playbooks.append(pb) else: pbdir = os.path.dirname(__file__) self._playbooks.append(os.path.join(pbdir, pb)) if inventory is None: self._inventory = Inventory( loader=self._loader, variable_manager=self._variable_manager, host_list=self._options.inventory) self._pbex = PlaybookExecutor( playbooks=self._playbooks, inventory=self._inventory, variable_manager=self._variable_manager, loader=self._loader, options=self._options, passwords=self._passwords)
def run_playbook(self, playbook_yml, group=None, script=None): """ 运行 playbook """ playbook = None try: with open(playbook_yml) as f: playbook_content = f.read() check, variable = check_ansible_variable(playbook_content) if check: data = '<code style="color: #FF0000">playbook 中包含非法 ansible 变量: [{}],禁止运行</code>'.format( variable) data2 = '\033[01;31mplaybook 中包含非法 ansible 变量: [{}],禁止运行\r\n\r\n\033[0m'.format( variable) delay = round(time.time() - self.results_callback.start_time, 6) self.results_callback.res.append( json.dumps([delay, 'o', data2])) message = dict() message['status'] = 0 message['message'] = data message = json.dumps(message) async_to_sync(channel_layer.group_send)(group, { "type": "send.message", "text": message, }) else: playbook = PlaybookExecutor( playbooks=[playbook_yml], inventory=self.dynamic_inventory, variable_manager=self.variable_manager, loader=self.loader, passwords=self.passwords, ) playbook._tqm._stdout_callback = self.results_callback playbook.run() except Exception as err: data = '<code style="color: #FF0000">{}</code>'.format(str(err)) data2 = '\033[01;31m{}\r\n\r\n\033[0m'.format( str(err).strip().replace('\n', '\r\n')) delay = round(time.time() - self.results_callback.start_time, 6) self.results_callback.res.append(json.dumps([delay, 'o', data2])) message = dict() message['status'] = 0 message['message'] = data message = json.dumps(message) async_to_sync(channel_layer.group_send)(group, { "type": "send.message", "text": message, }) finally: if group: message = dict() message['status'] = 0 message['message'] = '执行完毕...' message = json.dumps(message) async_to_sync(channel_layer.group_send)(group, { "type": "close.channel", "text": message, }) if self.results_callback.res: save_res(self.results_callback.res_file, self.results_callback.res) batchcmd_log( user=self.results_callback.user, hosts=self.results_callback.hosts, cmd=self.results_callback.playbook, detail=self.results_callback.res_file, address=self.results_callback.client, useragent=self.results_callback.user_agent, start_time=self.results_callback.start_time_django, type=4, script=script, ) if playbook._tqm is not None: playbook._tqm.cleanup()
def on_configuration(self, ch, method, props, response): if props.app_id != self.specific_manager_id: LOG.info('Start retrieving the IP address ...') response = yaml.load(str(response)) list = response['VNFR'] mgmt_ip = None vm_image = 'http://files.sonata-nfv.eu/son-psa-pilot/vpn-vnf/' \ 'sonata-vpn.qcow2' for x in range(len(list)): if (response['VNFR'][x]['virtual_deployment_units'][0] ['vm_image']) == vm_image: mgmt_ip = (response['VNFR'][x]['virtual_deployment_units'] [0]['vnfc_instance'][0]['connection_points'][0] ['type']['address']) if not mgmt_ip: LOG.error("Couldn't obtain IP address from VNFR") return # send the status to the SMR (not necessary) self.manoconn.publish(topic='specific.manager.registry.ssm.status', message=yaml.dump({ 'name': self.specific_manager_id, 'status': "IP address:'{0}'".format(mgmt_ip) })) LOG.info("IP address:'{0}'".format(mgmt_ip)) self.manoconn.notify(topic=self.amqp_topic, msg=yaml.dump({ 'name': self.specific_manager_id, 'IP': mgmt_ip })) # configure vm using ansible playbook variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager) playbook_path = 'fsm/vpn-config/ansible/site.yml' if not os.path.exists(playbook_path): LOG.error('The playbook does not exist') return Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='******', verbosity=None, check=False) variable_manager.extra_vars = {'hosts': mgmt_ip} passwords = {} pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run() return
from ansible.inventory.manager import InventoryManager from ansible.parsing.dataloader import DataLoader from ansible.vars.manager import VariableManager from collections import namedtuple loader = DataLoader() hosts_path = InventoryManager(loader=loader, sources=sys.argv[1]) variable_manager = VariableManager(loader=loader, inventory=hosts) playbook_path = sys.argv[2] Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check','diff' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method='sudo', become_user='******', verbosity=None, check=False, diff=False) play = PlaybookExecutor(playbooks=[playbook_path], inventory=hosts_path, variable_manager=variable_manager, loader=loader, options=options, passwords={}) results = play.run()
def test_get_serialized_batches(self): fake_loader = DictDataLoader({ 'no_serial.yml': ''' - hosts: all gather_facts: no tasks: - debug: var=inventory_hostname ''', 'serial_int.yml': ''' - hosts: all gather_facts: no serial: 2 tasks: - debug: var=inventory_hostname ''', 'serial_pct.yml': ''' - hosts: all gather_facts: no serial: 20% tasks: - debug: var=inventory_hostname ''', 'serial_list.yml': ''' - hosts: all gather_facts: no serial: [1, 2, 3] tasks: - debug: var=inventory_hostname ''', 'serial_list_mixed.yml': ''' - hosts: all gather_facts: no serial: [1, "20%", -1] tasks: - debug: var=inventory_hostname ''', }) mock_inventory = MagicMock() mock_var_manager = MagicMock() templar = Templar(loader=fake_loader) pbe = PlaybookExecutor( playbooks=['no_serial.yml', 'serial_int.yml', 'serial_pct.yml', 'serial_list.yml', 'serial_list_mixed.yml'], inventory=mock_inventory, variable_manager=mock_var_manager, loader=fake_loader, passwords=[], ) playbook = Playbook.load(pbe._playbooks[0], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] play.post_validate(templar) mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] self.assertEqual(pbe._get_serialized_batches(play), [['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']]) playbook = Playbook.load(pbe._playbooks[1], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] play.post_validate(templar) mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] self.assertEqual( pbe._get_serialized_batches(play), [['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9']] ) playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] play.post_validate(templar) mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] self.assertEqual( pbe._get_serialized_batches(play), [['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9']] ) playbook = Playbook.load(pbe._playbooks[3], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] play.post_validate(templar) mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] self.assertEqual( pbe._get_serialized_batches(play), [['host0'], ['host1', 'host2'], ['host3', 'host4', 'host5'], ['host6', 'host7', 'host8'], ['host9']] ) playbook = Playbook.load(pbe._playbooks[4], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] play.post_validate(templar) mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9'] self.assertEqual(pbe._get_serialized_batches(play), [['host0'], ['host1', 'host2'], ['host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']]) # Test when serial percent is under 1.0 playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] play.post_validate(templar) mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2'] self.assertEqual(pbe._get_serialized_batches(play), [['host0'], ['host1'], ['host2']]) # Test when there is a remainder for serial as a percent playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader) play = playbook.get_plays()[0] play.post_validate(templar) mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9', 'host10'] self.assertEqual( pbe._get_serialized_batches(play), [['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'], ['host6', 'host7'], ['host8', 'host9'], ['host10']] )
def run_playbook(self, playbook_file, playbook_variables=None, option_overrides=None): """ Run a playbook from file with the variables provided. :param playbook_file: the location of the playbook :param playbook_variables: extra variables for the playbook """ # on the first run, the playbook that initializes the # inventory will not have yet run, so we should ensure # that the directory for it exists, at the least, so # ansible doesn't complain if not exists(self.host_list): mkdir(self.host_list) variable_manager = VariableManager() data_loader = DataLoader() inventory = Inventory(loader=data_loader, variable_manager=variable_manager, host_list=self.host_list) variable_manager.set_inventory(inventory) variable_manager.extra_vars = playbook_variables # until Ansible's display logic is less hack-ey we need # to mutate their global in __main__ options = self.generate_playbook_options(playbook_file) display.verbosity = options.verbosity # we want to log everything so we can parse output # nicely later from files and don't miss output due # to the pretty printer, if it's on from ..oct import __file__ as root_dir callback_loader.add_directory( join(dirname(root_dir), 'ansible', 'oct', 'callback_plugins')) constants.DEFAULT_CALLBACK_WHITELIST = [ 'log_results', 'generate_junit' ] environ['ANSIBLE_LOG_ROOT_PATH'] = self.log_directory if options.verbosity == 1: # if the user has not asked for verbose output # we will use our pretty printer for progress # on the TTY constants.DEFAULT_STDOUT_CALLBACK = 'pretty_progress' # we really don't want output in std{err,out} # that we didn't put there, but some code in # Ansible calls directly to the Display, not # through a callback, so we need to ensure # that those raw calls don't go to stdout display.display = partial(display.display, log_only=True) else: # if the user asks for verbose output, we want # to give them nicer output than the default # plugin, anyway constants.DEFAULT_STDOUT_CALLBACK = 'default_with_output_lists' if option_overrides: for key in option_overrides: if hasattr(options, key): setattr(options, key, option_overrides[key]) result = PlaybookExecutor(playbooks=[playbook_file], inventory=inventory, variable_manager=variable_manager, loader=data_loader, options=options, passwords=None).run() if result != TaskQueueManager.RUN_OK: # TODO: this seems bad, but can we discover the thread here to join() it? sleep(0.2) raise ClickException('Playbook execution failed with code ' + str(result))
def run_script(playbook_path, channel, inventory_sources=None, extra_arguments={}): Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'diff' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user=None, verbosity=True, check=False, diff=False) if not os.path.exists(playbook_path): print '[INFO] The playbook does not exist' sys.exit() loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader, variable_manager) variable_manager.extra_vars = extra_arguments # This can accomodate various other command line arguments.` passwords = {} pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) pbex.run() stats = pbex._tqm._stats hosts = sorted(stats.processed.keys()) run_success = True for h in hosts: t = stats.summarize(h) if t['unreachable'] > 0 or t['failures'] > 0: run_success = False send_message(channel, '```{}```'.format(str(t))) pbex._tqm.send_callback('notify_slack', channel) if run_success: send_message(channel, '```Execution is completed. Please check the result```')
def run_playbook(name, **kwargs): kwargs["cluster_name"] = "my_name_keystone_kong" kwargs["n_slaves"] = "32" #kwargs["global_db"] = "postgresql" kwargs["ansible_ssh_private_key_file"] = "~/.ssh/my_name_key.key" ansible_dir = "/home/%s/keystone_full_deployment/ansible" % getuser() loader = DataLoader() global variable_manager global inventory if inventory is None: variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list="%s/openstack_inventory.py" % ansible_dir) variable_manager.set_inventory(inventory) playbook_path = '/home/%s/keystone_full_deployment/ansible/%s.yml' % ( getuser(), name) if not os.path.exists(playbook_path): print '[INFO] The playbook does not exist' sys.exit() Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user=_user, private_key_file='/home/%s/.ssh/id_rsa' % getuser(), ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method="sudo", become_user="******", verbosity=None, check=False) variable_manager.extra_vars = kwargs # This can accomodate various other command line arguments.` passwords = dict(vault_pass="******" % _password, sudo_pass="******" % _password, become_pass="******" % _password) pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run()
def run(self): super(PlaybookCLI, self).run() # Note: slightly wrong, this is written so that implicit localhost # Manage passwords sshpass = None becomepass = None vault_pass = None passwords = {} # don't deal with privilege escalation or passwords when we don't need to if not self.options.listhosts and not self.options.listtasks and not self.options.listtags and not self.options.syntax: self.normalize_become_options() (sshpass, becomepass) = self.ask_passwords() passwords = { 'conn_pass': sshpass, 'become_pass': becomepass } loader = DataLoader() if self.options.vault_password_file: # read vault_pass from a file vault_pass = CLI.read_vault_password_file(self.options.vault_password_file, loader=loader) loader.set_vault_password(vault_pass) elif self.options.ask_vault_pass: vault_pass = self.ask_vault_passwords()[0] loader.set_vault_password(vault_pass) # initial error check, to make sure all specified playbooks are accessible # before we start running anything through the playbook executor for playbook in self.args: if not os.path.exists(playbook): raise AnsibleError("the playbook: %s could not be found" % playbook) if not (os.path.isfile(playbook) or stat.S_ISFIFO(os.stat(playbook).st_mode)): raise AnsibleError("the playbook: %s does not appear to be a file" % playbook) # create the variable manager, which will be shared throughout # the code, ensuring a consistent view of global variables variable_manager = VariableManager() variable_manager.extra_vars = load_extra_vars(loader=loader, options=self.options) # create the inventory, and filter it based on the subset specified (if any) inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.options.inventory) variable_manager.set_inventory(inventory) # (which is not returned in list_hosts()) is taken into account for # warning if inventory is empty. But it can't be taken into account for # checking if limit doesn't match any hosts. Instead we don't worry about # limit if only implicit localhost was in inventory to start with. # # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts()) no_hosts = False if len(inventory.list_hosts()) == 0: # Empty inventory display.warning("provided hosts list is empty, only localhost is available") no_hosts = True inventory.subset(self.options.subset) if len(inventory.list_hosts()) == 0 and no_hosts is False: # Invalid limit raise AnsibleError("Specified --limit does not match any hosts") # create the playbook executor, which manages running the plays via a task queue manager pbex = PlaybookExecutor(playbooks=self.args, inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.options, passwords=passwords) results = pbex.run() if isinstance(results, list): for p in results: display.display('\nplaybook: %s' % p['playbook']) for idx, play in enumerate(p['plays']): msg = "\n play #%d (%s): %s" % (idx + 1, ','.join(play.hosts), play.name) mytags = set(play.tags) msg += '\tTAGS: [%s]' % (','.join(mytags)) if self.options.listhosts: playhosts = set(inventory.get_hosts(play.hosts)) msg += "\n pattern: %s\n hosts (%d):" % (play.hosts, len(playhosts)) for host in playhosts: msg += "\n %s" % host display.display(msg) all_tags = set() if self.options.listtags or self.options.listtasks: taskmsg = '' if self.options.listtasks: taskmsg = ' tasks:\n' def _process_block(b): taskmsg = '' for task in b.block: if isinstance(task, Block): taskmsg += _process_block(task) else: if task.action == 'meta': continue all_tags.update(task.tags) if self.options.listtasks: cur_tags = list(mytags.union(set(task.tags))) cur_tags.sort() if task.name: taskmsg += " %s" % task.get_name() else: taskmsg += " %s" % task.action taskmsg += "\tTAGS: [%s]\n" % ', '.join(cur_tags) return taskmsg all_vars = variable_manager.get_vars(loader=loader, play=play) play_context = PlayContext(play=play, options=self.options) for block in play.compile(): block = block.filter_tagged_tasks(play_context, all_vars) if not block.has_tasks(): continue taskmsg += _process_block(block) if self.options.listtags: cur_tags = list(mytags.union(all_tags)) cur_tags.sort() taskmsg += " TASK TAGS: [%s]\n" % ', '.join(cur_tags) display.display(taskmsg) return 0 else: return results
def __init__(self, hosts=None, playbook_path=None, forks=C.DEFAULT_FORKS, listtags=False, listtasks=False, listhosts=False, syntax=False, module_path=None, remote_user='******', timeout=C.DEFAULT_TIMEOUT, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user="******", verbosity=None, extra_vars=None, connection_type="ssh", passwords=None, private_key_file=None, check=False): C.RETRY_FILES_ENABLED = False self.callbackmodule = PlaybookResultCallBack() if playbook_path is None or not os.path.exists(playbook_path): raise AnsibleError( "Not Found the playbook file: %s." % playbook_path) self.playbook_path = playbook_path self.loader = DataLoader() self.variable_manager = VariableManager() self.passwords = passwords or {} self.inventory = JMSInventory(hosts) self.options = self.Options( listtags=listtags, listtasks=listtasks, listhosts=listhosts, syntax=syntax, timeout=timeout, connection=connection_type, module_path=module_path, forks=forks, remote_user=remote_user, private_key_file=private_key_file, ssh_common_args=ssh_common_args or "", ssh_extra_args=ssh_extra_args or "", sftp_extra_args=sftp_extra_args, scp_extra_args=scp_extra_args, become=become, become_method=become_method, become_user=become_user, verbosity=verbosity, extra_vars=extra_vars or [], check=check ) self.variable_manager.extra_vars = load_extra_vars(loader=self.loader, options=self.options) self.variable_manager.options_vars = load_options_vars(self.options) self.variable_manager.set_inventory(self.inventory) # 初始化playbook的executor self.runner = PlaybookExecutor( playbooks=[self.playbook_path], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=self.passwords) if self.runner._tqm: self.runner._tqm._stdout_callback = self.callbackmodule
def playbook(**kwargs): group_vars = kwargs['group_vars'] groups = kwargs['groups'] host_list = kwargs['host_list'] playbook_basedir = os.sep.join( [ANSIBLE_PLAYBOOK_PATH, kwargs['playbook_basedir']]) playbooks = [] for pb in kwargs['playbooks']: playbooks.append(os.sep.join([playbook_basedir, pb])) job_id = kwargs['job_id'] loader = DataLoader() vars = VariableManager() # 指定inventory为一个目录,设置所有主机,包含group和host invertory = Inventory(loader, vars, host_list=host_list) invertory.set_playbook_basedir(playbook_basedir) for group_name, hosts in groups.items(): t_group = Group(group_name) for host in hosts: t_host = Host(host) t_group.add_host(t_host) invertory.add_group(t_group) vars.set_inventory(invertory) display = LogDisplay(logname=job_id) callback = CALLBACKMODULE[CALLBACK](display=display) Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'timeout', 'remote_user', 'ask_pass', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'ask_value_pass', 'verbosity', 'check', 'listhosts', 'listtags', 'listtasks', 'syntax' ]) options = Options(connection='smart', module_path='/usr/share/ansible', forks=100, timeout=10, remote_user='******', ask_pass=False, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user='******', ask_value_pass=False, verbosity=None, check=False, listhosts=None, listtags=None, listtasks=None, syntax=None) passwords = dict() pb_executor = PlaybookExecutor(playbooks, invertory, vars, loader, options, passwords) pb_executor._tqm._stdout_callback = callback pb_executor.run() return display.get_log_json()
def execute_playbook(self, playbook_info): try: loader = DataLoader() inventory = InventoryManager(loader=loader, sources=['localhost']) variable_manager = VariableManager(loader=loader, inventory=inventory) Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'diff' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False, diff=False) variable_manager.extra_vars = playbook_info['extra_vars'] pbex = PlaybookExecutor(playbooks=[playbook_info['uri']], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None) ret_val = pbex.run() if ret_val != 0: msg = MsgBundle.getMessage( MsgBundle.PLAYBOOK_RETURN_WITH_ERROR) raise Exception(msg) output = self.get_plugin_output(pbex) if output is None or output.get('status') is None: msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_OUTPUT_MISSING) raise Exception(msg) if output.get('status').lower() == "failure": msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_STATUS_FAILED) raise Exception(msg) return output except Exception as exp: msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_EXECUTE_ERROR, playbook_uri=playbook_info['uri'], execution_id=playbook_info['extra_vars'] ['playbook_input']['job_execution_id'], exc_msg=repr(exp)) if exp.message: msg = msg + "\n" + exp.message JM_LOGGER.error(msg) sys.exit(msg)
def __init__(self, playbook, hosts=None,subset=None,tags=None): print "First tags is: %s" %tags if tags is None: tags='all' self.Options = namedtuple('Options', ['connection', 'remote_user', 'ask_sudo_pass', 'verbosity', 'ack_pass', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'sudo_user', 'sudo', 'diff', 'subset', 'tags']) self.ops = self.Options(connection='smart', remote_user=None, ack_pass=None, sudo_user=None, forks=5, sudo=None, ask_sudo_pass=False, verbosity=5, module_path=None, become=None, become_method=None, become_user=None, check=False, diff=False, listhosts=None, listtasks=None, listtags=None, syntax=None, subset=[subset], tags=[tags]) self.loader = DataLoader() self.passwords = dict() self.results_callback = ResultCallback() self.inventory = InventoryManager(loader=self.loader, sources=[hosts]) if self.ops.subset != [None]: self.inventory.subset(self.ops.subset) self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) print "Tags is %s:"%self.ops.tags # setup playbook executor, but don't run until run() called self.pbex = PlaybookExecutor( playbooks=[playbook], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.ops, passwords=self.passwords)
def run_ansible(playbooks, inventory_path, extra_vars={}, tags=None, on_error_continue=False): extra_vars = extra_vars or {} variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_path) variable_manager.set_inventory(inventory) if extra_vars: variable_manager.extra_vars = extra_vars if tags is None: tags = [] passwords = {} # NOTE(msimonin): The ansible api is "low level" in the # sense that we are redefining here all the default values # that are usually enforce by ansible called from the cli Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'remote_user', 'verbosity', 'check', 'tags', 'pipelining' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method='sudo', become_user='******', remote_user=None, verbosity=2, check=False, tags=tags, pipelining=True) for path in playbooks: logging.info("Running playbook %s with vars:\n%s" % (path, extra_vars)) pbex = PlaybookExecutor(playbooks=[path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) code = pbex.run() stats = pbex._tqm._stats hosts = stats.processed.keys() result = [{h: stats.summarize(h)} for h in hosts] results = {'code': code, 'result': result, 'playbook': path} print(results) failed_hosts = [] unreachable_hosts = [] for h in hosts: t = stats.summarize(h) if t['failures'] > 0: failed_hosts.append(h) if t['unreachable'] > 0: unreachable_hosts.append(h) if len(failed_hosts) > 0: logging.error("Failed hosts: %s" % failed_hosts) if not on_error_continue: raise EnosFailedHostsError(failed_hosts) if len(unreachable_hosts) > 0: logging.error("Unreachable hosts: %s" % unreachable_hosts) if not on_error_continue: raise EnosUnreachableHostsError(unreachable_hosts)
def ansible_playbook(): playbook = ['tt.ym1'] pb = PlaybookExecutor(playbook=playbook, )
# -*-coding:utf-8 -*-
def run(self): super(PlaybookCLI, self).run() # Note: slightly wrong, this is written so that implicit localhost # Manage passwords sshpass = None becomepass = None passwords = {} # initial error check, to make sure all specified playbooks are accessible # before we start running anything through the playbook executor for playbook in self.args: if not os.path.exists(playbook): raise AnsibleError("the playbook: %s could not be found" % playbook) if not (os.path.isfile(playbook) or stat.S_ISFIFO(os.stat(playbook).st_mode)): raise AnsibleError( "the playbook: %s does not appear to be a file" % playbook) # don't deal with privilege escalation or passwords when we don't need to if not self.options.listhosts and not self.options.listtasks and not self.options.listtags and not self.options.syntax: self.normalize_become_options() (sshpass, becomepass) = self.ask_passwords() passwords = {'conn_pass': sshpass, 'become_pass': becomepass} loader, inventory, variable_manager = self._play_prereqs(self.options) # (which is not returned in list_hosts()) is taken into account for # warning if inventory is empty. But it can't be taken into account for # checking if limit doesn't match any hosts. Instead we don't worry about # limit if only implicit localhost was in inventory to start with. # # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts()) hosts = CLI.get_host_list(inventory, self.options.subset) # flush fact cache if requested if self.options.flush_cache: self._flush_cache(inventory, variable_manager) # create the playbook executor, which manages running the plays via a task queue manager pbex = PlaybookExecutor(playbooks=self.args, inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.options, passwords=passwords, callback=self.callback) results = pbex.run() if isinstance(results, list): for p in results: display.display('\nplaybook: %s' % p['playbook']) for idx, play in enumerate(p['plays']): if play._included_path is not None: loader.set_basedir(play._included_path) else: pb_dir = os.path.realpath( os.path.dirname(p['playbook'])) loader.set_basedir(pb_dir) msg = "\n play #%d (%s): %s" % (idx + 1, ','.join( play.hosts), play.name) mytags = set(play.tags) msg += '\tTAGS: [%s]' % (','.join(mytags)) if self.options.listhosts: playhosts = set(inventory.get_hosts(play.hosts)) msg += "\n pattern: %s\n hosts (%d):" % ( play.hosts, len(playhosts)) for host in playhosts: msg += "\n %s" % host display.display(msg) all_tags = set() if self.options.listtags or self.options.listtasks: taskmsg = '' if self.options.listtasks: taskmsg = ' tasks:\n' def _process_block(b): taskmsg = '' for task in b.block: if isinstance(task, Block): taskmsg += _process_block(task) else: if task.action == 'meta': continue all_tags.update(task.tags) if self.options.listtasks: cur_tags = list( mytags.union(set(task.tags))) cur_tags.sort() if task.name: taskmsg += " %s" % task.get_name( ) else: taskmsg += " %s" % task.action taskmsg += "\tTAGS: [%s]\n" % ', '.join( cur_tags) return taskmsg all_vars = variable_manager.get_vars(play=play) play_context = PlayContext(play=play, options=self.options) for block in play.compile(): block = block.filter_tagged_tasks( play_context, all_vars) if not block.has_tasks(): continue taskmsg += _process_block(block) if self.options.listtags: cur_tags = list(mytags.union(all_tags)) cur_tags.sort() taskmsg += " TASK TAGS: [%s]\n" % ', '.join( cur_tags) display.display(taskmsg) return 0 else: return results
def execute_playbook(self, playbook_info): try: loader = DataLoader() inventory = InventoryManager(loader=loader, sources=['localhost']) variable_manager = VariableManager(loader=loader, inventory=inventory) Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'diff' ]) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user=None, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False, diff=False) variable_manager.extra_vars = playbook_info['extra_vars'] pbex = PlaybookExecutor(playbooks=[playbook_info['uri']], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None) ret_val = pbex.run() if ret_val != 0: msg = MsgBundle.getMessage( MsgBundle.PLAYBOOK_RETURN_WITH_ERROR) raise Exception(msg) output = self.get_plugin_output(pbex) # if it comes here, it implies the pb has ended # So derive the playbook output to be # written to file and finally write END to the file try: unique_pb_id = playbook_input_json['extra_vars'][ 'playbook_input']['unique_pb_id'] exec_id = playbook_input_json['extra_vars']['playbook_input'][ 'job_execution_id'] # messages to be given to next playbooks(s) JM_LOGGER.info("Printing pb output results " "from pb_helper.py -->>>") JM_LOGGER.info(output) line_in_file = unique_pb_id + 'PLAYBOOK_OUTPUT##' + \ json.dumps(output) + 'PLAYBOOK_OUTPUT##' + \ '\n' with open("/tmp/" + exec_id, "a") as f: f.write(line_in_file + unique_pb_id + 'END' + '\n') except Exception, exc: ERR_MSG = "Error while trying to parse output"\ " from playbook due to exception: %s"\ % str(exc) with open("/tmp/" + exec_id, "a") as f: f.write(unique_pb_id + 'END' + '\n') JM_LOGGER.error(ERR_MSG) # not stopping execution just because of parsing error # no sys.exit therefore if output is None or output.get('status') is None: msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_OUTPUT_MISSING) raise Exception(msg) if output.get('status').lower() == "failure": msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_STATUS_FAILED) raise Exception(msg) return output