From f8c40cc26f353fda76577afdeee64cdb99b21f96 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Mon, 2 Apr 2018 16:07:17 -0400 Subject: [PATCH] properly support `v2_playbook_on_notify` in ansible 2.5+ see: https://github.com/ansible/awx/issues/1705 see: https://github.com/ansible/tower/issues/1205 related: https://github.com/ansible/ansible/pull/32633 --- awx/lib/awx_display_callback/module.py | 11 +++++------ awx/lib/tests/test_display_callback.py | 27 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/awx/lib/awx_display_callback/module.py b/awx/lib/awx_display_callback/module.py index 368063d0d1..8b4c59c8b1 100644 --- a/awx/lib/awx_display_callback/module.py +++ b/awx/lib/awx_display_callback/module.py @@ -274,15 +274,14 @@ class BaseCallbackModule(CallbackBase): with self.capture_event_data('playbook_on_no_hosts_remaining'): super(BaseCallbackModule, self).v2_playbook_on_no_hosts_remaining() - def v2_playbook_on_notify(self, result, handler): - # NOTE: Not used by Ansible 2.x. + def v2_playbook_on_notify(self, handler, host): + # NOTE: Not used by Ansible < 2.5. event_data = dict( - host=result._host.get_name(), - task=result._task, - handler=handler, + host=host.get_name(), + handler=handler.get_name(), ) with self.capture_event_data('playbook_on_notify', **event_data): - super(BaseCallbackModule, self).v2_playbook_on_notify(result, handler) + super(BaseCallbackModule, self).v2_playbook_on_notify(handler, host) ''' ansible_stats is, retoractively, added in 2.2 diff --git a/awx/lib/tests/test_display_callback.py b/awx/lib/tests/test_display_callback.py index d8c7923108..e3287b2055 100644 --- a/awx/lib/tests/test_display_callback.py +++ b/awx/lib/tests/test_display_callback.py @@ -28,6 +28,7 @@ CALLBACK = os.path.splitext(os.path.basename(__file__))[0] PLUGINS = os.path.dirname(__file__) with mock.patch.dict(os.environ, {'ANSIBLE_STDOUT_CALLBACK': CALLBACK, 'ANSIBLE_CALLBACK_PLUGINS': PLUGINS}): + from ansible import __version__ as ANSIBLE_VERSION from ansible.cli.playbook import PlaybookCLI from ansible.executor.playbook_executor import PlaybookExecutor from ansible.inventory.manager import InventoryManager @@ -284,3 +285,29 @@ def test_callback_plugin_saves_custom_stats(executor, cache, playbook): assert json.load(f) == {'foo': 'bar'} finally: shutil.rmtree(os.path.join(private_data_dir)) + + +@pytest.mark.parametrize('playbook', [ +{'handle_playbook_on_notify.yml': ''' +- name: handle playbook_on_notify events properly + connection: local + hosts: all + handlers: + - name: my_handler + debug: msg="My Handler" + tasks: + - debug: msg="My Task" + changed_when: true + notify: + - my_handler +'''}, # noqa +]) +@pytest.mark.skipif(ANSIBLE_VERSION < '2.5', reason="v2_playbook_on_notify doesn't work before ansible 2.5") +def test_callback_plugin_records_notify_events(executor, cache, playbook): + executor.run() + assert len(cache) + notify_events = [x[1] for x in cache.items() if x[1]['event'] == 'playbook_on_notify'] + assert len(notify_events) == 1 + assert notify_events[0]['event_data']['handler'] == 'my_handler' + assert notify_events[0]['event_data']['host'] == 'localhost' + assert notify_events[0]['event_data']['task'] == 'debug'