简体   繁体   中英

Execution flow - How ansible play works?

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" ie 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 ie 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM