Ansible 2.9的CHANGELOG裏是這麼描述的:
- Added new `throttle` keyword, which can be used at the task, block, or play level to limit the number of workers (up to the specified forks or serial setting) allowed.
定義在lib/ansible/playbook/base.py裏作爲Base的成員,也就是說Play,Task,Block都可以使用這個關鍵字。
它的用途主要體現在Strategy裏,跟StrategyBase.ALLOW_BASE_THROTTLING共同作用與WorkerProcess的創建。在StrategyBase中,ALLOW_BASE_THROTTLING默認爲True,也就是允許節流。註釋是這麼寫的(158-161行):
# by default, strategies should support throttling but we allow individual
# strategies to disable this and either forego supporting it or managing
# the throttling internally (as `free` does)
ALLOW_BASE_THROTTLING = True
在StrategyBase._queue_task裏原本的代碼上加了一塊:
# Determine the "rewind point" of the worker list. This means we start
# iterating over the list of workers until the end of the list is found.
# Normally, that is simply the length of the workers list (as determined
# by the forks or serial setting), however a task/block/play may "throttle"
# that limit down.
rewind_point = len(self._workers)
if throttle > 0 and self.ALLOW_BASE_THROTTLING:
if task.run_once:
display.debug("Ignoring 'throttle' as 'run_once' is also set for '%s'" % task.get_name())
else:
if throttle <= rewind_point:
display.debug("task: %s, throttle: %d" % (task.get_name(), throttle))
rewind_point = throttle
if self._cur_worker >= rewind_point:
self._cur_worker = 0
簡單解釋就是throttle的值是在作用在forks基礎之上的,因爲forks是個選項,寫在配置文件裏的默認值是5。
Ansible啓動的時候還是按照forks的大小去創建workers列表,但這個列表裏具體有多少個槽位能用,是取決於throttle的。由於throttle可以在play,block,task中使用,然而至少就我們目前的應用場景而言,這個關鍵字比較雞肋,我還沒想到它能有啥用。