added handlers_from (#49220)

* added handlers_from

fixes #46769
pull/46528/head
Brian Coca 6 years ago committed by GitHub
parent 1e415899ad
commit 88c87a3583
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- add from_handlers option to include_role/import_role

@ -48,6 +48,11 @@ options:
- Overrides the role's metadata setting to allow using a role more than once with the same parameters.
type: bool
default: 'yes'
handlers_from:
description:
- File to load from a role's C(handlers/) directory.
default: main
version_added: '2.8'
notes:
- Handlers are made available to the whole play.
- "Since Ansible 2.7: variables defined in C(vars) and C(defaults) for the role are exposed at playbook parsing time.

@ -61,6 +61,11 @@ options:
type: bool
default: 'no'
version_added: '2.7'
handlers_from:
description:
- File to load from a role's C(handlers/) directory.
default: main
version_added: '2.8'
notes:
- Handlers are made available to the whole play.
- Before Ansible 2.4, as with C(include), this task could be static or dynamic, If static, it implied that it won't

@ -230,7 +230,7 @@ class Role(Base, Become, Conditional, Taggable):
raise AnsibleParserError("The tasks/main.yml file for role '%s' must contain a list of tasks" % self._role_name,
obj=task_data, orig_exc=e)
handler_data = self._load_role_yaml('handlers')
handler_data = self._load_role_yaml('handlers', main=self._from_files.get('handlers'))
if handler_data:
try:
self._handler_blocks = load_list_of_blocks(handler_data, play=self._play, role=self, use_handlers=True, loader=self._loader,

@ -41,7 +41,7 @@ class IncludeRole(TaskInclude):
"""
BASE = ('name', 'role') # directly assigned
FROM_ARGS = ('tasks_from', 'vars_from', 'defaults_from') # used to populate from dict in role
FROM_ARGS = ('tasks_from', 'vars_from', 'defaults_from', 'handlers_from') # used to populate from dict in role
OTHER_ARGS = ('apply', 'public', 'allow_duplicates') # assigned to matching property
VALID_ARGS = tuple(frozenset(BASE + FROM_ARGS + OTHER_ARGS)) # all valid args

@ -0,0 +1,41 @@
- name: verify handlers_from on include_role
hosts: A
gather_facts: False
connection: local
tags: ['scenario1']
tasks:
- name: test include_role
include_role: name=test_handlers_meta handlers_from=alternate.yml
- name: force handler run
meta: flush_handlers
- name: verify handlers ran
assert:
that:
- "'handler1_alt_called' in hostvars[inventory_hostname]"
- "'handler2_alt_called' in hostvars[inventory_hostname]"
tags: ['scenario1']
- name: verify handlers_from on import_role
hosts: A
gather_facts: False
connection: local
tasks:
- name: set facts to false
set_fact:
handler1_alt_called: False
handler2_alt_called: False
- import_role: name=test_handlers_meta handlers_from=alternate.yml
- name: force handler run
meta: flush_handlers
- name: verify handlers ran
assert:
that:
- handler1_alt_called|bool
- handler2_alt_called|bool
tags: ['scenario1']

@ -0,0 +1,12 @@
- name: set_handler_fact_1
set_fact:
handler1_called: True
handler1_alt_called: True
- name: set_handler_fact_2
set_fact:
handler2_called: True
handler2_alt_called: True
- name: count_handler
shell: echo . >> {{ handler_countpath }}

@ -2,7 +2,12 @@
set -eux
# simple handler test
ansible-playbook test_handlers.yml -i inventory.handlers -v "$@" --tags scenario1
# simple from_handlers test
ansible-playbook from_handlers.yml -i inventory.handlers -v "$@" --tags scenario1
ansible-playbook test_listening_handlers.yml -i inventory.handlers -v "$@"
[ "$(ansible-playbook test_handlers.yml -i inventory.handlers -v "$@" --tags scenario2 -l A \

Loading…
Cancel
Save