# Buggy code
def _execute_meta(self, task, play_context, iterator, target_host):
    # meta tasks store their args in the _raw_params field of args,
    # since they do not use k=v pairs, so get that
    meta_action = task.args.get("_raw_params")

    # FIXME(s):
    # * raise an error or show a warning when a conditional is used
    #   on a meta task that doesn't support them

    def _evaluate_conditional(h):
        all_vars = self._variable_manager.get_vars(
            play=iterator._play, host=h, task=task
        )
        templar = Templar(loader=self._loader, variables=all_vars)
        return task.evaluate_conditional(templar, all_vars)

    skipped = False
    msg = ""
    if meta_action == "noop":
        # FIXME: issue a callback for the noop here?
        msg = "noop"
    elif meta_action == "flush_handlers":
        self.run_handlers(iterator, play_context)
        msg = "ran handlers"
    elif meta_action == "refresh_inventory":
        self._inventory.refresh_inventory()
        msg = "inventory successfully refreshed"
    elif meta_action == "clear_facts":
        if _evaluate_conditional(target_host):
            for host in self._inventory.get_hosts(iterator._play.hosts):
                hostname = host.get_name()
                self._variable_manager.clear_facts(hostname)
            msg = "facts cleared"
        else:
            skipped = True
    elif meta_action == "clear_host_errors":
        if _evaluate_conditional(target_host):
            for host in self._inventory.get_hosts(iterator._play.hosts):
                self._tqm._failed_hosts.pop(host.name, False)
                self._tqm._unreachable_hosts.pop(host.name, False)
                iterator._host_states[host.name].fail_state = iterator.FAILED_NONE
            msg = "cleared host errors"
        else:
            skipped = True
    elif meta_action == "end_play":
        if _evaluate_conditional(target_host):
            for host in self._inventory.get_hosts(iterator._play.hosts):
                if host.name not in self._tqm._unreachable_hosts:
                    iterator._host_states[
                        host.name
                    ].run_state = iterator.ITERATING_COMPLETE
            msg = "ending play"
    elif meta_action == "reset_connection":
        if target_host in self._active_connections:
            connection = Connection(self._active_connections[target_host])
            del self._active_connections[target_host]
        else:
            connection = connection_loader.get(
                play_context.connection, play_context, os.devnull
            )
            play_context.set_options_from_plugin(connection)

        if connection:
            connection.reset()
            msg = "reset connection"
        else:
            msg = "no connection, nothing to reset"
    else:
        raise AnsibleError(
            "invalid meta action requested: %s" % meta_action, obj=task._ds
        )

    result = {"msg": msg}
    if skipped:
        result["skipped"] = True
    else:
        result["changed"] = False

    display.vv("META: %s" % msg)

    return [TaskResult(target_host, task, result)]


# Traceback (ConnectionError)
ERROR! Unexpected Exception, this is probably a bug: socket_path does not exist or cannot be found
the full traceback was:

Traceback (most recent call last):
File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/bin/ansible-playbook", line 109, in <module>
exit_code = cli.run()
File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/cli/playbook.py", line 130, in run
results = pbex.run()
File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/executor/playbook_executor.py", line 154, in run
result = self._tqm.run(play=play)
File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/executor/task_queue_manager.py", line 290, in run
play_return = strategy.run(iterator, play_context)
File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/plugins/strategy/linear.py", line 235, in run
results.extend(self._execute_meta(task, play_context, iterator, host))
File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/plugins/strategy/__init__.py", line 916, in _execute_meta
connection.reset()
File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/module_utils/connection.py", line 140, in __rpc__
response = self._exec_jsonrpc(name, *args, **kwargs)
File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/module_utils/connection.py", line 115, in _exec_jsonrpc
raise ConnectionError('socket_path does not exist or cannot be found')
ConnectionError: socket_path does not exist or cannot be found

# Fixed code
def _execute_meta(self, task, play_context, iterator, target_host):
    # meta tasks store their args in the _raw_params field of args,
    # since they do not use k=v pairs, so get that
    meta_action = task.args.get("_raw_params")

    # FIXME(s):
    # * raise an error or show a warning when a conditional is used
    #   on a meta task that doesn't support them

    def _evaluate_conditional(h):
        all_vars = self._variable_manager.get_vars(
            play=iterator._play, host=h, task=task
        )
        templar = Templar(loader=self._loader, variables=all_vars)
        return task.evaluate_conditional(templar, all_vars)

    skipped = False
    msg = ""
    if meta_action == "noop":
        # FIXME: issue a callback for the noop here?
        msg = "noop"
    elif meta_action == "flush_handlers":
        self.run_handlers(iterator, play_context)
        msg = "ran handlers"
    elif meta_action == "refresh_inventory":
        self._inventory.refresh_inventory()
        msg = "inventory successfully refreshed"
    elif meta_action == "clear_facts":
        if _evaluate_conditional(target_host):
            for host in self._inventory.get_hosts(iterator._play.hosts):
                hostname = host.get_name()
                self._variable_manager.clear_facts(hostname)
            msg = "facts cleared"
        else:
            skipped = True
    elif meta_action == "clear_host_errors":
        if _evaluate_conditional(target_host):
            for host in self._inventory.get_hosts(iterator._play.hosts):
                self._tqm._failed_hosts.pop(host.name, False)
                self._tqm._unreachable_hosts.pop(host.name, False)
                iterator._host_states[host.name].fail_state = iterator.FAILED_NONE
            msg = "cleared host errors"
        else:
            skipped = True
    elif meta_action == "end_play":
        if _evaluate_conditional(target_host):
            for host in self._inventory.get_hosts(iterator._play.hosts):
                if host.name not in self._tqm._unreachable_hosts:
                    iterator._host_states[
                        host.name
                    ].run_state = iterator.ITERATING_COMPLETE
            msg = "ending play"
    elif meta_action == "reset_connection":
        if target_host in self._active_connections:
            connection = Connection(self._active_connections[target_host])
            del self._active_connections[target_host]
        else:
            connection = connection_loader.get(
                play_context.connection, play_context, os.devnull
            )
            play_context.set_options_from_plugin(connection)

        if connection:
            try:
                connection.reset()
                msg = "reset connection"
            except ConnectionError as e:
                # most likely socket is already closed
                display.debug(
                    "got an error while closing persistent connection: %s" % e
                )
        else:
            msg = "no connection, nothing to reset"
    else:
        raise AnsibleError(
            "invalid meta action requested: %s" % meta_action, obj=task._ds
        )

    result = {"msg": msg}
    if skipped:
        result["skipped"] = True
    else:
        result["changed"] = False

    display.vv("META: %s" % msg)

    return [TaskResult(target_host, task, result)]