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

Problem with handlers when reusing role in playbook #15084

Closed
mickael-ange opened this issue Mar 22, 2016 · 2 comments
Closed

Problem with handlers when reusing role in playbook #15084

mickael-ange opened this issue Mar 22, 2016 · 2 comments
Assignees
Labels
bug This issue/PR relates to a bug.
Milestone

Comments

@mickael-ange
Copy link
Contributor

Issue Type:
  • Bug Report
Ansible Version:
ansible 2.0.0.2
  config file = /home/mickael/git/emq-devops/ansible/ansible.cfg
  configured module search path = Default w/o overrides
Ansible Configuration:

n/a

Environment:

Running Ansible from Ubuntu 14.04
Managing OS: CentOS 7

Summary:

When reusing a role in playbook, a handler is not using expected role's variable value within the handler task.

(I'm not sure if it is the expected behavior or not. I'm suspecting that variables used in handlers are substituted when the handler is defined)

Steps To Reproduce:

Clone https://github.com/mickael-ange/ansible-reports and run:

ansible-playbook playbooks/test-handlers-01.yml
Expected Results:

The second handler call would have item=test2.

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [test-handlers-01 : always changes to trigger handler] ********************
changed: [localhost]

TASK [test-handlers-01 : include] **********************************************
included: /home/mickael/git/ansible-reports/roles/test-handlers-01/tasks/flush_handlers.yml for localhost

RUNNING HANDLER [test-handlers-01 : test handler] ******************************
changed: [localhost] => (item=test1)

TASK [test-handlers-01 : always changes to trigger handler] ********************
changed: [localhost]

TASK [test-handlers-01 : include] **********************************************
included: /home/mickael/git/ansible-reports/roles/test-handlers-01/tasks/flush_handlers.yml for localhost

RUNNING HANDLER [test-handlers-01 : test handler] ******************************
changed: [localhost] => (item=test2)

PLAY RECAP *********************************************************************
localhost                  : ok=7    changed=4    unreachable=0    failed=0   
Actual Results:

The actual result is that both handler calls use the same variable value (item=test1) even if the variable test_name has been redefined in the playbook.

$ ansible-playbook -vvvv playbooks/test-handlers-01.yml
Using /home/mickael/git/ansible-reports/ansible.cfg as config file
Loaded callback default of type stdout, v2.0
1 plays in playbooks/test-handlers-01.yml

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ESTABLISH LOCAL CONNECTION FOR USER: mickael
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621398.48-100196802537724 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621398.48-100196802537724 )" )
localhost PUT /tmp/tmp67Fgab TO /home/mickael/.ansible/tmp/ansible-tmp-1458621398.48-100196802537724/setup
localhost EXEC LANG=C LC_ALL=C LC_MESSAGES=C /usr/bin/python /home/mickael/.ansible/tmp/ansible-tmp-1458621398.48-100196802537724/setup; rm -rf "/home/mickael/.ansible/tmp/ansible-tmp-1458621398.48-100196802537724/" > /dev/null 2>&1
ok: [localhost]

TASK [test-handlers-01 : always changes to trigger handler] ********************
task path: /home/mickael/git/ansible-reports/roles/test-handlers-01/tasks/main.yml:2
ESTABLISH LOCAL CONNECTION FOR USER: mickael
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621398.81-97442582835398 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621398.81-97442582835398 )" )
localhost PUT /tmp/tmpGhth2G TO /home/mickael/.ansible/tmp/ansible-tmp-1458621398.81-97442582835398/command
localhost EXEC LANG=C LC_ALL=C LC_MESSAGES=C /usr/bin/python /home/mickael/.ansible/tmp/ansible-tmp-1458621398.81-97442582835398/command; rm -rf "/home/mickael/.ansible/tmp/ansible-tmp-1458621398.81-97442582835398/" > /dev/null 2>&1
NOTIFIED HANDLER test handler
changed: [localhost] => {"changed": true, "cmd": "echo hello", "delta": "0:00:00.000985", "end": "2016-03-22 12:36:38.842304", "invocation": {"module_args": {"_raw_params": "echo hello", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "rc": 0, "start": "2016-03-22 12:36:38.841319", "stderr": "", "stdout": "hello", "stdout_lines": ["hello"], "warnings": []}

TASK [test-handlers-01 : include] **********************************************
task path: /home/mickael/git/ansible-reports/roles/test-handlers-01/tasks/main.yml:7
included: /home/mickael/git/ansible-reports/roles/test-handlers-01/tasks/flush_handlers.yml for localhost

RUNNING HANDLER [test-handlers-01 : test handler] ******************************
ESTABLISH LOCAL CONNECTION FOR USER: mickael
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621398.89-30437666642483 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621398.89-30437666642483 )" )
localhost PUT /tmp/tmp0ZvLkG TO /home/mickael/.ansible/tmp/ansible-tmp-1458621398.89-30437666642483/command
localhost EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-galeggchoargiitnuhweljdxhuzvxyfk; LANG=C LC_ALL=C LC_MESSAGES=C /usr/bin/python /home/mickael/.ansible/tmp/ansible-tmp-1458621398.89-30437666642483/command; rm -rf "/home/mickael/.ansible/tmp/ansible-tmp-1458621398.89-30437666642483/" > /dev/null 2>&1'"'"''
changed: [localhost] => (item=test1) => {"changed": true, "cmd": "echo test1", "delta": "0:00:00.001024", "end": "2016-03-22 12:36:38.923978", "invocation": {"module_args": {"_raw_params": "echo test1", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "item": "test1", "rc": 0, "start": "2016-03-22 12:36:38.922954", "stderr": "", "stdout": "test1", "stdout_lines": ["test1"], "warnings": []}

TASK [test-handlers-01 : always changes to trigger handler] ********************
task path: /home/mickael/git/ansible-reports/roles/test-handlers-01/tasks/main.yml:2
ESTABLISH LOCAL CONNECTION FOR USER: mickael
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621398.94-276260811692367 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621398.94-276260811692367 )" )
localhost PUT /tmp/tmp86IG7E TO /home/mickael/.ansible/tmp/ansible-tmp-1458621398.94-276260811692367/command
localhost EXEC LANG=C LC_ALL=C LC_MESSAGES=C /usr/bin/python /home/mickael/.ansible/tmp/ansible-tmp-1458621398.94-276260811692367/command; rm -rf "/home/mickael/.ansible/tmp/ansible-tmp-1458621398.94-276260811692367/" > /dev/null 2>&1
NOTIFIED HANDLER test handler
changed: [localhost] => {"changed": true, "cmd": "echo hello", "delta": "0:00:00.001004", "end": "2016-03-22 12:36:38.968493", "invocation": {"module_args": {"_raw_params": "echo hello", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "rc": 0, "start": "2016-03-22 12:36:38.967489", "stderr": "", "stdout": "hello", "stdout_lines": ["hello"], "warnings": []}

TASK [test-handlers-01 : include] **********************************************
task path: /home/mickael/git/ansible-reports/roles/test-handlers-01/tasks/main.yml:7
included: /home/mickael/git/ansible-reports/roles/test-handlers-01/tasks/flush_handlers.yml for localhost

RUNNING HANDLER [test-handlers-01 : test handler] ******************************
ESTABLISH LOCAL CONNECTION FOR USER: mickael
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621399.02-117648891898111 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1458621399.02-117648891898111 )" )
localhost PUT /tmp/tmpBLQAIM TO /home/mickael/.ansible/tmp/ansible-tmp-1458621399.02-117648891898111/command
localhost EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-djwqhqqtijmpdsiqoxgfazrcxttepenq; LANG=C LC_ALL=C LC_MESSAGES=C /usr/bin/python /home/mickael/.ansible/tmp/ansible-tmp-1458621399.02-117648891898111/command; rm -rf "/home/mickael/.ansible/tmp/ansible-tmp-1458621399.02-117648891898111/" > /dev/null 2>&1'"'"''
changed: [localhost] => (item=test1) => {"changed": true, "cmd": "echo test1", "delta": "0:00:00.001034", "end": "2016-03-22 12:36:39.051023", "invocation": {"module_args": {"_raw_params": "echo test1", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "item": "test1", "rc": 0, "start": "2016-03-22 12:36:39.049989", "stderr": "", "stdout": "test1", "stdout_lines": ["test1"], "warnings": []}

PLAY RECAP *********************************************************************
localhost                  : ok=7    changed=4    unreachable=0    failed=0  
@jimi-c jimi-c added this to the stable-2.0 milestone Mar 26, 2016
@jimi-c
Copy link
Member

jimi-c commented Mar 26, 2016

I may have fixed this recently, but I'll look into it as soon as I can.

@jimi-c jimi-c self-assigned this Mar 26, 2016
@emanation
Copy link

emanation commented Jun 9, 2016

This is still same with Ansible 2.1
I cannot really use same role more than one time if handler looks like

- name: delete varnish container
  docker:
    image: "{{ docker_varnish_image_tag }}"
    name: "{{ docker_varnish_container_name }}"
    state: absent
    docker_api_version: "{{ docker_api_version }}"

docker_varnish_container_name variable is different each time I call this role like this in playbook

- { role: docker_varnish, docker_varnish_container_name: varnish-apps }
- { role: docker_varnish, docker_varnish_container_name: varnish-cache }

But it stops container with name that has been defined as very first in playbook.

I tried to use variable in handler's name but they are not triggered at all in that case.

Update:
This only works if I have this in role

- name: Set container name variable
  set_fact: docker_varnish_container_name={{container_name}}
  when: container_name is defined

and this in playbook

- { role: docker_varnish, container_name: varnish-apps }
- { role: docker_varnish, container_name: varnish-cache }

jimi-c added a commit that referenced this issue Jun 13, 2016
Due to the fact that roles may be instantiated with different sets of
params (multiple inclusions of the same role or via role dependencies),
simply tracking notified handlers by name does not work. This patch
changes the way we track handler notifications by using the handler
object itself instead of just the name, allowing for multiple internal
instances. Normally this would be bad, but we also modify the way we
search for handlers by first looking at the notifying tasks dependency
chain (ensuring that roles find their own handlers first) and then at
the main list of handlers, using the first match it finds.

This patch also modifies the way we setup the internal list of handlers,
which should allow us to correctly identify if a notified handler exists
more easily.

Fixes #15084
jimi-c added a commit that referenced this issue Jun 13, 2016
Due to the fact that roles may be instantiated with different sets of
params (multiple inclusions of the same role or via role dependencies),
simply tracking notified handlers by name does not work. This patch
changes the way we track handler notifications by using the handler
object itself instead of just the name, allowing for multiple internal
instances. Normally this would be bad, but we also modify the way we
search for handlers by first looking at the notifying tasks dependency
chain (ensuring that roles find their own handlers first) and then at
the main list of handlers, using the first match it finds.

This patch also modifies the way we setup the internal list of handlers,
which should allow us to correctly identify if a notified handler exists
more easily.

Fixes #15084
jimi-c added a commit that referenced this issue Jun 14, 2016
Due to the fact that roles may be instantiated with different sets of
params (multiple inclusions of the same role or via role dependencies),
simply tracking notified handlers by name does not work. This patch
changes the way we track handler notifications by using the handler
object itself instead of just the name, allowing for multiple internal
instances. Normally this would be bad, but we also modify the way we
search for handlers by first looking at the notifying tasks dependency
chain (ensuring that roles find their own handlers first) and then at
the main list of handlers, using the first match it finds.

This patch also modifies the way we setup the internal list of handlers,
which should allow us to correctly identify if a notified handler exists
more easily.

Fixes #15084
jimi-c added a commit that referenced this issue Jun 15, 2016
Due to the fact that roles may be instantiated with different sets of
params (multiple inclusions of the same role or via role dependencies),
simply tracking notified handlers by name does not work. This patch
changes the way we track handler notifications by using the handler
object itself instead of just the name, allowing for multiple internal
instances. Normally this would be bad, but we also modify the way we
search for handlers by first looking at the notifying tasks dependency
chain (ensuring that roles find their own handlers first) and then at
the main list of handlers, using the first match it finds.

This patch also modifies the way we setup the internal list of handlers,
which should allow us to correctly identify if a notified handler exists
more easily.

Fixes #15084
@jimi-c jimi-c closed this as completed in 8218591 Jun 20, 2016
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

4 participants