执行流程 - ansible play 如何工作? [英] Execution flow - How ansible play works?

查看:16
本文介绍了执行流程 - ansible play 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的机器场景中:

我试图在文档中理解这一点:默认情况下,Ansible 在任何主机上开始下一个任务之前,在受播放影响的所有主机上运行每个任务,使用 5 个分叉."

比如说,task1是远程执行类型(不是本地执行类型)

如果play1需要在所有4台主机上运行task1(以上),那么

ansible 在 host2only 上执行 task1 后执行 (task1 on host1> 并检索执行结果)?如何使用 5 个分叉?

注意:每个任务执行都应该将 JSON 结果返回给 ansible 服务器.

解决方案

问:ansible 是不是只有在执行完(task1 在host1 上并获取执行结果)后才在host2 上执行task1?"

答:不是.

<块引用>

问:如何使用 5 个分叉?"

A:默认情况下,Ansible 在 5 个远程主机上并行运行 play.请参阅设置分叉数量.

<块引用>

问:试图理解:默认情况下,Ansible 在任何主机上开始下一个任务之前,先在受播放影响的所有主机上运行每个任务,..."

A:默认为 线性策略将同时执行每个任务",即所有分叉必须完成一项任务才能移动到下一个任务.有关其他策略,请参阅插件列表.

<块引用>

问:什么是锁步?"

A:"Lockstep" 描述了线性策略,即所有分叉必须完成一个任务(步骤)才能移动到下一个任务(分叉被锁定).引用:

锁步系统是容错的计算机系统,可以同时并行运行相同的一组操作."

<块引用>

问:fork 到底是什么?它是一个 python 线程吗?"

A:不可以.Ansible fork 不是 Python 线程.引用自 Glossary:

分叉:Ansible 与远程节点并行对话,并行级别可以通过传递 --forks 或编辑配置文件中的默认值来设置.默认值是非常保守的五 (5) 个分叉,但如果您有大量 RAM,您可以轻松地将其设置为 50 之类的值以提高并行度."

查看源代码.部分代码是线程化的.

$ grep -r "import threading" ansibleansible/test/lib/ansible_test/_internal/thread.py:导入线程ansible/lib/ansible/galaxy/collection.py:导入线程ansible/lib/ansible/plugins/callback/cgroup_perf_recap.py:导入线程ansible/lib/ansible/plugins/callback/cgroup_memory_recap.py:导入线程ansible/lib/ansible/plugins/strategy/__init__.py:import 线程

<块引用>

问:如何使用 Python 实现并行化?"

A:Ansible 中的并行性由 策略插件控制.例如 linear 插件:

"C(serial) 定义的每个主机批次的任务执行是锁步的(默认全部).直到主机的 fork 限制将同时执行每个任务,然后是下一个主机系列,直到在继续下一个任务之前,批次已完成."

查看线性插件

display.debug("为主机构建下一个任务列表")对于主机中的主机:host_tasks[host.name] = iterator.get_next_task_for_host(host, peek=True)...display.debug("计算每个执行状态的任务")host_tasks_to_run = [(host, state_task)对于主机, iteritems(host_tasks) 中的 state_task如果 state_task 和 state_task[1]]...对于 host_tasks_to_run 中的 (k, v):...

处理class StrategyBase 中的结果 线程.

#创建结果处理线程,用于后台读取结果self._results_thread = threading.Thread(target=results_thread_main, args=(self,))self._results_thread.daemon = Trueself._results_thread.start()

In the below machine scenario:

Am trying to understand this point in documentation: "By default, Ansible runs each task on all hosts affected by a play before starting the next task on any host, using 5 forks."

Say, task1 is remote execution type(not local execution type)

If play1 needs to run task1 on all 4 hosts(above), then

Does ansible execute task1 on host2 only after executing (task1 on host1 and retrieve the result of execution)? How are 5 forks utilised?

Note: Every task execution should provide JSON result back to ansible server.

解决方案

Q: "Does ansible execute task1 on host2 only after executing (task1 on host1 and retrieve the result of execution)?"

A: No.

Q: "How are 5 forks utilized?"

A: By default, Ansible runs the play at 5 remote hosts in parallel. See Setting the number of forks.

Q: "Trying to understand: By default, Ansible runs each task on all hosts affected by a play before starting the next task on any host,..."

A: The default is linear strategy "will execute each task at the same time" i.e. all forks must complete a task before moving to the next one. For other strategies see Plugin list.

Q: "What is lockstep?"

A: "Lockstep" describes the linear strategy i.e. all forks must complete a task(step) before moving to the next one(the forks are locked). Quoting:

"Lockstep systems are fault-tolerant computer systems that run the same set of operations at the same time in parallel."

Q: "What exactly are forks? Is it a python thread?"

A: No. Ansible fork is not a python thread. Quoting from Glosary:

"Forks: Ansible talks to remote nodes in parallel and the level of parallelism can be set either by passing --forks or editing the default in a configuration file. The default is a very conservative five (5) forks, though if you have a lot of RAM, you can easily set this to a value like 50 for increased parallelism."

Take a look at the source-code. Parts of the code are threaded.

$ grep -r "import threading" ansible
ansible/test/lib/ansible_test/_internal/thread.py:import threading
ansible/lib/ansible/galaxy/collection.py:import threading
ansible/lib/ansible/plugins/callback/cgroup_perf_recap.py:import threading
ansible/lib/ansible/plugins/callback/cgroup_memory_recap.py:import threading
ansible/lib/ansible/plugins/strategy/__init__.py:import threading

Q: "How parallelism is achieved using Python?"

A: The parallelism in Ansible is controlled by strategy plugins. For example the linear plugin:

"Task execution is in lockstep per host batch as defined by C(serial) (default all). Up to the fork limit of hosts will execute each task at the same time and then the next series of hosts until the batch is done, before going on to the next task."

See the elements in linear plugin

display.debug("building list of next tasks for hosts")
for host in hosts:
        host_tasks[host.name] = iterator.get_next_task_for_host(host, peek=True)
...

display.debug("counting tasks in each state of execution")
host_tasks_to_run = [(host, state_task)
                         for host, state_task in iteritems(host_tasks)
                         if state_task and state_task[1]]
...

for (k, v) in host_tasks_to_run:
...

Processing of the results in class StrategyBase is threaded.

# create the result processing thread for reading results in the background
self._results_thread = threading.Thread(target=results_thread_main, args=(self,))
self._results_thread.daemon = True
self._results_thread.start()

这篇关于执行流程 - ansible play 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆