From cfd8c685d15ec6bf350957d0acfbe878dcd979b4 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 27 May 2014 10:55:09 -0500 Subject: [PATCH] Correct issue of handlers running on all hosts incorrectly Also adds an integration test to catch this bug in the future. Fixes #7559 --- lib/ansible/playbook/__init__.py | 2 +- .../roles/test_handlers/meta/main.yml | 3 +-- .../roles/test_handlers/tasks/main.yml | 17 ++++++++++++++++- test/integration/test_handlers.yml | 11 ++++++----- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/ansible/playbook/__init__.py b/lib/ansible/playbook/__init__.py index 5eba7745c8d..bbec4d5fdc7 100644 --- a/lib/ansible/playbook/__init__.py +++ b/lib/ansible/playbook/__init__.py @@ -355,7 +355,7 @@ class PlayBook(object): def _run_task_internal(self, task): ''' run a particular module step in a playbook ''' - hosts = self._trim_unavailable_hosts(task.play._play_hosts) + hosts = self._trim_unavailable_hosts(self.inventory.list_hosts(task.play._play_hosts)) self.inventory.restrict_to(hosts) runner = ansible.runner.Runner( diff --git a/test/integration/roles/test_handlers/meta/main.yml b/test/integration/roles/test_handlers/meta/main.yml index 1050c23ce30..74d2c33354c 100644 --- a/test/integration/roles/test_handlers/meta/main.yml +++ b/test/integration/roles/test_handlers/meta/main.yml @@ -1,3 +1,2 @@ -dependencies: - - prepare_tests +dependencies: [] diff --git a/test/integration/roles/test_handlers/tasks/main.yml b/test/integration/roles/test_handlers/tasks/main.yml index e788f51ea2c..6f117681883 100644 --- a/test/integration/roles/test_handlers/tasks/main.yml +++ b/test/integration/roles/test_handlers/tasks/main.yml @@ -16,8 +16,23 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -- name: notify the handler + +- name: reset handler_called variable to false for all hosts + set_fact: + handler_called: False + +- name: notify the handler for host A only shell: echo notify: - set handler fact + when: inventory_hostname == 'A' + +- name: force handler execution now + meta: "flush_handlers" + +- debug: var=handler_called +- name: validate the handler only ran on one host + assert: + that: + - "inventory_hostname == 'A' and handler_called == True or handler_called == False" diff --git a/test/integration/test_handlers.yml b/test/integration/test_handlers.yml index dd766a9deaf..6a5366408c7 100644 --- a/test/integration/test_handlers.yml +++ b/test/integration/test_handlers.yml @@ -17,8 +17,9 @@ - "not hostvars[inventory_hostname]['handler1_called']" - "'handler2_called' in hostvars[inventory_hostname]" -#- hosts: testgroup -# gather_facts: False -# connection: local -# roles: -# - { role: test_handlers_meta } +- name: test handlers + hosts: testgroup + gather_facts: False + connection: local + roles: + - { role: test_handlers }