runner = ansible.runner.Runner(
module_name='ping',
module_args='',
pattern='web*',
forks=10
)
datastructure = runner.run()
ansible.executor.task_queue_manager
這是ansible的壹個內部模塊(ansible/executor/task_queue_manager.py)。初始化的源碼如下:
class TaskQueueManager:
'''
This class handles the multiprocessing requirements of Ansible by
creating a pool of worker forks, a result handler fork, and a
manager object with shared datastructures/queues for coordinating
work between all processes.
The queue manager is responsible for loading the play strategy plugin,
which dispatches the Play's tasks to hosts.
'''
def __init__(self, inventory, variable_manager, loader, options, passwords, stdout_callback=None, run_additional_callbacks=True, run_tree=False):self._inventory = inventoryself._variable_manager = variable_managerself._loader = loaderself._options = optionsself._stats = AggregateStats()self.passwords = passwordsself._stdout_callback = stdout_callbackself._run_additional_callbacks = run_additional_callbacksself._run_tree = run_treeself._callbacks_loaded = Falseself._callback_plugins = []self._start_at_done = Falseself._result_prc = None……
創建時,需要的主要參數包括:
inventory --> 由ansible.inventory模塊創建,用於導入inventory文件
variable_manager --> 由ansible.vars模塊創建,用於存儲各類變量信息
loader --> 由ansible.parsing.dataloader模塊創建,用於數據解析
options --> 存放各類配置信息的數據字典
passwords --> 登錄密碼,可設置加密信息
stdout_callback --> 回調函數
ansible.playbook.play
ansible.playbook是壹個原生模塊,既用於CLI也用於API。從源碼可以看出來:
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
ansible.playbook.play(ansible/playbook/play.py)。初始化源碼的介紹如下:
__all__ = ['Play']
class Play(Base, Taggable, Become):
"""
A play is a language feature that represents a list of roles and/or
task/handler blocks to execute on a given set of hosts.
Usage:
Play.load(datastructure) -> Play
Play.something(...)
"""
最後,用task_queue_manager(play)來執行,老規矩,源碼的官方解釋。
def run(self, play):'''Iterates over the roles/tasks in a play, using the given (or default)strategy for queueing tasks. The default is the linear strategy, whichoperates like classic Ansible by keeping all hosts in lock-step witha given task (meaning no hosts move on to the next task until all hostsare done with the current task).'''