added handlers_from (#49220)

* added handlers_from

fixes #46769
This commit is contained in:
Brian Coca 2018-12-18 11:28:24 -05:00 committed by GitHub
parent 1e415899ad
commit 88c87a3583
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 72 additions and 2 deletions

View file

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

View file

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

View file

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

View file

@ -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, 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) 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: if handler_data:
try: try:
self._handler_blocks = load_list_of_blocks(handler_data, play=self._play, role=self, use_handlers=True, loader=self._loader, self._handler_blocks = load_list_of_blocks(handler_data, play=self._play, role=self, use_handlers=True, loader=self._loader,

View file

@ -41,7 +41,7 @@ class IncludeRole(TaskInclude):
""" """
BASE = ('name', 'role') # directly assigned 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 OTHER_ARGS = ('apply', 'public', 'allow_duplicates') # assigned to matching property
VALID_ARGS = tuple(frozenset(BASE + FROM_ARGS + OTHER_ARGS)) # all valid args VALID_ARGS = tuple(frozenset(BASE + FROM_ARGS + OTHER_ARGS)) # all valid args

View file

@ -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']

View file

@ -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 }}

View file

@ -2,7 +2,12 @@
set -eux set -eux
# simple handler test
ansible-playbook test_handlers.yml -i inventory.handlers -v "$@" --tags scenario1 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_listening_handlers.yml -i inventory.handlers -v "$@"
[ "$(ansible-playbook test_handlers.yml -i inventory.handlers -v "$@" --tags scenario2 -l A \ [ "$(ansible-playbook test_handlers.yml -i inventory.handlers -v "$@" --tags scenario2 -l A \