当前位置: 首页>>代码示例>>Python>>正文


Python Runner.run_async方法代码示例

本文整理汇总了Python中ansible.runner.Runner.run_async方法的典型用法代码示例。如果您正苦于以下问题:Python Runner.run_async方法的具体用法?Python Runner.run_async怎么用?Python Runner.run_async使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ansible.runner.Runner的用法示例。


在下文中一共展示了Runner.run_async方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _run

# 需要导入模块: from ansible.runner import Runner [as 别名]
# 或者: from ansible.runner.Runner import run_async [as 别名]
    def _run(self):
        self.start = datetime.now()
        self.save()

        # initial jobs
        for host in Inventory(ANSIBLE_INVENTORY).list_hosts(self.inventory):
            self.job_set.add(Job(host = host, cmd = self.cmd,
                                 start = datetime.now()))
        self.save()

        runner = Runner(module_name = 'shell', module_args = self.cmd,
                        pattern = self.inventory, sudo = self.sudo,
                        forks = ANSIBLE_FORKS, host_list = ANSIBLE_INVENTORY)

        _, poller = runner.run_async(time_limit = WORKER_TIMEOUT)

        now = time.time()

        while True:

            if poller.completed or time.time() - now > WORKER_TIMEOUT: # TIMEOUT
                break

            results = poller.poll()
            results = results.get('contacted')

            if results:
                for host, result in results.items():
                    job = self.job_set.get(host = host)
                    job.end = result.get('end')
                    job.rc = result.get('rc')
                    job.stdout = result.get('stdout')
                    job.stderr = result.get('stderr')
                    job.save()

            time.sleep(1)

        jobs_timeout = filter(lambda job: job.rc is None, self.job_set.all()) # rc is None
        jobs_failed = filter(lambda job: job.rc, self.job_set.all()) # rc > 0

        for job in jobs_timeout:
            job.rc = 1
            job.stderr = 'JOB TIMEOUT' # marked as 'TIMEOUT'
            job.save()

        self.rc = (jobs_timeout or jobs_failed) and 1 or 0

        self.end = datetime.now()
        self.save()

        self.done()
开发者ID:naiteluode,项目名称:ftasomc,代码行数:53,代码来源:models.py

示例2: run

# 需要导入模块: from ansible.runner import Runner [as 别名]
# 或者: from ansible.runner.Runner import run_async [as 别名]
    def run(self, options, args):
        ''' use Runner lib to do SSH things '''

        pattern = args[0]

        sshpass = becomepass = vault_pass = become_method = None

        # Never ask for an SSH password when we run with local connection
        if options.connection == "local":
            options.ask_pass = False
        else:
            options.ask_pass = options.ask_pass or C.DEFAULT_ASK_PASS

        options.ask_vault_pass = options.ask_vault_pass or C.DEFAULT_ASK_VAULT_PASS

        # become
        utils.normalize_become_options(options)
        prompt_method = utils.choose_pass_prompt(options)
        (sshpass, becomepass, vault_pass) = utils.ask_passwords(ask_pass=options.ask_pass, become_ask_pass=options.become_ask_pass, ask_vault_pass=options.ask_vault_pass, become_method=prompt_method)

        # read vault_pass from a file
        if not options.ask_vault_pass and options.vault_password_file:
            vault_pass = utils.read_vault_file(options.vault_password_file)

        extra_vars = utils.parse_extra_vars(options.extra_vars, vault_pass)

        inventory_manager = inventory.Inventory(options.inventory, vault_password=vault_pass)
        if options.subset:
            inventory_manager.subset(options.subset)
        hosts = inventory_manager.list_hosts(pattern)

        if len(hosts) == 0:
            callbacks.display("No hosts matched", stderr=True)
            sys.exit(0)

        if options.listhosts:
            for host in hosts:
                callbacks.display('    %s' % host)
            sys.exit(0)

        if options.module_name in ['command','shell'] and not options.module_args:
            callbacks.display("No argument passed to %s module" % options.module_name, color='red', stderr=True)
            sys.exit(1)

        if options.tree:
            utils.prepare_writeable_dir(options.tree)



        runner = Runner(
            module_name=options.module_name,
            module_path=options.module_path,
            module_args=options.module_args,
            remote_user=options.remote_user,
            remote_pass=sshpass,
            inventory=inventory_manager,
            timeout=options.timeout,
            private_key_file=options.private_key_file,
            forks=options.forks,
            pattern=pattern,
            callbacks=self.callbacks,
            transport=options.connection,
            subset=options.subset,
            check=options.check,
            diff=options.check,
            vault_pass=vault_pass,
            become=options.become,
            become_method=options.become_method,
            become_pass=becomepass,
            become_user=options.become_user,
            extra_vars=extra_vars,
        )

        import pdb
        pdb.set_trace() 
        if options.seconds:
            callbacks.display("background launch...\n\n", color='cyan')
            results, poller = runner.run_async(options.seconds)
            results = self.poll_while_needed(poller, options)
        else:
            results = runner.run()

        return (runner, results)
开发者ID:zhaogaolong,项目名称:oneFinger,代码行数:85,代码来源:ans.py

示例3: __call__

# 需要导入模块: from ansible.runner import Runner [as 别名]
# 或者: from ansible.runner.Runner import run_async [as 别名]
    def __call__(self, *args, **kwargs):
        '''
        Adhoc execution wrapper
        :param args: module arguments
        :param kwargs:
         kwargs[run_async]: Running async
         kwargs[time_limit]: related to async
         kwargs[forks]: amount of parallel processes
         kwargs[remote_user]: costume remote user login
         kwargs[remote_pass]: costume remote user password
         kwargs[remote_port]: costume remote port
         kwargs[transport]: support "ssh, paramiko, local"
         kwargs[become_user]: connect as sudo user
         kwargs[become_method]: set to ‘sudo’/’su’/’pbrun’/’pfexec’/’doas’
        :return: Future object in case of async, result dict in case of sync
        '''
        # Assemble module argument string
        module_args = list()
        if args:
            module_args += list(args)
        module_args = ' '.join(module_args)

        # pop async parameters
        async = kwargs.pop('run_async', False)
        time_limit = kwargs.pop('time_limit', 60)

        # Build module runner object
        options = dict(
            inventory=self.inventory_manager,
            pattern=self.pattern,
            callbacks=AnsibleRunnerCallback(),
            module_name=self.module_name,
            module_args=module_args,
            complex_args=kwargs,
            forks=kwargs.pop('forks', C.DEFAULT_FORKS),
            remote_user=kwargs.pop('remote_user', C.DEFAULT_REMOTE_USER),
            remote_pass=kwargs.pop('remote_pass', C.DEFAULT_REMOTE_PASS),
            remote_port=kwargs.pop('remote_port', None),
            transport=kwargs.pop('connection', C.DEFAULT_TRANSPORT),
        )

        if 'become_user' in kwargs:
            # Handle >= 1.9.0 options
            if has_ansible_become:
                options.update(dict(
                    become=True,
                    become_method=kwargs.pop('become_method', C.DEFAULT_BECOME_METHOD),
                    become_user=kwargs.pop('become_user', C.DEFAULT_BECOME_USER)
                ))
            else:
                options.update(dict(
                    sudo=True,
                    sudo_user=kwargs.pop('become_user', C.DEFAULT_BECOME_USER))
                )

        runner = Runner(**options)

        # Run the module
        if async:
            res, poll = runner.run_async(time_limit=time_limit)
            return _ExtendedPoll(res, poll)
        else:
            return _ExtendedPoll(runner.run(), None).poll()
开发者ID:autostack,项目名称:pytest-ansible,代码行数:65,代码来源:wrappers.py


注:本文中的ansible.runner.Runner.run_async方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。