(self, host_list, playbook_path,extra_vars=dict())
run ansible palybook
run ansible palybook
run ansible palybook
def run_playbook(self, host_list, playbook_path,extra_vars=dict()): 
        """ 
        run ansible palybook 
        """   
        inventory = MyInventory(self.resource, self.loader, self.variable_manager)
        try: 
            if self.redisKey or self.logId:self.callback = PlayBookResultsCollectorToSave(self.redisKey,self.logId)  
            else:self.callback = PlayBookResultsCollector()  
            extra_vars['host'] = ','.join(host_list)
            self.variable_manager.extra_vars = extra_vars            
            executor = PlaybookExecutor(  
                playbooks=[playbook_path], inventory=inventory, variable_manager=self.variable_manager, loader=self.loader,  
                options=self.options, passwords=self.passwords,  
            )  
            executor._tqm._stdout_callback = self.callback  
            constants.HOST_KEY_CHECKING = False #关闭第一次使用ansible连接客户端是输入命令
            constants.DEPRECATION_WARNINGS = False
            constants.RETRY_FILES_ENABLED = False  
            executor.run()  
        except Exception as err: 
            logger.error(msg="run playbook failed: {err}".format(err=str(err)))
            if self.redisKey:
                DsRedis.OpsAnsibleModel.lpush(self.redisKey,data=err)
            if self.logId:
                AnsibleSaveResult.Model.insert(self.logId, err)
            return False
def run_playbook ( self , host_list , playbook_path , extra_vars = dict ( ) ) : inventory = MyInventory ( self . resource , self . loader , self . variable_manager ) try : if self . redisKey or self . logId : self . callback = PlayBookResultsCollectorToSave ( self . redisKey , self . logId ) else : self . callback = PlayBookResultsCollector ( ) extra_vars [ 'host' ] = ',' . join ( host_list ) self . variable_manager . extra_vars = extra_vars executor = PlaybookExecutor ( playbooks = [ playbook_path ] , inventory = inventory , variable_manager = self . variable_manager , loader = self . loader , options = self . options , passwords = self . passwords , ) executor . _tqm . _stdout_callback = self . callback constants . HOST_KEY_CHECKING = False #关闭第一次使用ansible连接客户端是输入命令 constants . DEPRECATION_WARNINGS = False constants . RETRY_FILES_ENABLED = False executor . run ( ) except Exception as err : logger . error ( msg = "run playbook failed: {err}" . format ( err = str ( err ) ) ) if self . redisKey : DsRedis . OpsAnsibleModel . lpush ( self . redisKey , data = err ) if self . logId : AnsibleSaveResult . Model . insert ( self . logId , err ) return False