2014-11-14 22:14:08 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2015-10-19 18:42:46 +00:00
|
|
|
from ansible.errors import AnsibleError
|
2015-08-05 15:52:52 +00:00
|
|
|
from ansible.playbook.included_file import IncludedFile
|
2016-02-05 16:19:50 +00:00
|
|
|
from ansible.plugins import action_loader
|
2015-09-15 15:57:54 +00:00
|
|
|
from ansible.plugins.strategy import StrategyBase
|
2016-02-05 16:19:50 +00:00
|
|
|
from ansible.template import Templar
|
2015-07-23 14:24:50 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-11-11 18:19:58 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
class StrategyModule(StrategyBase):
|
|
|
|
|
2015-07-21 16:12:22 +00:00
|
|
|
def run(self, iterator, play_context):
|
2014-11-14 22:14:08 +00:00
|
|
|
'''
|
|
|
|
The "free" strategy is a bit more complex, in that it allows tasks to
|
|
|
|
be sent to hosts as quickly as they can be processed. This means that
|
|
|
|
some hosts may finish very quickly if run tasks result in little or no
|
|
|
|
work being done versus other systems.
|
|
|
|
|
|
|
|
The algorithm used here also tries to be more "fair" when iterating
|
|
|
|
through hosts by remembering the last host in the list to be given a task
|
|
|
|
and starting the search from there as opposed to the top of the hosts
|
|
|
|
list again, which would end up favoring hosts near the beginning of the
|
|
|
|
list.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# the last host to be given a task
|
2015-07-24 00:47:24 +00:00
|
|
|
last_host = 0
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-04-05 06:05:17 +00:00
|
|
|
result = True
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
work_to_do = True
|
2015-01-12 22:04:56 +00:00
|
|
|
while work_to_do and not self._tqm._terminated:
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-02-11 02:27:14 +00:00
|
|
|
hosts_left = [host for host in self._inventory.get_hosts(iterator._play.hosts) if host.name not in self._tqm._unreachable_hosts and not iterator.is_failed(host)]
|
2014-11-14 22:14:08 +00:00
|
|
|
if len(hosts_left) == 0:
|
2015-04-05 06:05:17 +00:00
|
|
|
self._tqm.send_callback('v2_playbook_on_no_hosts_remaining')
|
|
|
|
result = False
|
2014-11-14 22:14:08 +00:00
|
|
|
break
|
|
|
|
|
2015-04-05 06:05:17 +00:00
|
|
|
work_to_do = False # assume we have no more work to do
|
|
|
|
starting_host = last_host # save current position so we know when we've
|
|
|
|
# looped back around and need to break
|
|
|
|
|
|
|
|
# try and find an unblocked host with a task to run
|
|
|
|
host_results = []
|
|
|
|
while True:
|
|
|
|
host = hosts_left[last_host]
|
2015-11-11 18:19:58 +00:00
|
|
|
display.debug("next free host: %s" % host)
|
2015-04-05 06:05:17 +00:00
|
|
|
host_name = host.get_name()
|
|
|
|
|
|
|
|
# peek at the next task for the host, to see if there's
|
|
|
|
# anything to do do for this host
|
|
|
|
(state, task) = iterator.get_next_task_for_host(host, peek=True)
|
2015-11-11 18:19:58 +00:00
|
|
|
display.debug("free host state: %s" % state)
|
|
|
|
display.debug("free host task: %s" % task)
|
2016-02-03 23:42:27 +00:00
|
|
|
if host_name not in self._tqm._unreachable_hosts and task:
|
2015-04-05 06:05:17 +00:00
|
|
|
|
|
|
|
# set the flag so the outer loop knows we've still found
|
|
|
|
# some work which needs to be done
|
|
|
|
work_to_do = True
|
|
|
|
|
2015-11-11 18:19:58 +00:00
|
|
|
display.debug("this host has work to do")
|
2015-04-05 06:05:17 +00:00
|
|
|
|
|
|
|
# check to see if this host is blocked (still executing a previous task)
|
2015-11-11 18:19:58 +00:00
|
|
|
if host_name not in self._blocked_hosts or not self._blocked_hosts[host_name]:
|
2015-04-05 06:05:17 +00:00
|
|
|
# pop the task, mark the host blocked, and queue it
|
|
|
|
self._blocked_hosts[host_name] = True
|
|
|
|
(state, task) = iterator.get_next_task_for_host(host)
|
|
|
|
|
2016-02-05 16:19:50 +00:00
|
|
|
try:
|
|
|
|
action = action_loader.get(task.action, class_only=True)
|
|
|
|
except KeyError:
|
|
|
|
# we don't care here, because the action may simply not have a
|
|
|
|
# corresponding action plugin
|
|
|
|
action = None
|
|
|
|
|
2015-11-11 18:19:58 +00:00
|
|
|
display.debug("getting variables")
|
2015-04-05 06:05:17 +00:00
|
|
|
task_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, host=host, task=task)
|
2016-02-05 16:19:50 +00:00
|
|
|
self.add_tqm_variables(task_vars, play=iterator._play)
|
|
|
|
templar = Templar(loader=self._loader, variables=task_vars)
|
2015-11-11 18:19:58 +00:00
|
|
|
display.debug("done getting variables")
|
2015-04-05 06:05:17 +00:00
|
|
|
|
2016-02-05 16:19:50 +00:00
|
|
|
run_once = templar.template(task.run_once) or action and getattr(action, 'BYPASS_HOST_LOOP', False)
|
|
|
|
if run_once:
|
|
|
|
if action and getattr(action, 'BYPASS_HOST_LOOP', False):
|
|
|
|
raise AnsibleError("The '%s' module bypasses the host loop, which is currently not supported in the free strategy " \
|
|
|
|
"and would instead execute for every host in the inventory list." % task.action, obj=task._ds)
|
|
|
|
else:
|
|
|
|
display.warning("Using run_once with the free strategy is not currently supported. This task will still be " \
|
|
|
|
"executed for every host in the inventory list.")
|
|
|
|
|
2015-04-05 06:05:17 +00:00
|
|
|
# check to see if this task should be skipped, due to it being a member of a
|
|
|
|
# role which has already run (and whether that role allows duplicate execution)
|
2015-08-11 20:34:58 +00:00
|
|
|
if task._role and task._role.has_run(host):
|
2015-04-05 06:05:17 +00:00
|
|
|
# If there is no metadata, the default behavior is to not allow duplicates,
|
|
|
|
# if there is metadata, check to see if the allow_duplicates flag was set to true
|
|
|
|
if task._role._metadata is None or task._role._metadata and not task._role._metadata.allow_duplicates:
|
2015-11-11 18:19:58 +00:00
|
|
|
display.debug("'%s' skipped because role has already run" % task)
|
2015-04-05 06:05:17 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if task.action == 'meta':
|
2016-02-03 23:42:27 +00:00
|
|
|
self._execute_meta(task, play_context, iterator)
|
2015-04-05 06:05:17 +00:00
|
|
|
self._blocked_hosts[host_name] = False
|
|
|
|
else:
|
2015-07-24 00:47:24 +00:00
|
|
|
# handle step if needed, skip meta actions as they are used internally
|
|
|
|
if not self._step or self._take_step(task, host_name):
|
2016-01-15 18:14:27 +00:00
|
|
|
if task.any_errors_fatal:
|
|
|
|
display.warning("Using any_errors_fatal with the free strategy is not supported, as tasks are executed independently on each host")
|
2015-07-24 00:47:24 +00:00
|
|
|
self._tqm.send_callback('v2_playbook_on_task_start', task, is_conditional=False)
|
|
|
|
self._queue_task(host, task, task_vars, play_context)
|
2016-02-03 23:42:27 +00:00
|
|
|
else:
|
|
|
|
display.debug("%s is blocked, skipping for now" % host_name)
|
2015-04-05 06:05:17 +00:00
|
|
|
|
|
|
|
# move on to the next host and make sure we
|
|
|
|
# haven't gone past the end of our hosts list
|
|
|
|
last_host += 1
|
|
|
|
if last_host > len(hosts_left) - 1:
|
|
|
|
last_host = 0
|
|
|
|
|
|
|
|
# if we've looped around back to the start, break out
|
|
|
|
if last_host == starting_host:
|
|
|
|
break
|
|
|
|
|
2016-04-08 10:49:16 +00:00
|
|
|
results = self._process_pending_results(iterator)
|
2015-04-05 06:05:17 +00:00
|
|
|
host_results.extend(results)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-08-05 15:52:52 +00:00
|
|
|
try:
|
2015-12-17 00:12:05 +00:00
|
|
|
included_files = IncludedFile.process_include_results(
|
|
|
|
host_results,
|
|
|
|
self._tqm,
|
|
|
|
iterator=iterator,
|
|
|
|
inventory=self._inventory,
|
|
|
|
loader=self._loader,
|
|
|
|
variable_manager=self._variable_manager
|
|
|
|
)
|
2015-08-27 06:16:11 +00:00
|
|
|
except AnsibleError as e:
|
2015-08-05 15:52:52 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if len(included_files) > 0:
|
2015-12-02 09:55:04 +00:00
|
|
|
all_blocks = dict((host, []) for host in hosts_left)
|
2015-08-05 15:52:52 +00:00
|
|
|
for included_file in included_files:
|
2015-12-02 09:56:10 +00:00
|
|
|
display.debug("collecting new blocks for %s" % included_file)
|
2015-08-05 15:52:52 +00:00
|
|
|
try:
|
|
|
|
new_blocks = self._load_included_file(included_file, iterator=iterator)
|
2015-08-27 06:16:11 +00:00
|
|
|
except AnsibleError as e:
|
2015-08-05 15:52:52 +00:00
|
|
|
for host in included_file._hosts:
|
|
|
|
iterator.mark_host_failed(host)
|
2015-11-11 18:19:58 +00:00
|
|
|
display.warning(str(e))
|
2015-08-05 15:52:52 +00:00
|
|
|
continue
|
|
|
|
|
2015-11-16 22:13:55 +00:00
|
|
|
for new_block in new_blocks:
|
|
|
|
task_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, task=included_file._task)
|
|
|
|
final_block = new_block.filter_tagged_tasks(play_context, task_vars)
|
|
|
|
for host in hosts_left:
|
|
|
|
if host in included_file._hosts:
|
|
|
|
all_blocks[host].append(final_block)
|
2015-12-02 09:56:10 +00:00
|
|
|
display.debug("done collecting new blocks for %s" % included_file)
|
2015-11-16 22:13:55 +00:00
|
|
|
|
2015-12-02 09:56:10 +00:00
|
|
|
display.debug("adding all collected blocks from %d included file(s) to iterator" % len(included_files))
|
2015-11-16 22:13:55 +00:00
|
|
|
for host in hosts_left:
|
|
|
|
iterator.add_tasks(host, all_blocks[host])
|
2015-12-02 09:56:10 +00:00
|
|
|
display.debug("done adding collected blocks to iterator")
|
2015-08-05 15:52:52 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
# pause briefly so we don't spin lock
|
2016-01-02 05:31:09 +00:00
|
|
|
time.sleep(0.001)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
# run the base class run() method, which executes the cleanup function
|
|
|
|
# and runs any outstanding handlers which have been triggered
|
2015-08-03 18:40:16 +00:00
|
|
|
return super(StrategyModule, self).run(iterator, play_context, result)
|