From ca8668f08b5c87115df50c66dadc3528faa69e88 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 28 Oct 2017 23:47:27 -0500 Subject: [PATCH] Use playbook callbacks for adhoc commands too This makes adhoc mirror playbook callback functionality by running a callback before and after all tasks have run. Adhoc commands now call: - v2_playbook_on_start - v2_playbook_on_stats NOTE: When v2_playbook_on_start is called, a dummy playbook is provided that says its _file_name is __adhoc_playbook__. All callback plugins that provide v2_playbook_on_start access the _file_name attribute, so this should maintain backward compatibility when those plugins are called with adhoc commands even though they would not have been called previously. The adhoc play is also added to _entries for any private callback plugins that might be using this callback. --- lib/ansible/cli/adhoc.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/ansible/cli/adhoc.py b/lib/ansible/cli/adhoc.py index 807c4ca4128..8611db458a4 100644 --- a/lib/ansible/cli/adhoc.py +++ b/lib/ansible/cli/adhoc.py @@ -28,6 +28,7 @@ from ansible.errors import AnsibleError, AnsibleOptionsError from ansible.executor.task_queue_manager import TaskQueueManager from ansible.module_utils._text import to_text from ansible.parsing.splitter import parse_kv +from ansible.playbook import Playbook from ansible.playbook.play import Play from ansible.plugins.loader import get_all_plugin_loaders @@ -145,6 +146,11 @@ class AdHocCLI(CLI): play_ds = self._play_ds(pattern, self.options.seconds, self.options.poll_interval) play = Play().load(play_ds, variable_manager=variable_manager, loader=loader) + # used in start callback + playbook = Playbook(loader) + playbook._entries.append(play) + playbook._file_name = '__adhoc_playbook__' + if self.callback: cb = self.callback elif self.options.one_line: @@ -175,7 +181,11 @@ class AdHocCLI(CLI): run_tree=run_tree, ) + self._tqm.send_callback('v2_playbook_on_start', playbook) + result = self._tqm.run(play) + + self._tqm.send_callback('v2_playbook_on_stats', self._tqm._stats) finally: if self._tqm: self._tqm.cleanup()