Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for group variables to handlers #17978

Closed

Conversation

byrondover
Copy link

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

Ansible Core

ANSIBLE VERSION
ansible 2.3.0 (devel 0a86ddc251) last updated 2016/10/05 13:51:38 (GMT +200)
  lib/ansible/modules/core: (detached HEAD 0ee774ff15) last updated 2016/10/05 13:51:46 (GMT +200)
  lib/ansible/modules/extras: (detached HEAD 5cc72c3f06) last updated 2016/10/05 13:51:53 (GMT +200)
  config file = 
  configured module search path = Default w/o overrides
SUMMARY

Fixes #15103.

Moves variables['groups'] assignment from if hosts: scope to if self._inventory: scope, to better support task delegation within handlers (e.g. using with_items or with_inventory_hostnames).

Example role and bug fix illustrated below.

% cat development

[webservers]
master1.foobarsites.com

[slaves]
slave1.foobarsites.com
slave2.foobarsites.com
slave2.foobarsites.com
% cat roles/test_role/tasks/main.yml

- name: Trigger handlers.
  command: true
  notify: test_handler
% cat roles/test_role/handlers/main.yml

- name: test_handler
  command: true
  delegate_to: "{{ item }}"
  with_inventory_hostnames: slaves

Before [Doesn't Work]

% ansible-playbook -i hosts/dev site.yml

PLAYBOOK: test.yml *************************************************************
1 plays in test.yml

PLAY [webservers] **************************************************************

TASK [test_role : Trigger handlers.] *******************************************

ERROR! Unexpected Exception: 'groups'
the full traceback was:

Traceback (most recent call last):
  File "/usr/local/bin/ansible-playbook", line 92, in <module>
    exit_code = cli.run()
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/cli/playbook.py", line 154, in run
    results = pbex.run()
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 146, in run
    result = self._tqm.run(play=play)
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/executor/task_queue_manager.py", line 264, in run
    play_return = strategy.run(iterator, play_context)
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/plugins/strategy/linear.py", line 271, in run
    results += self._wait_on_pending_results(iterator)
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 478, in _wait_on_pending_results
    results = self._process_pending_results(iterator)
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 371, in _process_pending_results
    target_handler = search_handler_blocks(iterator._play.handlers)
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/plugins/strategy/__init__.py", line 326, in search_handler_blocks
    handler_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, task=handler_task)
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/vars/__init__.py", line 379, in get_vars
    all_vars['ansible_delegated_vars'] = self._get_delegated_vars(loader, play, task, all_vars)
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/vars/__init__.py", line 456, in _get_delegated_vars
    items = lookup_loader.get(task.loop, loader=loader, templar=templar).run(terms=loop_terms, variables=vars_copy)
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/plugins/lookup/inventory_hostnames.py", line 49, in run
    that = self.get_hosts(variables, p)
  File "/usr/local/Cellar/ansible/2.1.2.0/libexec/lib/python2.7/site-packages/ansible/plugins/lookup/inventory_hostnames.py", line 35, in get_hosts
    if obj in variables['groups']:
KeyError: 'groups'

After [Works as Expected]

% ansible-playbook -i hosts/dev site.yml

PLAYBOOK: test.yml *************************************************************
1 plays in test.yml

PLAY [webservers] **************************************************************

TASK [test_role : Trigger handlers.] *******************************************
changed: [localhost.foobarsites.com]

RUNNING HANDLER [test_role : test_handler] *************************************
changed: [master1.foobarsites.com -> slave1.foobarsites.com] => (item=slave1.foobarsites.com)
changed: [master1.foobarsites.com -> slave2.foobarsites.com] => (item=slave2.foobarsites.com)
changed: [master1.foobarsites.com -> slave3.foobarsites.com] => (item=slave3.foobarsites.com)

PLAY RECAP *********************************************************************
localhost.foobarsites.com  : ok=0    changed=1    unreachable=0    failed=0

@bcoca
Copy link
Member

bcoca commented Oct 25, 2016

I don't think this is the correct fix, it seems we should not try to compile the full handler until it is time to execute and the variables are available in that context.

cc @jimi-c

@bcoca bcoca added the needs_info This issue requires further information. Please answer any outstanding questions. label Oct 25, 2016
@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 bugfix_pullrequest labels Dec 13, 2016
@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Dec 16, 2016
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 3, 2017
@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Mar 21, 2017
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Apr 11, 2017
@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Jun 29, 2017
@ansibot ansibot added the new_contributor This PR is the first contribution by a new community member. label Oct 18, 2017
@ansibot ansibot removed the new_contributor This PR is the first contribution by a new community member. label Nov 3, 2017
@ansibot ansibot added the new_contributor This PR is the first contribution by a new community member. label Jan 22, 2018
@ansibot
Copy link
Contributor

ansibot commented Jan 31, 2018

@byrondover This pullrequest is waiting for your response. Please respond or the pullrequest will be closed.

click here for bot help

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bugfix_pull_request labels Mar 2, 2018
@ansibot
Copy link
Contributor

ansibot commented Mar 10, 2018

@byrondover This pullrequest is waiting for your response. Please respond or the pullrequest will be closed.

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Mar 18, 2018

@byrondover You have not responded to information requests in this pullrequest so we will assume it no longer affects you. If you are still interested in this, please create a new pullrequest with the requested information.

click here for bot help

@ansibot ansibot closed this Mar 18, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions. needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. new_contributor This PR is the first contribution by a new community member. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handlers delegated to a list of items fails when "with_items" is provided with a fact
3 participants