def pbexe(sid,war,mode): variable_manager = VariableManager() loader = DataLoader() # inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[]) inventory = Inventory(loader=loader, variable_manager=variable_manager) playbook_path = '/etc/ansible/yml/'+sid+'.yml' if not os.path.exists(playbook_path): print 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='******', 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 = {'war': war,'mode': mode} # This can accomodate various other command line arguments.` passwords = {} #cb = ResultCallback() # cb = default.CallbackModule() pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) # pbex._tqm._stdout_callback = cb op=sys.stdout filename='/var/log/ansible/'+time.strftime('%Y%m%d%H%M%S')+'.log' opf=open(filename,'w') sys.stdout=opf results = pbex.run() sys.stdout=op opf.close print open(filename,'r').read() return filename
def pbexe(userid,serial,host,module,apppath,yaml,url=None): variable_manager = VariableManager() loader = DataLoader() hostfile = '/ansible/hosts' inventory = Inventory(loader=loader, variable_manager=variable_manager,host_list='/ansible/hosts') playbook_path = yaml if not os.path.exists(playbook_path): print 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='******', 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 = { 'host': host, 'module': module, 'tomcat_root': apppath, 'url': url } # This can accomodate various other command line arguments.` passwords = {} cb = CallbackModule(serial) # cb = default.CallbackModule() pbex = PlaybookExecutor( playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) pbex._tqm._stdout_callback = cb results = pbex.run() return results
def exec_keys(vars_state,pub_list,host_list,passwords=''): Options = namedtuple('Options',['connection','remote_user','ask_sudo_pass','verbosity','ack_pass','host_key_checking','module_path','forks','become','become_method', 'become_user','check','listhosts','listtasks','listtags','syntax','sudo_user','sudo']) #初始化需要的对象 variable_manager = VariableManager() #管理变量的类,包括主机,组,扩展等变量,之前版本是在 inventory中的 loader = DataLoader() #用来加载解析yaml文件或JSON内容,并且支持vault的解密 options = Options(connection='smart',remote_user='******',ack_pass=None,host_key_checking=False,sudo_user='******',forks=50,sudo='yes',ask_sudo_pass=False,verbosity=5,module_path=None, become=True,become_method='sudo',become_user='******',check=None,listhosts=None,listtasks=None,listtags=None,syntax=None) passwords=dict(conn_pass=passwords) #设置密码,必须是dict类型。如果ansible主机对服务器有密钥认证,则不需要密码 inventory = Inventory( loader=loader, variable_manager=variable_manager, host_list=host_list ) #根据inventory加载对应变量,这里host_list可以是文件也可以是IP列表 variable_manager.set_inventory(inventory) extra_vars = {} #额外的参数,key.yml以及模板中的参数,对应ansible-playbook key.yml --extra-vars pub=xxx state=present/absent extra_vars["pub_list"] = pub_list extra_vars["state"] = vars_state variable_manager.extra_vars = extra_vars #playbooks填写yml文件路径,可以写多个,是个列表 playbook = PlaybookExecutor( playbooks=['/var/www/html/CMDB/CMDB/key.yml'], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords ) playbook.run()
def _run_playbook(playbook, service, node): # Unfortunately, there's no good way to get the options instance # with proper defaults since it's generated by argparse inside # PlaybookCLI. Due to the fact that the options can't be empty # and must contain proper values we have not choice but extract # them from PlaybookCLI instance. playbook_cli = PlaybookCLI(['to-be-stripped', playbook]) playbook_cli.parse() options = playbook_cli.options # Get others required options. loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader, variable_manager) variable_manager.set_inventory(inventory) variable_manager.extra_vars = _get_user_settings(loader) # OpenStack Ansible deploys control plane services in LXC containers, # and use those as native hosts in its inventory. However, from # Kostyor's POV we are interested in baremetal node-by-node upgrade, # so we need to limit playbook execution only to baremetal node under # upgrade and its service's containers. inventory.subset([ host.get_vars()['inventory_hostname'] for host in _get_component_hosts_on_node(inventory, service, node) ]) # Finally, we can create a playbook executor and run the playbook. executor = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords={}) executor.run()
def get(self): data_loader = DataLoader() # data_loader.set_vault_password('627VR8*;YU99B') variable_manager = VariableManager() extra_vars = self.console_args.extra_vars[:] configurations = [ '@' + config for config in self.cluster_config_path.split(',') ] extra_vars.append('cluster=' + get_cluster_name(self.cluster_config_path)) extra_vars.extend(configurations) options = collections.namedtuple('options', 'extra_vars') variable_manager.extra_vars = load_extra_vars( loader=data_loader, options=options(extra_vars=extra_vars)) read_variables = variable_manager.get_vars(data_loader) templar = Templar(data_loader, variables=read_variables) templar._filter_loader = self.template.filter_plugin_loader return templar.template(read_variables, fail_on_undefined=True)
def AnsibleTask(task_list,host_list,user): '''ansible python api 2.0''' loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) task_dict = [] for i in task_list: task_dict.append({"action": {"module": i[0], "args": i[1] }}) variable_manager.extra_vars = {"ansible_ssh_user": user, "ansible_ssh_pass": ""} play_source = {"name" : "Ansible PlayBook Run", "hosts": host_list[0], "gather_facts": "no","tasks": task_dict} play = Play().load(play_source, variable_manager=variable_manager, loader=loader) tqm = None try: tqm = TaskQueueManager( inventory = inventory, variable_manager = variable_manager, loader = loader, options = options, passwords = None, stdout_callback = 'minimal', run_tree = False, ) result = tqm.run(play) except Exception,e: result = e
def runPlaybook(self): sys.stdout = open(self.stdoutfile, 'w') variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.host_list) #self.playbook_path # self.playbook_path = '/home/davis/Documents/Network-automation/cisco_xe.yml' if not os.path.exists(self.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='******', 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) variable_manager.extra_vars = { 'ansible_user': '******', 'ansible_password': '******' } # Here are the variables used in the winclones.yml #variable_manager.extra_vars = {'hosts': 'mywebserver'} # This can accomodate various other command line arguments.` passwords = {} pbex = PlaybookExecutor(playbooks=[self.playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run() print "Output ::-", results return results
def create_ansible_objects(inventory_file, extra_vars, forks=50): """Create the default ansible objects needed to run a playbook. :param inventory_file: The path to the inventory file :param extra_vars: The dictionary containing the collection status of the optional products. :param forks: number of forks to run with, default of 50 :returns: tuple of (options, variable_manager, loader, inventory) """ named_options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check' ]) options = named_options(connection='ssh', module_path=C.DEFAULT_MODULE_PATH, forks=forks, become=False, become_method='sudo', become_user='******', check=False) variable_manager = VariableManager() loader = DataLoader() loader.set_vault_password(settings.SECRET_KEY) # create inventory and extra vars and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_file) variable_manager.extra_vars = extra_vars variable_manager.set_inventory(inventory) return options, variable_manager, loader, inventory
def runPlaybook(palyname, yml_file, myvars, forks): # initialize needed objects variable_manager = VariableManager() loader = DataLoaderV2(palyname) 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']) pb_options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=forks, 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) passwords = {} # create inventory and pass to var manager inventory = Inventory( loader=loader, variable_manager=variable_manager) variable_manager.set_inventory(inventory) variable_manager.extra_vars = myvars pbex = PlaybookExecutorV2(playbooks=[yml_file], inventory=inventory, variable_manager=variable_manager, loader=loader, options=pb_options, passwords=passwords) # pbex._tqm._stdout_callback.reset_output() rc = pbex.run() return {'rc': rc, 'detail': pbex._tqm._stdout_callback.std_lines}
def get(self, user_name): variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='./hosts') playbook_path = './playbook.yml' remoteuser = '******' # Modify for default user if user_name: remoteuser = user_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=remoteuser, 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=4, check=False) variable_manager.extra_vars = { 'target_host': request.remote_addr } # This can accomodate various other command line arguments.` passwords = {} pbexecutor = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbexecutor.run() return {'Result': results}
def execute_action(self, action, args): """ Execute the requested operation """ C.DEFAULT_ROLES_PATH = [os.path.join(ROLESDIR, str(action))] i3xfce.loggers.ROOTLOGGER.debug("Executing the %s action", action) # Get the real user behind the sudo username = os.getenv("SUDO_USER") if username is None: i3xfce.loggers.ROOTLOGGER.debug("Unable to get SUDO_USER environment variable. This means i3-xfce has not been \ started using sudo") raise Exception("This program must be ran using sudo ") i3xfce.loggers.ROOTLOGGER.debug("Creating the option tuple") options_tuple = namedtuple('Options', ['connection', 'forks', 'module_path', 'become_user', 'become', 'become_method', 'check', 'verbosity']) try: # initialize needed objects variable_manager = VariableManager() variable_manager.extra_vars = dict(action=str(action), remote_user=username) loader = DataLoader() i3xfce.loggers.ROOTLOGGER.debug("Creating option to count number of tasks to execute") options = options_tuple(connection=None, module_path=None, forks=1, become_user=None, become=None, become_method=None, verbosity=0, check=True) tasks_count_callback = TaskCountCallback() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=None) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name="Ansible Play", hosts='localhost', gather_facts='no', ignore_errors="yes", roles=args.parts ) CmdLine._execute_play(play_source, inventory, variable_manager, loader, options, tasks_count_callback) i3xfce.loggers.ROOTLOGGER.debug("%i tasks are going to be executed", tasks_count_callback.get_total_tasks_num()) play_source["ignore_errors"] = "no" options = options_tuple(connection=None, module_path=None, forks=1, become_user=None, become=None, become_method=None, verbosity=0, check=args.dryrun) self._results_callback = PlaybookExecutionCallback(tasks_count_callback.get_total_tasks_num(), tasks_count_callback.get_task_name_max_len()) CmdLine._execute_play(play_source, inventory, variable_manager, loader, options, self._results_callback) self._results_callback.get_progress_bar().stop() self._results_callback.get_progress_bar().join() if self._results_callback.get_task_failed() is True: raise TaskExecutionException("") except TaskExecutionException as exc: raise except Exception as exc: raise TaskExecutionException(str(exc))
def __launch_ansible_playbook(list_ip, playbook, extra_variable=None): """ Applies an Ansible playbook :param playbook: the playboo to be applied """ print extra_variable if not os.path.isfile(playbook): raise Exception('Requested playbook is not found - ' + playbook) if not playbook: logger.warn('Unable to find playbook - ' + playbook) variable_manager = VariableManager() if extra_variable is not None: variable_manager.extra_vars = extra_variable logger.info(extra_variable) else: logger.info('NO EXTRA VARS') loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=list_ip) variable_manager.set_inventory(inventory) options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'sftp_extra_args', 'scp_extra_args']) ansible_opts = options(listtags=None, listtasks=None, listhosts=None, syntax=False, connection='ssh', module_path=None, forks=100, remote_user=None, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, become=True, become_method='sudo', become_user=None, verbosity=11111, check=False, sftp_extra_args=None, scp_extra_args=None) logger.debug( 'Setting up Ansible Playbook Executor for playbook - ' + playbook) executor = PlaybookExecutor( playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=ansible_opts, passwords=None) logger.info('Executing Ansible Playbook - ' + playbook) ret = executor.run() if ret != 0: raise Exception('Error Executing Ansible Playbook - ' + playbook)
def ansiblex(vars1="1", vars2="2", vars3="3", vars4="4", vars5="5", vars6="6"): one_var = vars1 two_var = vars2 three_var = vars3 four_var = vars4 five_var = vars5 six_var = vars6 variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/etc/ansible/hosts') 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=30, 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 = { 'two_var': two_var, 'three_var': three_var, 'four_var': four_var, 'five_var': five_var, 'six_var': six_var } passwords = {} pbex = PlaybookExecutor(playbooks=[one_var], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run() return results
def post(self): args = parser.parse_args() stage = str(args['stage']) + ".yml" variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='ansible_hosts') playbook_path = stage 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='******', 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=None, check=False) variable_manager.extra_vars = { } # 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() print(isinstance(results, list)) if results == 0: return {"status": "success"}, 200 else: return {"status": "error"}, 500
def run_embebed_playb(self, options, passwords): # initialize needed objects variable_manager = VariableManager() loader = DataLoader() # Instantiate our ResultCallback for handling results as they come in results_callback = ResultCallback() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.inventory['hosts']) inventory.subset(self.inventory['subset']) variable_manager.set_inventory(inventory) variable_manager.extra_vars = { 'ansible_connection': self.ansible_cnx['ansible_connection'] } # create play with tasks play_source = dict( name="RUN PLAY", hosts=self.target, #remote_user = self.ansible_cnx['ansible_user'], gather_facts='false', tasks=[ dict(action=dict(module=self.module, args=self.pargs), register='module_out'), ]) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback=results_callback, # custom callback ) result = tqm.run(play) finally: if tqm is not None: tqm.cleanup() return results_callback.results
def __execute_playbook(self, playbook, inventory_file, random_user, random_clear_text_password): path_exists(playbook, self.logger) path_exists(inventory_file, self.logger) variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_file) 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=1, remote_user=None, 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) variable_manager.extra_vars = { 'r_user': random_user, 'r_password': random_clear_text_password } passwords = {} pbex = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run() return results
def _load_defaults(inventory_path, extra_vars=None, tags=None): """Load common defaults data structures. For factorization purpose.""" extra_vars = extra_vars or {} tags = tags 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 # 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) return inventory, variable_manager, loader, options
def invoke_linchpin(base_path, e_vars, playbook="PROVISION", console=True): """ Invokes linchpin playbook """ module_path = base_path + "/library" print("debug:: module path ::" + module_path) playbook_path = base_path + "/provision/" + PLAYBOOKS[playbook] loader = DataLoader() variable_manager = VariableManager() variable_manager.extra_vars = e_vars inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[]) passwords = {} utils.VERBOSITY = 4 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=module_path, 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=utils.VERBOSITY, check=False) pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) if not console: cb = PlaybookCallback() pbex._tqm._stdout_callback = cb return_code = pbex.run() results = cb.results else: results = pbex.run() return results
def test_variable_manager_extra_vars(self): fake_loader = DictDataLoader({}) extra_vars = dict(a=1, b=2, c=3) v = VariableManager() v.extra_vars = extra_vars vars = v.get_vars(loader=fake_loader, use_cache=False) for (key, val) in extra_vars.iteritems(): self.assertEqual(vars.get(key), val) self.assertIsNot(v.extra_vars, extra_vars)
def run_playbook(inventory_root, playbook, extra_vars=None, connection='ssh', module_path=None, forks=5, become=None, become_method=None, become_user=None, check=False): # We need a way to pass options to Ansible. We do so by passing a tuple object # This is not a exhaustive list, we only do what we need Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax' ]) # initialize needed objects stats = AggregateStats() variable_manager = VariableManager() variable_manager.extra_vars = extra_vars loader = DataLoader() options = Options(connection=connection, module_path=module_path, forks=forks, become=become, become_method=become_method, become_user=become_user, check=check, listhosts=False, listtasks=False, listtags=False, syntax=False) passwords = dict(vault_pass='******') # this is required, for now would # mostly be junk for us # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_root) variable_manager.set_inventory(inventory) pbex = PlaybookExecutor(playbook, inventory=inventory, variable_manager=variable_manager, loader=loader, passwords=passwords, options=options) pbex._tqm._stdout_callback = 'json' return_code = pbex.run() results = pbex._tqm._stdout_callback.results tqm = pbex._tqm return (return_code, tqm, results)
def run_playbook(playbook_file, inventory_file, extra_vars=None): if extra_vars is None: extra_vars = {} variable_manager = VariableManager() loader = DataLoader() options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection=C.DEFAULT_TRANSPORT, 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=False, become_method=None, become_user=None, verbosity=C.DEFAULT_VERBOSITY, check=False, extra_vars=extra_vars) # Load the extra vars variable_manager.extra_vars = extra_vars # Load the inventory file inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_file) variable_manager.set_inventory(inventory) pbex = PlaybookExecutor(playbooks=[playbook_file], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None) # Register a callback so that we can inspect the results callback = ResultCallback() pbex._tqm._callback_plugins.append(callback) # Run the playbook code = pbex.run() # User is most likely interested in output of last command, so get that out = callback.results[-1]._result['out'] return code, out.strip(), callback.results
def ansiblePlay(self): '''run ansible playbook''' variable_manager = VariableManager() Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'listhosts', 'listtasks', 'listtags', 'syntax', 'become', 'become_method', 'become_user', 'check']) options = Options(connection='ssh', module_path='', forks=1, become=None, listhosts=None, listtasks=False, listtags=False, syntax=False, become_method=None, become_user=None, check=False) loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager) variable_manager.set_inventory(inventory) self.callback = ResultCallback(self.rediskey) variable_manager.extra_vars = self.extra_vars self.runner = PlaybookExecutor(playbooks=self.playbook_path, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None) self.runner._tqm._stdout_callback = self.callback
def run_cmd(self): passwords = { 'conn_pass': "******", 'become_pass': "******" } loader = DataLoader() variable_manager = VariableManager() variable_manager.extra_vars = load_extra_vars(loader=loader, options=self.pb_options) variable_manager.options_vars = load_options_vars(self.pb_options) inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.pb_options.inventory) variable_manager.set_inventory(inventory) play_ds = self._play_ds(self.pb_options.name, self.pb_options.pattern) play = Play().load(play_ds, variable_manager=variable_manager, loader=loader) self._tqm = None try: self._tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.pb_options, passwords=passwords, # stdout_callback=CallbackWrap(), ) rc = self._tqm.run(play) detail = json.loads(self._tqm._stdout_callback._dump_results) print("json.loads.self._tqm._stdout_callback._dump_results") print(json.loads(self._tqm._stdout_callback._dump_results)) print("self._tqm._stdout_callback dir") print(dir(self._tqm._stdout_callback)) finally: if self._tqm: self._tqm.cleanup() if loader: loader.cleanup_all_tmp_files() return {'rc': rc, 'detail': detail}
def get(self): data_loader = DataLoader() # data_loader.set_vault_password() variable_manager = VariableManager() extra_vars = self.console_args.extra_vars[:] extra_vars.append('cluster=' + get_cluster_name(self.cluster_config_path)) options = collections.namedtuple('options', 'extra_vars') variable_manager.extra_vars = load_extra_vars(loader=data_loader, options=options(extra_vars=extra_vars)) variables = variable_manager.get_vars(data_loader) rendered = self.template.render(self.cluster_config_path, variables) return yaml.load(rendered)
def _run_playbook_impl(playbook, hosts_fn=None, cwd=None, ignore_errors=False): # Unfortunately, there's no good way to get the options instance # with proper defaults since it's generated by argparse inside # PlaybookCLI. Due to the fact that the options can't be empty # and must contain proper values we have not choice but extract # them from PlaybookCLI instance. playbook_cli = PlaybookCLI(['to-be-stripped', playbook]) playbook_cli.parse() options = playbook_cli.options # Get others required options. loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader, variable_manager) variable_manager.set_inventory(inventory) variable_manager.extra_vars = _get_user_settings(loader) # Limit playbook execution to hosts returned by 'hosts_fn'. if hosts_fn is not None: inventory.subset([ host.get_vars()['inventory_hostname'] for host in hosts_fn(inventory) ]) # Finally, we can create a playbook executor and run the playbook. executor = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords={}) # Some playbooks may rely on current working directory, so better allow # to change it before execution. with _setcwd(cwd): exitcode = executor.run() # Celery treats exceptions from task as way to mark it failed. So let's # throw one to do so in case return code is not zero. if all([not ignore_errors, exitcode is not None, exitcode != 0]): raise Exception('Playbook "%s" has been finished with errors. ' 'Exit code is "%d".' % (playbook, exitcode)) return exitcode
def IpmiPlay(self): 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', 'extra_vars' ]) self.options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=10, remote_user='', private_key_file=None, ssh_common_args='', ssh_extra_args='', sftp_extra_args='', scp_extra_args='', become=True, become_method='sudo', become_user='******', verbosity=3, check=False, extra_vars={}) self.loader = DataLoader() # 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=self.loader, options=self.options) variable_manager.options_vars = load_options_vars(self.options) self.variable_manager = variable_manager # create the inventory, and filter it based on the subset specified (if any) self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=self.host_list) self.variable_manager.set_inventory(self.inventory) self.variable_manager.extra_vars = self.extra_vars
def run_yml_playb(self, options, passwords, playbook=None): # initialize needed objects variable_manager = VariableManager() loader = DataLoader() # Instantiate our ResultCallback for handling results as they come in results_callback = ResultCallback() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.inventory['hosts']) inventory.subset(self.inventory['subset']) variable_manager.set_inventory(inventory) # Currently support target = servers, cmd = commands, dir = args: dir variable_manager.extra_vars = { 'target': self.target, 'cmd': self.pargs, 'dir': '/bin' } # Playbook if playbook is None: playbook_path = 'playbooks/commands.yml' else: playbook_path = 'playbooks/' + playbook if not os.path.exists(playbook_path): print '[INFO] The playbook does not exist' sys.exit() pexec = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pexec.run()
def run_playbook(name, **kwargs): ansible_dir = "/home/%s/keystone_full_deployment/ansible" % getuser() variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list="%s/hosts" % ansible_dir) 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 __execute_playbook(self, playbook, inventory_file, random_user, random_clear_text_password): path_exists(playbook, self.logger) path_exists(inventory_file, self.logger) variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_file) 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=1, remote_user=None, 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) variable_manager.extra_vars = {'r_user': random_user, 'r_password': random_clear_text_password} passwords = {} pbex = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run() return results
def _load(self): variable_manager = VariableManager() variable_manager.extra_vars = {} variable_manager.options_vars = {'ansible_check_mode': False} loader = DataLoader() passwords = dict(vault_pass='******') #display seems not to work yet :-( display = Display() display.verbosity = self.options.verbosity # playbook_executor.display.verbosity = self.options.verbosity extra_vars = { 'elastic_url': Configuration.elastic_url, 'uuid': self._job.uuid } self.variable_manager.extra_vars = extra_vars inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.inventoryContainer.allHosts()) for group in self.inventoryContainer.groups: if group.name == 'all': if group.vars: inventory.get_group('all').vars.update(group.vars) else: group.parent_groups.append(inventory.get_group('all')) inventory.add_group(group) variable_manager.set_inventory(inventory) playbook = "%s/%s" % (self.pb_dir__, self.playbook__) pbx = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.options, passwords=passwords) pbx.run()
def playbook_run(playbook_path, playbook_vars): """ run an ansible playbook :param playbook_path: :param playbook_vars: :return: """ variable_manager = VariableManager() variable_manager.extra_vars = playbook_vars loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/etc/ansible/hosts') passwords = {} pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) result = pbex.run() return result
def run_playbook(playbook_path, host_list_path): variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list_path) 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='******', 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': '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() node_info = dict() host_vars = pbex._tqm.hostvars for info in host_vars._cached_result.values(): node_info[info['ansible_nodename']] = deepcopy(info) return results, node_info
def run_playbook(playbook_path, host_list, extra_vars): loader = DataLoader() variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) 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="/Users/dyc/.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=True, check=False) extra_vars["ansible_ssh_port"] = 22 variable_manager.extra_vars = extra_vars pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords={}) # Instantiate our ResultCallback for handling results as they come in results_callback = ResultCallback() results_callback = FileCallbackModule() pbex._tqm._stdout_callback=results_callback 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(ask_vault_pass=True, ask_new_vault_pass=False, confirm_new=False)[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 self.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, display=self.display, options=self.options, passwords=passwords, ) results = pbex.run() if isinstance(results, list): for p in results: self.display.display("\nplaybook: %s" % p["playbook"]) i = 1 for play in p["plays"]: if play.name: playname = play.name else: playname = "#" + str(i) msg = "\n PLAY: %s" % (playname) mytags = set() if self.options.listtags and play.tags: mytags = mytags.union(set(play.tags)) msg += " TAGS: [%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 self.display.display(msg) if self.options.listtags or self.options.listtasks: taskmsg = " tasks:" for block in play.compile(): if not block.has_tasks(): continue j = 1 for task in block.block: taskmsg += "\n %s" % task if self.options.listtags and task.tags: taskmsg += " TAGS: [%s]" % ",".join(mytags.union(set(task.tags))) j = j + 1 self.display.display(taskmsg) i = i + 1 return 0 else: return results
def launch_playbook_v2(self): ''' run ansible-playbook operations v2.X''' # create parser for CLI options parser = CLI.base_parser( usage="%prog playbook.yml", connect_opts=True, meta_opts=True, runas_opts=True, subset_opts=True, check_opts=True, inventory_opts=True, runtask_opts=True, vault_opts=True, fork_opts=True, module_opts=True, ) options, _ = parser.parse_args([]) sshpass = None if not options.become_user: options.become_user = "******" if self.pk_file: options.private_key_file = self.pk_file else: sshpass = self.passwd passwords = {'conn_pass': sshpass, 'become_pass': sshpass} if self.user: options.remote_user = self.user if not os.path.exists(self.playbook_file): raise errors.AnsibleError( "the playbook: %s could not be found" % self.playbook_file) if not os.path.isfile(self.playbook_file): raise errors.AnsibleError( "the playbook: %s does not appear to be a file" % self.playbook_file) variable_manager = VariableManager() variable_manager.extra_vars = self.extra_vars if self.inventory_file: options.inventory = self.inventory_file options.forks = self.threads loader = DataLoader() # Add this to avoid the Ansible bug: no host vars as host is not in inventory # In version 2.0.1 it must be fixed ansible.inventory.HOSTS_PATTERNS_CACHE = {} inventory = ansible.inventory.Inventory( loader=loader, variable_manager=variable_manager, host_list=options.inventory) variable_manager.set_inventory(inventory) if self.host: inventory.subset(self.host) # let inventory know which playbooks are using so it can know the # basedirs inventory.set_playbook_basedir(os.path.dirname(self.playbook_file)) num_retries = 0 return_code = 4 results = None while return_code != 0 and num_retries < self.retries: time.sleep(5 * num_retries) num_retries += 1 return_code = 0 try: # create the playbook executor, which manages running the plays # via a task queue manager pbex = IMPlaybookExecutor(playbooks=[self.playbook_file], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, output=self.output) return_code = pbex.run() except errors.AnsibleError, e: display("ERROR: %s" % e, output=self.output) return_code = 1 if return_code != 0: display("ERROR executing playbook (%s/%s)" % (num_retries, self.retries), output=self.output)
def ansiblex_deploy(vars1="1", vars2="2", vars3="3", vars4="4", vars5="5", vars6="6", vars7="7", vars8="8", vars9="9", vars10="10", vars11="11", vars12="12", vars13="13", vars14="14", vars15="15", vars16="16"): one_var = vars1 two_var = vars2 three_var = vars3 four_var = vars4 five_var = vars5 six_var = vars6 seven_var = vars7 eight_var = vars8 nine_var = vars9 ten_var = vars10 eleven_var = vars11 twelve_var = vars12 thirteen_var = vars13 fourteen_var = vars14 fifteen_var = vars15 sixteen_var = vars16 variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=one_var) 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=30, 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 = { 'three_var': three_var, 'four_var': four_var, 'five_var': five_var, 'six_var': six_var, 'seven_var': seven_var, 'eight_var': eight_var, 'nine_var': nine_var, 'ten_var': ten_var, 'eleven_var': eleven_var, 'twelve_var': twelve_var, 'thirteen_var': thirteen_var, 'fourteen_var': fourteen_var, 'fifteen_var': fifteen_var, 'sixteen_var': sixteen_var } passwords = {} pbex = PlaybookExecutor(playbooks=[two_var], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) callback = CallbackModule() pbex._tqm._stdout_callback = callback return_code = pbex.run() results = callback.results return results
print '[INFO] The playbook_pathaybook 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='******', 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) # This can accomodate various other command line arguments.` variable_manager.extra_vars = {'hosts': 'mywebserver'} passwords = {} pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run()
from ansible.executor.playbook_executor import PlaybookExecutor variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/home/slotlocker/hosts2') playbook_path = '/home/slotlocker/ls.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']) 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': '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() # ad-hoc #!/usr/bin/env python import json from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager
def invoke_linchpin(base_path, e_vars, playbook="PROVISION", console=True): """ Invokes linchpin playbook """ module_path = base_path+"/library" print("debug:: module path ::"+module_path) playbook_path = base_path+"/provision/"+PLAYBOOKS[playbook] loader = DataLoader() variable_manager = VariableManager() variable_manager.extra_vars = e_vars inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[]) passwords = {} utils.VERBOSITY = 4 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=module_path, 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=utils.VERBOSITY, check=False) pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) if not console: cb = PlaybookCallback() pbex._tqm._stdout_callback = cb return_code = pbex.run() results = cb.results else: results = pbex.run() return results
import sys from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager from ansible.inventory import Inventory from ansible.executor.playbook_executor import PlaybookExecutor from ansible.utils.display import log_file import sys variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/etc/ansible/hosts') 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', 'verbosity', 'check']) 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=False, become_method=None, become_user=None, verbosity=None, check=False) playbook_path = 'test.yml' variable_manager.extra_vars = {"args": "pong",} # This can accomodate various other command line arguments.` if not os.path.exists(playbook_path): print '[INFO] The playbook does not exist' sys.exit() passwords = {} pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) results = pbex.run() print results
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='sudo', become_user='******', verbosity=None, check=False) loader = DataLoader() variable_manager = VariableManager() variable_manager.extra_vars = { 'hosts': 'all', 'roles': 'all', } variable_manager.extra_vars.update(args.extra_vars) inventory = build_inventory(loader, variable_manager, args.groups, playbook_basedir) variable_manager.set_inventory(inventory) plays = build_plays(loader, variable_manager, args.playbook, plays=args.plays, hosts=args.hosts) tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, passwords=None, options=options, stdout_callback='default', run_tree=False,
def run_ansible(playbooks, inventory_path, extra_vars={}, tags=None): 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 passwords = {} 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']) 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=False, become_method=None, become_user=None, remote_user=None, verbosity=None, check=False, tags=tags) for path in playbooks: logger.info("Running playbook %s with vars:\n%s" % (style.emph(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: logger.error("Failed hosts: %s" % failed_hosts) if len(unreachable_hosts) > 0: logger.error("Unreachable hosts: %s" % unreachable_hosts)
import os import sys from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager from ansible.inventory import Inventory from ansible.executor.playbook_executor import PlaybookExecutor from ansible.playbook.play import Play from ansible.executor.task_queue_manager import TaskQueueManager variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/etc/ansible/hosts') playbook_path = '/root/test/ls.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']) 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': 'vmware'} # 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()
def apply_playbook(playbook_path, hosts_inv, host_user, ssh_priv_key_file_path, variables=None, proxy_setting=None): """ Executes an Ansible playbook to the given host :param playbook_path: the (relative) path to the Ansible playbook :param hosts_inv: a list of hostnames/ip addresses to which to apply the Ansible playbook :param host_user: A user for the host instances (must be a password-less sudo user if playbook has "sudo: yes" :param ssh_priv_key_file_path: the file location of the ssh key :param variables: a dictionary containing any substitution variables needed by the Jinga 2 templates :param proxy_setting: string containing host:port of the proxy server in use :return: the results """ if not os.path.isfile(playbook_path): raise Exception('Requested playbook not found - ' + playbook_path) if not os.path.isfile(ssh_priv_key_file_path): raise Exception('Requested private SSH key not found - ' + ssh_priv_key_file_path) import ansible.constants ansible.constants.HOST_KEY_CHECKING = False variable_manager = VariableManager() if variables: variable_manager.extra_vars = variables loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=hosts_inv) variable_manager.set_inventory(inventory) loader = DataLoader() ssh_common_args = None ssh_extra_args = None ssh_connection = 'ssh' proxy_command = None if proxy_setting: tokens = re.split(':', proxy_setting) # TODO - Need to configure the proxy settings to avoid adding entries into the host's ~/.ssh/config file 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', 'host_key_checking', 'transport', 'proxy_command']) ansible_opts = options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection=ssh_connection, module_path=None, forks=100, remote_user=host_user, private_key_file=ssh_priv_key_file_path, ssh_common_args=ssh_common_args, ssh_extra_args=ssh_extra_args, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user='******', verbosity=1111, check=False, host_key_checking=True, transport='paramiko', proxy_command=proxy_command) logger.debug('Setting up Ansible Playbook Executor') executor = PlaybookExecutor( playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=ansible_opts, passwords=None) logger.debug('Executing Ansible Playbook - ' + playbook_path) retval = executor.run() if retval != 0: logger.error('Playbook application failed [' + playbook_path + '] with return value of - ' + str(retval)) raise Exception('Playbook not applied - ' + playbook_path) return retval
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 = {} loader = DataLoader() 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) hostlist = self.translate_inventory_itsm_to_ansible(self.options.inventory) pt = self.creaate_palybook_target(self.options.inventory) task_id = self.get_task_id(self.options.inventory) playbook_location = json.loads(self.options.inventory)["playbook"] inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=hostlist) variable_manager.set_inventory(inventory) 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 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=[playbook_location], inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.options, passwords=passwords, pt=pt, task_id=task_id) 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' 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 for task in block.block: 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) 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 run_ansible_on_host(host, logger, driver_config): from ansible.plugins.callback import CallbackBase # A rough logger that logs dict messages to standard logger class ResultCallback(CallbackBase): def __init__(self): super(ResultCallback, self).__init__() def v2_runner_on_ok(self, result, **kwargs): self.log('ok :' + str(result._result)) def v2_runner_on_failed(self, result, **kwargs): warnings = result._result['warnings'] error = result._result['stderr'] if warnings: self.log('warning : ' + str(result._result)) elif error: self.log('error : ' + str(result._result), info=True) def v2_runner_on_skipped(self, result, **kwargs): self.log('skipped : ' + str(result._result)) def v2_runner_on_unreachable(self, result, **kwargs): self.log('unreachable : ' + str(result._result), info=True) def v2_playbook_on_no_hosts_matched(self, *args, **kwargs): self.log('no hosts matched!') def v2_playbook_on_no_hosts_remaining(self, *args, **kwargs): self.log('NO MORE HOSTS LEFT') def v2_playbook_on_task_start(self, task, **kwargs): self.log('starting task: ' + str(task)) def v2_playbook_on_start(self, playbook, **kwargs): self.log('starting playbook' + str(playbook), info=True) def v2_playbook_on_play_start(self, play, **kwargs): self.log('starting play' + str(play), info=True) def v2_playbook_on_stats(self, stats, info=True, **kwargs): self.log('STATS FOR PLAY') hosts = sorted(stats.processed.keys()) hosts.extend(stats.failures.keys()) hosts.extend(stats.dark.keys()) hosts.extend(stats.changed.keys()) hosts.extend(stats.skipped.keys()) hosts.extend(stats.ok.keys()) for h in hosts: t = stats.summarize(h) self.log(str(t)) def log(self, param, info=False): if not info: logger.debug(str(param)) else: logger.info(str(param)) from ansible.parsing.dataloader import DataLoader from ansible.inventory import Inventory, Group, Host from ansible.executor import playbook_executor from ansible.vars import VariableManager from collections import namedtuple Options = namedtuple( 'Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'ansible_user', 'listhosts', 'listtasks', 'listtags', 'syntax', 'ssh_private_key_file', 'host_key_checking' ] ) options = Options( connection='ssh', become=True, become_method='sudo', become_user='******', check=False, module_path=None, forks=100, ansible_user='******', listhosts=False, listtasks=False, listtags=False, syntax=False, ssh_private_key_file=PEBBLES_SSH_KEY_LOCATION, host_key_checking=False ) variable_manager = VariableManager() loader = DataLoader() a_host = Host(name=host['private_ip']) a_host.set_variable('ansible_host', host['private_ip']) a_group = Group(name='notebook_host') a_group.add_host(a_host) inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[host['private_ip']]) inventory.add_group(a_group) variable_manager.set_inventory(inventory) logger.debug('HOST:') logger.debug(a_host.serialize() ) logger.debug('HOSTs from inventory:') # for some reason setting these before adding the host to inventory didn't work so well # ToDo: read up on variable_manager and figure out a more elegant way to set the variables for h_ in inventory.get_hosts(): h_.set_variable('ansible_user', 'cloud-user') h_.set_variable('ansible_ssh_common_args', '-o StrictHostKeyChecking=no') h_.set_variable('ansible_ssh_private_key_file', '/home/pebbles/.ssh/id_rsa') extra_vars = dict() extra_vars['ansible_ssh_extra_args'] = '-o StrictHostKeyChecking=no' logger.debug('Setting driver config....') if 'DD_HOST_DATA_VOLUME_DEVICE' in driver_config: extra_vars['notebook_host_block_dev_path'] = driver_config['DD_HOST_DATA_VOLUME_DEVICE'] variable_manager.extra_vars = extra_vars pb_executor = playbook_executor.PlaybookExecutor( playbooks=['/webapps/pebbles/source/ansible/notebook_playbook.yml'], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None ) rescb = ResultCallback() pb_executor._tqm._stdout_callback = rescb logger.info('_prepare_host(): running ansible') logger.info('_prepare_host(): inventory hosts') for h_ in inventory.get_hosts(): logger.info(h_.serialize()) logger.info(h_.get_vars()) pb_executor.run() stats = pb_executor._tqm._stats run_success = True hosts_list = sorted(stats.processed.keys()) if len(hosts_list) == 0: logger.debug('no hosts handled') for h in hosts_list: t = stats.summarize(h) logger.debug(t) logger.debug(h) if t['unreachable'] > 0 or t['failures'] > 0: run_success = False if run_success: logger.debug('_prepare_host(): run successfull') else: logger.debug('_prepare_host(): run failed') if getattr(pb_executor, '_unreachable_hosts', False): logger.debug('UNREACHABLE HOSTS ' + str(pb_executor._unreachable_hosts)) if getattr(pb_executor, '_failed_hosts', False): logger.debug('FAILED_HOSTS ' + str(pb_executor._failed_hosts)) raise RuntimeError('run_ansible_on_host(%s) failed' % host['id']) logger.debug('_prepare_host(): done running ansible')
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 = {} # 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 = 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) # 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) variable_manager.options_vars = load_options_vars(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") # flush fact cache if requested if self.options.flush_cache: for host in inventory.list_hosts(): variable_manager.clear_facts(host) # 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 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) variable_manager.options_vars = load_options_vars(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