# Buggy code
def run(self, iterator, play_context):
    """
    The linear strategy is simple - get the next task and queue
    it for all hosts, then wait for the queue to drain before
    moving on to the next task
    """

    # iteratate over each task, while there is one left to run
    result = True
    work_to_do = True
    while work_to_do and not self._tqm._terminated:
        try:
            display.debug("getting the remaining hosts for this loop")
            hosts_left = [
                host
                for host in self._inventory.get_hosts(iterator._play.hosts)
                if host.name not in self._tqm._unreachable_hosts
            ]
            display.debug("done getting the remaining hosts for this loop")

            # queue up this task for each host in the inventory
            callback_sent = False
            work_to_do = False

            host_results = []
            host_tasks = self._get_next_task_lockstep(hosts_left, iterator)

            # skip control
            skip_rest = False
            choose_step = True

            results = []
            for host, task in host_tasks:
                if not task:
                    continue

                if self._tqm._terminated:
                    break

                run_once = False
                work_to_do = True

                # test to see if the task across all hosts points to an action plugin which
                # sets BYPASS_HOST_LOOP to true, or if it has run_once enabled. If so, we
                # will only send this task to the first host in the list.

                try:
                    action = action_loader.get(task.action, class_only=True)
                    if task.run_once or getattr(action, "BYPASS_HOST_LOOP", False):
                        run_once = True
                except KeyError:
                    # we don't care here, because the action may simply not have a
                    # corresponding action plugin
                    pass

                # 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)
                if task._role and task._role.has_run(host):
                    # 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
                    ):
                        display.debug(
                            "'%s' skipped because role has already run" % task
                        )
                        continue

                if task.action == "meta":
                    self._execute_meta(task, play_context, iterator)
                else:
                    # handle step if needed, skip meta actions as they are used internally
                    if self._step and choose_step:
                        if self._take_step(task):
                            choose_step = False
                        else:
                            skip_rest = True
                            break

                    display.debug("getting variables")
                    task_vars = self._variable_manager.get_vars(
                        loader=self._loader, play=iterator._play, host=host, task=task
                    )
                    self.add_tqm_variables(task_vars, play=iterator._play)
                    templar = Templar(loader=self._loader, variables=task_vars)
                    display.debug("done getting variables")

                    if not callback_sent:
                        display.debug(
                            "sending task start callback, copying the task so we can template it temporarily"
                        )
                        saved_name = task.name
                        display.debug("done copying, going to template now")
                        try:
                            task.name = text_type(
                                templar.template(task.name, fail_on_undefined=False)
                            )
                            display.debug("done templating")
                        except:
                            # just ignore any errors during task name templating,
                            # we don't care if it just shows the raw name
                            display.debug("templating failed for some reason")
                            pass
                        display.debug("here goes the callback...")
                        self._tqm.send_callback(
                            "v2_playbook_on_task_start", task, is_conditional=False
                        )
                        task.name = saved_name
                        callback_sent = True
                        display.debug("sending task start callback")

                    self._blocked_hosts[host.get_name()] = True
                    self._queue_task(host, task, task_vars, play_context)

                # if we're bypassing the host loop, break out now
                if run_once:
                    break

                results += self._process_pending_results(iterator, one_pass=True)

            # go to next host/task group
            if skip_rest:
                continue

            display.debug(
                "done queuing things up, now waiting for results queue to drain"
            )
            results += self._wait_on_pending_results(iterator)
            host_results.extend(results)

            if not work_to_do and len(iterator.get_failed_hosts()) > 0:
                display.debug("out of hosts to run on")
                self._tqm.send_callback("v2_playbook_on_no_hosts_remaining")
                result = False
                break

            try:
                included_files = IncludedFile.process_include_results(
                    host_results,
                    self._tqm,
                    iterator=iterator,
                    loader=self._loader,
                    variable_manager=self._variable_manager,
                )
            except AnsibleError as e:
                return False

            if len(included_files) > 0:
                display.debug("we have included files to process")
                noop_task = Task()
                noop_task.action = "meta"
                noop_task.args["_raw_params"] = "noop"
                noop_task.set_loader(iterator._play._loader)

                display.debug("generating all_blocks data")
                all_blocks = dict((host, []) for host in hosts_left)
                display.debug("done generating all_blocks data")
                for included_file in included_files:
                    display.debug(
                        "processing included file: %s" % included_file._filename
                    )
                    # included hosts get the task list while those excluded get an equal-length
                    # list of noop tasks, to make sure that they continue running in lock-step
                    try:
                        new_blocks = self._load_included_file(
                            included_file, iterator=iterator
                        )

                        display.debug(
                            "iterating over new_blocks loaded from include file"
                        )
                        for new_block in new_blocks:
                            task_vars = self._variable_manager.get_vars(
                                loader=self._loader,
                                play=iterator._play,
                                task=included_file._task,
                            )
                            display.debug("filtering new block on tags")
                            final_block = new_block.filter_tagged_tasks(
                                play_context, task_vars
                            )
                            display.debug("done filtering new block on tags")

                            noop_block = Block(parent_block=task._block)
                            noop_block.block = [noop_task for t in new_block.block]
                            noop_block.always = [noop_task for t in new_block.always]
                            noop_block.rescue = [noop_task for t in new_block.rescue]

                            for host in hosts_left:
                                if host in included_file._hosts:
                                    all_blocks[host].append(final_block)
                                else:
                                    all_blocks[host].append(noop_block)
                        display.debug(
                            "done iterating over new_blocks loaded from include file"
                        )

                    except AnsibleError as e:
                        for host in included_file._hosts:
                            self._tqm._failed_hosts[host.name] = True
                            iterator.mark_host_failed(host)
                        display.error(e, wrap_text=False)
                        continue

                # finally go through all of the hosts and append the
                # accumulated blocks to their list of tasks
                display.debug("extending task lists for all hosts with included blocks")

                for host in hosts_left:
                    iterator.add_tasks(host, all_blocks[host])

                display.debug("done extending task lists")
                display.debug("done processing included files")

            display.debug("results queue empty")
        except (IOError, EOFError) as e:
            display.debug("got IOError/EOFError in task loop: %s" % e)
            # most likely an abort, return failed
            return False

    # run the base class run() method, which executes the cleanup function
    # and runs any outstanding handlers which have been triggered

    return super(StrategyModule, self).run(iterator, play_context, result)


# Traceback (TypeError)
Unexpected Exception: _do_handler_run() takes at least 5 arguments (5 given)
the full traceback was:

Traceback (most recent call last):
File "/usr/bin/ansible-playbook", line 84, in <module>
sys.exit(cli.run())
File "/usr/lib/python2.7/site-packages/ansible/cli/playbook.py", line 149, in run
results = pbex.run()
File "/usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 149, in run
result = self._tqm.run(play=play)
File "/usr/lib/python2.7/site-packages/ansible/executor/task_queue_manager.py", line 249, in run
play_return = strategy.run(iterator, play_context)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/linear.py", line 205, in run
self._execute_meta(task, play_context, iterator)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 641, in _execute_meta
self.run_handlers(iterator, play_context)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 528, in run_handlers
result = self._do_handler_run(handler, handler_name, iterator=iterator, play_context=play_context)
File "/usr/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 592, in _do_handler_run
notified_hosts=included_file._hosts[:],
TypeError: _do_handler_run() takes at least 5 arguments (5 given)

# Fixed code
def run(self, iterator, play_context):
    """
    The linear strategy is simple - get the next task and queue
    it for all hosts, then wait for the queue to drain before
    moving on to the next task
    """

    # iteratate over each task, while there is one left to run
    result = True
    work_to_do = True
    while work_to_do and not self._tqm._terminated:
        try:
            display.debug("getting the remaining hosts for this loop")
            hosts_left = [
                host
                for host in self._inventory.get_hosts(iterator._play.hosts)
                if host.name not in self._tqm._unreachable_hosts
            ]
            display.debug("done getting the remaining hosts for this loop")

            # queue up this task for each host in the inventory
            callback_sent = False
            work_to_do = False

            host_results = []
            host_tasks = self._get_next_task_lockstep(hosts_left, iterator)

            # skip control
            skip_rest = False
            choose_step = True

            results = []
            for host, task in host_tasks:
                if not task:
                    continue

                if self._tqm._terminated:
                    break

                run_once = False
                work_to_do = True

                # test to see if the task across all hosts points to an action plugin which
                # sets BYPASS_HOST_LOOP to true, or if it has run_once enabled. If so, we
                # will only send this task to the first host in the list.

                try:
                    action = action_loader.get(task.action, class_only=True)
                    if task.run_once or getattr(action, "BYPASS_HOST_LOOP", False):
                        run_once = True
                except KeyError:
                    # we don't care here, because the action may simply not have a
                    # corresponding action plugin
                    pass

                # 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)
                if task._role and task._role.has_run(host):
                    # 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
                    ):
                        display.debug(
                            "'%s' skipped because role has already run" % task
                        )
                        continue

                if task.action == "meta":
                    self._execute_meta(task, play_context, iterator)
                else:
                    # handle step if needed, skip meta actions as they are used internally
                    if self._step and choose_step:
                        if self._take_step(task):
                            choose_step = False
                        else:
                            skip_rest = True
                            break

                    display.debug("getting variables")
                    task_vars = self._variable_manager.get_vars(
                        loader=self._loader, play=iterator._play, host=host, task=task
                    )
                    self.add_tqm_variables(task_vars, play=iterator._play)
                    templar = Templar(loader=self._loader, variables=task_vars)
                    display.debug("done getting variables")

                    if not callback_sent:
                        display.debug(
                            "sending task start callback, copying the task so we can template it temporarily"
                        )
                        saved_name = task.name
                        display.debug("done copying, going to template now")
                        try:
                            task.name = text_type(
                                templar.template(task.name, fail_on_undefined=False)
                            )
                            display.debug("done templating")
                        except:
                            # just ignore any errors during task name templating,
                            # we don't care if it just shows the raw name
                            display.debug("templating failed for some reason")
                            pass
                        display.debug("here goes the callback...")
                        self._tqm.send_callback(
                            "v2_playbook_on_task_start", task, is_conditional=False
                        )
                        task.name = saved_name
                        callback_sent = True
                        display.debug("sending task start callback")

                    self._blocked_hosts[host.get_name()] = True
                    self._queue_task(host, task, task_vars, play_context)

                # if we're bypassing the host loop, break out now
                if run_once:
                    break

                results += self._process_pending_results(iterator, one_pass=True)

            # go to next host/task group
            if skip_rest:
                continue

            display.debug(
                "done queuing things up, now waiting for results queue to drain"
            )
            results += self._wait_on_pending_results(iterator)
            host_results.extend(results)

            if not work_to_do and len(iterator.get_failed_hosts()) > 0:
                display.debug("out of hosts to run on")
                self._tqm.send_callback("v2_playbook_on_no_hosts_remaining")
                result = False
                break

            try:
                included_files = IncludedFile.process_include_results(
                    host_results,
                    self._tqm,
                    iterator=iterator,
                    inventory=self._inventory,
                    loader=self._loader,
                    variable_manager=self._variable_manager,
                )
            except AnsibleError as e:
                return False

            if len(included_files) > 0:
                display.debug("we have included files to process")
                noop_task = Task()
                noop_task.action = "meta"
                noop_task.args["_raw_params"] = "noop"
                noop_task.set_loader(iterator._play._loader)

                display.debug("generating all_blocks data")
                all_blocks = dict((host, []) for host in hosts_left)
                display.debug("done generating all_blocks data")
                for included_file in included_files:
                    display.debug(
                        "processing included file: %s" % included_file._filename
                    )
                    # included hosts get the task list while those excluded get an equal-length
                    # list of noop tasks, to make sure that they continue running in lock-step
                    try:
                        new_blocks = self._load_included_file(
                            included_file, iterator=iterator
                        )

                        display.debug(
                            "iterating over new_blocks loaded from include file"
                        )
                        for new_block in new_blocks:
                            task_vars = self._variable_manager.get_vars(
                                loader=self._loader,
                                play=iterator._play,
                                task=included_file._task,
                            )
                            display.debug("filtering new block on tags")
                            final_block = new_block.filter_tagged_tasks(
                                play_context, task_vars
                            )
                            display.debug("done filtering new block on tags")

                            noop_block = Block(parent_block=task._block)
                            noop_block.block = [noop_task for t in new_block.block]
                            noop_block.always = [noop_task for t in new_block.always]
                            noop_block.rescue = [noop_task for t in new_block.rescue]

                            for host in hosts_left:
                                if host in included_file._hosts:
                                    all_blocks[host].append(final_block)
                                else:
                                    all_blocks[host].append(noop_block)
                        display.debug(
                            "done iterating over new_blocks loaded from include file"
                        )

                    except AnsibleError as e:
                        for host in included_file._hosts:
                            self._tqm._failed_hosts[host.name] = True
                            iterator.mark_host_failed(host)
                        display.error(e, wrap_text=False)
                        continue

                # finally go through all of the hosts and append the
                # accumulated blocks to their list of tasks
                display.debug("extending task lists for all hosts with included blocks")

                for host in hosts_left:
                    iterator.add_tasks(host, all_blocks[host])

                display.debug("done extending task lists")
                display.debug("done processing included files")

            display.debug("results queue empty")
        except (IOError, EOFError) as e:
            display.debug("got IOError/EOFError in task loop: %s" % e)
            # most likely an abort, return failed
            return False

    # run the base class run() method, which executes the cleanup function
    # and runs any outstanding handlers which have been triggered

    return super(StrategyModule, self).run(iterator, play_context, result)